org.apache.ibatis.datasource.pooled.PooledDataSource Java Examples

The following examples show how to use org.apache.ibatis.datasource.pooled.PooledDataSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolFalse() {

    // given
    // that the AbstractEngineConfiguration is configured with forceCloseMybatisConnectionPool = false
    StandaloneInMemDmnEngineConfiguration standaloneInMemDmnEngineConfiguration =  new StandaloneInMemDmnEngineConfiguration();
    standaloneInMemDmnEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-dmn-" + this.getClass().getName());
    standaloneInMemDmnEngineConfiguration.setForceCloseMybatisConnectionPool(false);

    DmnEngine dmnEngine = standaloneInMemDmnEngineConfiguration.buildDmnEngine();

    PooledDataSource pooledDataSource = (PooledDataSource) standaloneInMemDmnEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isPositive();

    // then
    // if the  engine is closed
    dmnEngine.close();

    // the idle connections are not closed
    assertThat(state.getIdleConnectionCount()).isPositive();

    pooledDataSource.forceCloseAll();
    assertThat(state.getIdleConnectionCount()).isZero();
}
 
Example #2
Source File: PooledDataSourceTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotFailCallingToStringOverAnInvalidConnection() throws Exception {
  PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
  Connection c = ds.getConnection();
  c.close();
  c.toString();
}
 
Example #3
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolFalse() {

    // given
    // that the process engine is configured with forceCloseMybatisConnectionPool = false
    StandaloneInMemProcessEngineConfiguration standaloneInMemProcessEngineConfiguration =  new StandaloneInMemProcessEngineConfiguration();
    standaloneInMemProcessEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-bpmn-" + this.getClass().getName());
    standaloneInMemProcessEngineConfiguration.setForceCloseMybatisConnectionPool(false);
    ProcessEngine processEngine = standaloneInMemProcessEngineConfiguration.buildProcessEngine();

    PooledDataSource pooledDataSource = (PooledDataSource) standaloneInMemProcessEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isGreaterThan(0);

    // then
    // if the process engine is closed
    processEngine.close();

    // the idle connections are not closed
    assertThat(state.getIdleConnectionCount()).isGreaterThan(0);

    pooledDataSource.forceCloseAll();
    assertThat(state.getIdleConnectionCount()).isZero();
}
 
Example #4
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolTrue() {

    // given
    // that the process engine is configured with forceCloseMybatisConnectionPool = true
    StandaloneInMemProcessEngineConfiguration standaloneInMemProcessEngineConfiguration =  new StandaloneInMemProcessEngineConfiguration();
    standaloneInMemProcessEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-bpmn-" + this.getClass().getName());
    standaloneInMemProcessEngineConfiguration.setForceCloseMybatisConnectionPool(true);

    ProcessEngine processEngine = standaloneInMemProcessEngineConfiguration.buildProcessEngine();

    PooledDataSource pooledDataSource = (PooledDataSource) standaloneInMemProcessEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isGreaterThan(0);

    // then
    // if the process engine is closed
    processEngine.close();

    // the idle connections are closed
    assertThat(state.getIdleConnectionCount()).isZero();
}
 
Example #5
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolTrue() {

    // given
    // that the AbstractEngineConfiguration is configured with forceCloseMybatisConnectionPool = true
    StandaloneInMemAppEngineConfiguration appEngineConfiguration =  new StandaloneInMemAppEngineConfiguration();
    appEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-app-" + this.getClass().getName());
    appEngineConfiguration.setForceCloseMybatisConnectionPool(true);

    AppEngine appEngine = appEngineConfiguration.buildAppEngine();


    PooledDataSource pooledDataSource = (PooledDataSource) appEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isPositive();

    // then
    // if the  engine is closed
    appEngine.close();

    // the idle connections are closed
    assertThat(state.getIdleConnectionCount()).isZero();
}
 
