Search in sources :

Example 11 with PooledDataSource

use of org.apache.ibatis.datasource.pooled.PooledDataSource in project camunda-bpm-platform by camunda.

the class DatabaseTablePrefixTest method shouldPerformDatabaseSchemaOperationCreate.

@Test
public void shouldPerformDatabaseSchemaOperationCreate() 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", "");
    // create two schemas is the database
    Connection connection = pooledDataSource.getConnection();
    connection.createStatement().execute("drop schema if exists SCHEMA1");
    connection.createStatement().execute("drop schema if exists SCHEMA2");
    connection.createStatement().execute("create schema SCHEMA1");
    connection.createStatement().execute("create schema SCHEMA2");
    connection.close();
    // configure & build two different process engines, each having a separate table prefix
    ProcessEngineConfigurationImpl config1 = createCustomProcessEngineConfiguration().setProcessEngineName("DatabaseTablePrefixTest-engine1").setDataSource(pooledDataSource).setDbMetricsReporterActivate(false).setDatabaseSchemaUpdate(// disable auto create/drop schema
    "NO_CHECK");
    config1.setDatabaseTablePrefix("SCHEMA1.");
    config1.setUseSharedSqlSessionFactory(true);
    ProcessEngine engine1 = config1.buildProcessEngine();
    ProcessEngineConfigurationImpl config2 = createCustomProcessEngineConfiguration().setProcessEngineName("DatabaseTablePrefixTest-engine2").setDataSource(pooledDataSource).setDbMetricsReporterActivate(false).setDatabaseSchemaUpdate(// disable auto create/drop schema
    "NO_CHECK");
    config2.setDatabaseTablePrefix("SCHEMA2.");
    config2.setUseSharedSqlSessionFactory(true);
    ProcessEngine engine2 = config2.buildProcessEngine();
    // create the tables in SCHEMA1
    connection = pooledDataSource.getConnection();
    connection.createStatement().execute("set schema SCHEMA1");
    engine1.getManagementService().databaseSchemaUpgrade(connection, "", "SCHEMA1");
    connection.close();
    // create the tables in SCHEMA2
    connection = pooledDataSource.getConnection();
    connection.createStatement().execute("set schema SCHEMA2");
    engine2.getManagementService().databaseSchemaUpgrade(connection, "", "SCHEMA2");
    connection.close();
    // engine:
    try {
        engine1.getRepositoryService().createDeployment().addClasspathResource("org/camunda/bpm/engine/test/api/cfg/oneJobProcess.bpmn20.xml").deploy();
        assertEquals(1, engine1.getRepositoryService().createDeploymentQuery().count());
        assertEquals(0, engine2.getRepositoryService().createDeploymentQuery().count());
    } finally {
        engine1.close();
        engine2.close();
        ProcessEngineConfigurationImpl.cachedSqlSessionFactory = null;
    }
}
Also used : Connection(java.sql.Connection) ProcessEngineConfigurationImpl(org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl) PooledDataSource(org.apache.ibatis.datasource.pooled.PooledDataSource) ProcessEngine(org.camunda.bpm.engine.ProcessEngine) Test(org.junit.Test)

Example 12 with PooledDataSource

use of org.apache.ibatis.datasource.pooled.PooledDataSource in project camunda-bpm-platform by camunda.

the class ForceCloseMybatisConnectionPoolTest method testForceCloseMybatisConnectionPoolTrue.

@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);
}
Also used : PoolState(org.apache.ibatis.datasource.pooled.PoolState) ProcessEngineConfigurationImpl(org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl) StandaloneInMemProcessEngineConfiguration(org.camunda.bpm.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration) ProcessEngine(org.camunda.bpm.engine.ProcessEngine) PooledDataSource(org.apache.ibatis.datasource.pooled.PooledDataSource) Test(org.junit.Test)

Example 13 with PooledDataSource

use of org.apache.ibatis.datasource.pooled.PooledDataSource in project camunda-bpm-platform by camunda.