Example #6
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolFalse() {

    // given
    // that the AbstractEngineConfiguration is configured with forceCloseMybatisConnectionPool = false
    StandaloneInMemCmmnEngineConfiguration standaloneInMemCmmnEngineConfiguration = new StandaloneInMemCmmnEngineConfiguration();
    standaloneInMemCmmnEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-cmmn-" + this.getClass().getName());
    standaloneInMemCmmnEngineConfiguration.setForceCloseMybatisConnectionPool(false);

    standaloneInMemCmmnEngineConfiguration.buildCmmnEngine();

    PooledDataSource pooledDataSource = (PooledDataSource) standaloneInMemCmmnEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isPositive();

    // then
    // if the  engine is closed
    standaloneInMemCmmnEngineConfiguration.close();

    // the idle connections are not closed
    assertThat(state.getIdleConnectionCount()).isPositive();

    pooledDataSource.forceCloseAll();
    assertThat(state.getIdleConnectionCount()).isZero();
}
 
Example #7
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolTrue() {

    // given
    // that the AbstractEngineConfiguration is configured with forceCloseMybatisConnectionPool = true
    StandaloneInMemCmmnEngineConfiguration standaloneInMemCmmnEngineConfiguration = new StandaloneInMemCmmnEngineConfiguration();
    standaloneInMemCmmnEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-cmmn-" + this.getClass().getName());
    standaloneInMemCmmnEngineConfiguration.setForceCloseMybatisConnectionPool(true);

    CmmnEngine cmmnEngine = standaloneInMemCmmnEngineConfiguration.buildCmmnEngine();

    PooledDataSource pooledDataSource = (PooledDataSource) standaloneInMemCmmnEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isPositive();

    // then
    // if the  engine is closed
    cmmnEngine.close();

    // the idle connections are closed
    assertThat(state.getIdleConnectionCount()).isZero();
}
 
Example #8
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolFalse() {

    // given
    // that the AbstractEngineConfiguration is configured with forceCloseMybatisConnectionPool = false
    StandaloneInMemAppEngineConfiguration appEngineConfiguration =  new StandaloneInMemAppEngineConfiguration();
    appEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-app-" + this.getClass().getName());
    appEngineConfiguration.setForceCloseMybatisConnectionPool(false);

    AppEngine appEngine = appEngineConfiguration.buildAppEngine();

    PooledDataSource pooledDataSource = (PooledDataSource) appEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isPositive();

    // then
    // if the  engine is closed
    appEngine.close();

    // the idle connections are not closed
    assertThat(state.getIdleConnectionCount()).isPositive();

    pooledDataSource.forceCloseAll();
    assertThat(state.getIdleConnectionCount()).isZero();
}
 
Example #9
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolTrue() {

    // given
    // that the AbstractEngineConfiguration is configured with forceCloseMybatisConnectionPool = true
    StandaloneInMemFormEngineConfiguration standaloneInMemFormEngineConfiguration =  new StandaloneInMemFormEngineConfiguration();
    standaloneInMemFormEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-form-" + this.getClass().getName());
    standaloneInMemFormEngineConfiguration.setForceCloseMybatisConnectionPool(true);

    FormEngine formEngine = standaloneInMemFormEngineConfiguration.buildFormEngine();


    PooledDataSource pooledDataSource = (PooledDataSource) standaloneInMemFormEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isPositive();

    // then
    // if the  engine is closed
    formEngine.close();

    // the idle connections are closed
    assertThat(state.getIdleConnectionCount()).isZero();

}
 
Example #10
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolFalse() {

    // given
    // that the AbstractEngineConfiguration is configured with forceCloseMybatisConnectionPool = false
    StandaloneInMemIdmEngineConfiguration standaloneInMemIdmEngineConfiguration = new StandaloneInMemIdmEngineConfiguration();
    standaloneInMemIdmEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-idm-" + this.getClass().getName());
    standaloneInMemIdmEngineConfiguration.setForceCloseMybatisConnectionPool(false);
    standaloneInMemIdmEngineConfiguration.setDatabaseSchemaUpdate("drop-create");

    IdmEngine idmEngine = standaloneInMemIdmEngineConfiguration.buildIdmEngine();

    PooledDataSource pooledDataSource = (PooledDataSource) standaloneInMemIdmEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isPositive();

    // then
    // if the  engine is closed
    idmEngine.close();

    // the idle connections are not closed
    assertThat(state.getIdleConnectionCount()).isPositive();

    pooledDataSource.forceCloseAll();
    assertThat(state.getIdleConnectionCount()).isZero();
}
 