the class ConnectionPoolTest method testMyBatisConnectionPoolProperlyConfigured.

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();
}
Also used : ProcessEngineConfiguration(org.camunda.bpm.engine.ProcessEngineConfiguration) Configuration(org.apache.ibatis.session.Configuration) ProcessEngineConfigurationImpl(org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl) ProcessEngine(org.camunda.bpm.engine.ProcessEngine) PooledDataSource(org.apache.ibatis.datasource.pooled.PooledDataSource) DataSource(javax.sql.DataSource) PooledDataSource(org.apache.ibatis.datasource.pooled.PooledDataSource)

Example 14 with PooledDataSource

use of org.apache.ibatis.datasource.pooled.PooledDataSource in project Activiti by Activiti.

the class ConnectionPoolTest method testMyBatisConnectionPoolProperlyConfigured.

public void testMyBatisConnectionPoolProperlyConfigured() {
    ProcessEngineConfigurationImpl config = (ProcessEngineConfigurationImpl) ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("org/activiti/engine/test/db/connection-pool.activiti.cfg.xml");
    config.buildProcessEngine();
    // Expected values
    int maxActive = 25;
    int maxIdle = 10;
    int maxCheckoutTime = 30000;
    int maxWaitTime = 25000;
    assertThat(config.getJdbcMaxActiveConnections()).isEqualTo(maxActive);
    assertThat(config.getJdbcMaxIdleConnections()).isEqualTo(maxIdle);
    assertThat(config.getJdbcMaxCheckoutTime()).isEqualTo(maxCheckoutTime);
    assertThat(config.getJdbcMaxWaitTime()).isEqualTo(maxWaitTime);
    // Verify that these properties are correctly set in the MyBatis
    // datasource
    DataSource datasource = config.getDbSqlSessionFactory().getSqlSessionFactory().getConfiguration().getEnvironment().getDataSource();
    assertThat(datasource).isInstanceOf(PooledDataSource.class);
    PooledDataSource pooledDataSource = (PooledDataSource) datasource;
    assertThat(pooledDataSource.getPoolMaximumActiveConnections()).isEqualTo(maxActive);
    assertThat(pooledDataSource.getPoolMaximumIdleConnections()).isEqualTo(maxIdle);
    assertThat(pooledDataSource.getPoolMaximumCheckoutTime()).isEqualTo(maxCheckoutTime);
    assertThat(pooledDataSource.getPoolTimeToWait()).isEqualTo(maxWaitTime);
}
Also used : ProcessEngineConfigurationImpl(org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl) PooledDataSource(org.apache.ibatis.datasource.pooled.PooledDataSource) DataSource(javax.sql.DataSource) PooledDataSource(org.apache.ibatis.datasource.pooled.PooledDataSource)

Example 15 with PooledDataSource

use of org.apache.ibatis.datasource.pooled.PooledDataSource in project tutorials by eugenp.

the class MyBatisUtil method buildqlSessionFactory.

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;
}
Also used : Configuration(org.apache.ibatis.session.Configuration) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Environment(org.apache.ibatis.mapping.Environment) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) PooledDataSource(org.apache.ibatis.datasource.pooled.PooledDataSource) DataSource(javax.sql.DataSource) PooledDataSource(org.apache.ibatis.datasource.pooled.PooledDataSource)

Aggregations

PooledDataSource (org.apache.ibatis.datasource.pooled.PooledDataSource)20 Connection (java.sql.Connection)11 ProcessEngine (org.camunda.bpm.engine.ProcessEngine)7 ProcessEngineConfigurationImpl (org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl)7 DataSource (javax.sql.DataSource)5 BaseDataTest (org.apache.ibatis.BaseDataTest)4 JDBCConnection (org.hsqldb.jdbc.JDBCConnection)4 Test (org.junit.Test)4 Test (org.junit.jupiter.api.Test)4 Properties (java.util.Properties)3 ProcessEngineConfigurationImpl (org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl)2 PoolState (org.apache.ibatis.datasource.pooled.PoolState)2 Configuration (org.apache.ibatis.session.Configuration)2 StandaloneInMemProcessEngineConfiguration (org.camunda.bpm.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1