Example #11
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolTrue() {

    // given
    // that the AbstractEngineConfiguration is configured with forceCloseMybatisConnectionPool = true
    StandaloneInMemIdmEngineConfiguration standaloneInMemIdmEngineConfiguration = new StandaloneInMemIdmEngineConfiguration();
    standaloneInMemIdmEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-idm-" + this.getClass().getName());
    standaloneInMemIdmEngineConfiguration.setForceCloseMybatisConnectionPool(true);
    standaloneInMemIdmEngineConfiguration.setDatabaseSchemaUpdate("drop-create");

    IdmEngine idmEngine = standaloneInMemIdmEngineConfiguration.buildIdmEngine();

    PooledDataSource pooledDataSource = (PooledDataSource) standaloneInMemIdmEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isPositive();

    // then
    // if the  engine is closed
    idmEngine.close();

    // the idle connections are closed
    assertThat(state.getIdleConnectionCount()).isZero();
}
 
Example #12
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolTrue() {

    // given
    // that the AbstractEngineConfiguration is configured with forceCloseMybatisConnectionPool = true
    StandaloneInMemDmnEngineConfiguration standaloneInMemDmnEngineConfiguration =  new StandaloneInMemDmnEngineConfiguration();
    standaloneInMemDmnEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-dmn-" + this.getClass().getName());
    standaloneInMemDmnEngineConfiguration.setForceCloseMybatisConnectionPool(true);

    DmnEngine dmnEngine = standaloneInMemDmnEngineConfiguration.buildDmnEngine();


    PooledDataSource pooledDataSource = (PooledDataSource) standaloneInMemDmnEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isPositive();

    // then
    // if the  engine is closed
    dmnEngine.close();

    // the idle connections are closed
    assertThat(state.getIdleConnectionCount()).isZero();

}
 
Example #13
Source File: AbstractAccTest.java    From taskana with Apache License 2.0 6 votes vote down vote up
/**
 * create Default DataSource for in-memory database.
 *
 * @return the TASKANA default datasource.
 */
private static DataSource createDefaultDataSource() {

  String jdbcDriver = "org.h2.Driver";
  String jdbcUrl = "jdbc:h2:mem:taskana;IGNORECASE=TRUE;LOCK_MODE=0";
  String dbUserName = "sa";
  String dbPassword = "sa";
  PooledDataSource ds =
      new PooledDataSource(
          Thread.currentThread().getContextClassLoader(),
          jdbcDriver,
          jdbcUrl,
          dbUserName,
          dbPassword);
  ds.setPoolTimeToWait(POOL_TIME_TO_WAIT);
  ds.forceCloseAll(); // otherwise the MyBatis pool is not initialized correctly

  return ds;
}
 
Example #14
Source File: TaskanaEngineTestConfiguration.java    From taskana with Apache License 2.0 6 votes vote down vote up
/**
 * create Default Datasource for in-memory database.
 *
 * @return the default datasource.
 */
private static DataSource createDefaultDataSource() {
  // JdbcDataSource ds = new JdbcDataSource();
  // ds.setURL("jdbc:h2:mem:taskana;IGNORECASE=TRUE;LOCK_MODE=0");
  // ds.setPassword("sa");
  // ds.setUser("sa");

  String jdbcDriver = "org.h2.Driver";
  String jdbcUrl =
      "jdbc:h2:mem:taskana;IGNORECASE=TRUE;LOCK_MODE=0;"
          + "INIT=CREATE SCHEMA IF NOT EXISTS TASKANA\\;"
          + "SET COLLATION DEFAULT_de_DE ";
  String dbUserName = "sa";
  String dbPassword = "sa";
  PooledDataSource ds =
      new PooledDataSource(
          Thread.currentThread().getContextClassLoader(),
          jdbcDriver,
          jdbcUrl,
          dbUserName,
          dbPassword);
  ds.setPoolTimeToWait(POOL_TIME_TO_WAIT);
  ds.forceCloseAll(); // otherwise the MyBatis pool is not initialized correctly

  return ds;
}
 
Example #15
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolFalse() {

    // given
    // that the AbstractEngineConfiguration is configured with forceCloseMybatisConnectionPool = false
    StandaloneInMemFormEngineConfiguration standaloneInMemFormEngineConfiguration =  new StandaloneInMemFormEngineConfiguration();
    standaloneInMemFormEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-form-" + this.getClass().getName());
    standaloneInMemFormEngineConfiguration.setForceCloseMybatisConnectionPool(false);

    FormEngine formEngine = standaloneInMemFormEngineConfiguration.buildFormEngine();

    PooledDataSource pooledDataSource = (PooledDataSource) standaloneInMemFormEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isPositive();

    // then
    // if the  engine is closed
    formEngine.close();

    // the idle connections are not closed
    assertThat(state.getIdleConnectionCount()).isPositive();

    pooledDataSource.forceCloseAll();
    assertThat(state.getIdleConnectionCount()).isZero();
}
 
Example #16
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolTrue() {

    // given
    // that the AbstractEngineConfiguration is configured with forceCloseMybatisConnectionPool = true
    StandaloneInMemContentEngineConfiguration standaloneInMemContentEngineConfiguration = new StandaloneInMemContentEngineConfiguration();
    standaloneInMemContentEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-content-" + this.getClass().getName());
    standaloneInMemContentEngineConfiguration.setForceCloseMybatisConnectionPool(true);

    ContentEngine contentEngine = standaloneInMemContentEngineConfiguration.buildContentEngine();

    PooledDataSource pooledDataSource = (PooledDataSource) standaloneInMemContentEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isPositive();

    // then
    // if the  engine is closed
    contentEngine.close();

    // the idle connections are closed
    assertThat(state.getIdleConnectionCount()).isEqualTo(0);

}
 
Example #17
Source File: ForceCloseMybatisConnectionPoolTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolFalse() {

    // given
    // that the AbstractEngineConfiguration is configured with forceCloseMybatisConnectionPool = false
    StandaloneInMemContentEngineConfiguration standaloneInMemContentEngineConfiguration = new StandaloneInMemContentEngineConfiguration();
    standaloneInMemContentEngineConfiguration.setJdbcUrl("jdbc:h2:mem:flowable-content-" + this.getClass().getName());
    standaloneInMemContentEngineConfiguration.setForceCloseMybatisConnectionPool(false);

    ContentEngine contentEngine = standaloneInMemContentEngineConfiguration.buildContentEngine();

    PooledDataSource pooledDataSource = (PooledDataSource) standaloneInMemContentEngineConfiguration.getDataSource();
    PoolState state = pooledDataSource.getPoolState();
    assertThat(state.getIdleConnectionCount()).isPositive();

    // then
    // if the  engine is closed
    contentEngine.close();

    // the idle connections are not closed
    assertThat(state.getIdleConnectionCount()).isPositive();
    pooledDataSource.forceCloseAll();
    assertThat(state.getIdleConnectionCount()).isEqualTo(0);
}
 
Example #18
Source File: ForceCloseMybatisConnectionPoolTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void testForceCloseMybatisConnectionPoolTrue() {

  // given
  // that the process engine is configured with forceCloseMybatisConnectionPool = true
  ProcessEngineConfigurationImpl configurationImpl = new StandaloneInMemProcessEngineConfiguration()
   .setJdbcUrl("jdbc:h2:mem:camunda-forceclose")
   .setProcessEngineName("engine-forceclose")
   .setForceCloseMybatisConnectionPool(true);

  ProcessEngine processEngine = configurationImpl
   .buildProcessEngine();

  PooledDataSource pooledDataSource = (PooledDataSource) configurationImpl.getDataSource();
  PoolState state = pooledDataSource.getPoolState();


  // then
  // if the process engine is closed
  processEngine.close();

  // the idle connections are closed
  Assert.assertTrue(state.getIdleConnectionCount() == 0);

}
 
Example #19
Source File: PooledDataSourceTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotFailCallingToStringOverAnInvalidConnection() throws Exception {
  PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
  Connection c = ds.getConnection();
  c.close();
  c.toString();
}
 
Example #20
Source File: BaseDataTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
public static PooledDataSource createPooledDataSource(String resource) throws IOException {
  Properties props = Resources.getResourceAsProperties(resource);
  PooledDataSource ds = new PooledDataSource();
  ds.setDriver(props.getProperty("driver"));
  ds.setUrl(props.getProperty("url"));
  ds.setUsername(props.getProperty("username"));
  ds.setPassword(props.getProperty("password"));
  return ds;
}
 
Example #21
Source File: BaseDb.java    From live-chat-engine with Apache License 2.0 5 votes vote down vote up
public static PooledDataSource createDataSource(Props p) {
	
	String url = p.findVal(db_url);
	
	PooledDataSource ds = new PooledDataSource();
	ds.setDriver(p.findVal(db_driver));         
	ds.setUrl(url);
	ds.setUsername(p.findVal(db_user));                                  
	ds.setPassword(p.findVal(db_psw));
	ds.setPoolMaximumActiveConnections(p.getIntVal(db_maxConnections));
	ds.setPoolMaximumIdleConnections(p.getIntVal(db_idleConnections));
	
	return ds;
}
 
Example #22
Source File: DbSchemaPrefixTestHelper.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void afterPropertiesSet() throws Exception {

    dataSource = new PooledDataSource(ReflectUtil.getClassLoader(),
            "org.h2.Driver",
            "jdbc:h2:mem:DatabaseTablePrefixTest;DB_CLOSE_DELAY=1000;MVCC=TRUE;",
            "sa",
            "" );

    // create schema in the
    Connection connection = dataSource.getConnection();
    connection.createStatement().execute("drop schema if exists SCHEMA1");
    connection.createStatement().execute("create schema SCHEMA1");
    connection.close();

    ProcessEngineConfigurationImpl config1 = createCustomProcessEngineConfiguration()
            .setProcessEngineName("DatabaseTablePrefixTest-engine1")
            .setDataSource(dataSource)
            .setDbMetricsReporterActivate(false)
            .setDatabaseSchemaUpdate("NO_CHECK"); // disable auto create/drop schema
    config1.setDatabaseTablePrefix("SCHEMA1.");
    ProcessEngine engine1 = config1.buildProcessEngine();

    // create the tables in SCHEMA1
    connection = dataSource.getConnection();
    connection.createStatement().execute("set schema SCHEMA1");
    engine1.getManagementService().databaseSchemaUpgrade(connection, "", "SCHEMA1");
    connection.close();

    engine1.close();

  }
 
Example #23
Source File: PooledDataSourceTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldProperlyMaintainPoolOf3ActiveAnd2IdleConnections() throws Exception {
  PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
  try {
    runScript(ds, JPETSTORE_DDL);
    ds.setDefaultAutoCommit(false);
    ds.setDriverProperties(new Properties() {
      {
        setProperty("username", "sa");
        setProperty("password", "");
      }
    });
    ds.setPoolMaximumActiveConnections(3);
    ds.setPoolMaximumIdleConnections(2);
    ds.setPoolMaximumCheckoutTime(10000);
    ds.setPoolPingConnectionsNotUsedFor(1);
    ds.setPoolPingEnabled(true);
    ds.setPoolPingQuery("SELECT * FROM PRODUCT");
    ds.setPoolTimeToWait(10000);
    ds.setLogWriter(null);
    List<Connection> connections = new ArrayList<Connection>();
    for (int i = 0; i < 3; i++) {
      connections.add(ds.getConnection());
    }
    assertEquals(3, ds.getPoolState().getActiveConnectionCount());
    for (Connection c : connections) {
      c.close();
    }
    assertEquals(2, ds.getPoolState().getIdleConnectionCount());
    assertEquals(4, ds.getPoolState().getRequestCount());
    assertEquals(0, ds.getPoolState().getBadConnectionCount());
    assertEquals(0, ds.getPoolState().getHadToWaitCount());
    assertEquals(0, ds.getPoolState().getAverageOverdueCheckoutTime());
    assertEquals(0, ds.getPoolState().getClaimedOverdueConnectionCount());
    assertEquals(0, ds.getPoolState().getAverageWaitTime());
    assertNotNull(ds.getPoolState().toString());
  } finally {
    ds.forceCloseAll();
  }
}
 
Example #24
Source File: ScriptRunnerTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void assertProductsTableExistsAndLoaded() throws IOException, SQLException {
  PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
  try {
    Connection conn = ds.getConnection();
    SqlRunner executor = new SqlRunner(conn);
    List<Map<String, Object>> products = executor.selectAll("SELECT * FROM PRODUCT");
    assertEquals(16, products.size());
  } finally {
    ds.forceCloseAll();
  }
}
 
Example #25
Source File: BaseDataTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
public static PooledDataSource createPooledDataSource(String resource) throws IOException {
  Properties props = Resources.getResourceAsProperties(resource);
  PooledDataSource ds = new PooledDataSource();
  ds.setDriver(props.getProperty("driver"));
  ds.setUrl(props.getProperty("url"));
  ds.setUsername(props.getProperty("username"));
  ds.setPassword(props.getProperty("password"));
  return ds;
}
 
Example #26
Source File: MyBatisUtil.java    From tutorials with MIT License 5 votes vote down vote up
public static SqlSessionFactory buildqlSessionFactory() {
	DataSource dataSource = new PooledDataSource(DRIVER, URL, USERNAME, PASSWORD);
	Environment environment = new Environment("Development", new JdbcTransactionFactory(), dataSource);
	Configuration configuration = new Configuration(environment);
	configuration.addMapper(PersonMapper.class);
	configuration.addMapper(AddressMapper.class);
	SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
	SqlSessionFactory factory = builder.build(configuration);
	return factory;

}
 
Example #27
Source File: ProcessEngineConfigurationImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void close() {
  if (forceCloseMybatisConnectionPool
      && dataSource instanceof PooledDataSource) {

    // ACT-233: connection pool of Ibatis is not properely initialized if this is not called!
    ((PooledDataSource) dataSource).forceCloseAll();
  }
}
 
Example #28
Source File: ConnectionPoolTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void testMyBatisConnectionPoolProperlyConfigured() {
  ProcessEngineConfigurationImpl config = (ProcessEngineConfigurationImpl) ProcessEngineConfiguration
    .createProcessEngineConfigurationFromResource("org/camunda/bpm/engine/test/api/cfg/connection-pool.camunda.cfg.xml");

  ProcessEngine engine = config.buildProcessEngine();

  // Expected values
  int maxActive = 25;
  int maxIdle = 10;
  int maxCheckoutTime = 30000;
  int maxWaitTime = 25000;
  Integer jdbcStatementTimeout = 300;

  assertEquals(maxActive, config.getJdbcMaxActiveConnections());
  assertEquals(maxIdle, config.getJdbcMaxIdleConnections());
  assertEquals(maxCheckoutTime, config.getJdbcMaxCheckoutTime());
  assertEquals(maxWaitTime, config.getJdbcMaxWaitTime());
  assertEquals(jdbcStatementTimeout, config.getJdbcStatementTimeout());

  // Verify that these properties are correctly set in the MyBatis datasource
  Configuration sessionFactoryConfiguration = config.getDbSqlSessionFactory().getSqlSessionFactory().getConfiguration();
  DataSource datasource = sessionFactoryConfiguration.getEnvironment().getDataSource();
  assertTrue(datasource instanceof PooledDataSource);

  PooledDataSource pooledDataSource = (PooledDataSource) datasource;
  assertEquals(maxActive, pooledDataSource.getPoolMaximumActiveConnections());
  assertEquals(maxIdle, pooledDataSource.getPoolMaximumIdleConnections());
  assertEquals(maxCheckoutTime, pooledDataSource.getPoolMaximumCheckoutTime());
  assertEquals(maxWaitTime, pooledDataSource.getPoolTimeToWait());

  assertEquals(jdbcStatementTimeout, sessionFactoryConfiguration.getDefaultStatementTimeout());

  engine.close();
}
 
Example #29
Source File: DatabaseTableSchemaTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void testPerformDatabaseSchemaOperationCreateTwice() throws Exception {

    // both process engines will be using this datasource.
    PooledDataSource pooledDataSource = new PooledDataSource(ReflectUtil.getClassLoader(), "org.h2.Driver",
        "jdbc:h2:mem:DatabaseTablePrefixTest;DB_CLOSE_DELAY=1000", "sa", "");

    Connection connection = pooledDataSource.getConnection();
    connection.createStatement().execute("drop schema if exists " + SCHEMA_NAME);
    connection.createStatement().execute("create schema " + SCHEMA_NAME);
    connection.close();

    ProcessEngineConfigurationImpl config1 = createCustomProcessEngineConfiguration().setProcessEngineName("DatabaseTablePrefixTest-engine1")
    // disable auto create/drop schema
        .setDataSource(pooledDataSource).setDatabaseSchemaUpdate("NO_CHECK");
    config1.setDatabaseTablePrefix(SCHEMA_NAME + ".");
    config1.setDatabaseSchema(SCHEMA_NAME);
    config1.setDbMetricsReporterActivate(false);
    ProcessEngine engine1 = config1.buildProcessEngine();

    // create the tables for the first time
    connection = pooledDataSource.getConnection();
    connection.createStatement().execute("set schema " + SCHEMA_NAME);
    engine1.getManagementService().databaseSchemaUpgrade(connection, "", SCHEMA_NAME);
    connection.close();
    // create the tables for the second time; here we shouldn't crash since the
    // session should tell us that the tables are already present and
    // databaseSchemaUpdate is set to noop
    connection = pooledDataSource.getConnection();
    connection.createStatement().execute("set schema " + SCHEMA_NAME);
    engine1.getManagementService().databaseSchemaUpgrade(connection, "", SCHEMA_NAME);
    engine1.close();
  }
 
Example #30
Source File: DatabaseTableSchemaTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void testTablePresentWithSchemaAndPrefix() throws SQLException {
  PooledDataSource pooledDataSource = new PooledDataSource(ReflectUtil.getClassLoader(), "org.h2.Driver",
      "jdbc:h2:mem:DatabaseTablePrefixTest;DB_CLOSE_DELAY=1000", "sa", "");

  Connection connection = pooledDataSource.getConnection();
  connection.createStatement().execute("drop schema if exists " + SCHEMA_NAME);
  connection.createStatement().execute("create schema " + SCHEMA_NAME);
  connection.createStatement().execute("create table " + SCHEMA_NAME + "." + PREFIX_NAME + "SOME_TABLE(id varchar(64));");
  connection.close();

  ProcessEngineConfigurationImpl config1 = createCustomProcessEngineConfiguration().setProcessEngineName("DatabaseTablePrefixTest-engine1")
  // disable auto create/drop schema
      .setDataSource(pooledDataSource).setDatabaseSchemaUpdate("NO_CHECK");
  config1.setDatabaseTablePrefix(SCHEMA_NAME + "." + PREFIX_NAME);
  config1.setDatabaseSchema(SCHEMA_NAME);
  config1.setDbMetricsReporterActivate(false);
  ProcessEngine engine = config1.buildProcessEngine();
  CommandExecutor commandExecutor = config1.getCommandExecutorTxRequired();

  commandExecutor.execute(new Command<Void>(){
    public Void execute(CommandContext commandContext) {
      DbSqlSession sqlSession = commandContext.getSession(DbSqlSession.class);
      assertTrue(sqlSession.isTablePresent("SOME_TABLE"));
      return null;
    }
  });

  engine.close();

}