Search in sources :

Example 6 with PooledDataSource

use of org.apache.ibatis.datasource.pooled.PooledDataSource in project mybatis-3 by mybatis.

the class PooledDataSourceTest method shouldReconnectWhenServerKilledLeakedConnection.

@Ignore("See the comments")
@Test
public void shouldReconnectWhenServerKilledLeakedConnection() throws Exception {
    // See #748
    // Requirements:
    // 1. MySQL JDBC driver dependency.
    // 2. MySQL server instance with the following.
    //  - CREATE DATABASE `test`;
    //  - SET GLOBAL wait_timeout=3;
    // 3. Tweak the connection info below.
    final String URL = "jdbc:mysql://localhost:3306/test";
    final String USERNAME = "admin";
    final String PASSWORD = "";
    Connection con;
    PooledDataSource ds = new PooledDataSource();
    ds.setDriver("com.mysql.jdbc.Driver");
    ds.setUrl(URL);
    ds.setUsername(USERNAME);
    ds.setPassword(PASSWORD);
    ds.setPoolMaximumActiveConnections(1);
    ds.setPoolMaximumIdleConnections(1);
    ds.setPoolTimeToWait(1000);
    ds.setPoolMaximumCheckoutTime(2000);
    ds.setPoolPingEnabled(true);
    ds.setPoolPingQuery("select 1");
    ds.setDefaultAutoCommit(true);
    // MySQL wait_timeout * 1000 or less. (unit:ms)
    ds.setPoolPingConnectionsNotUsedFor(1000);
    con = ds.getConnection();
    exexuteQuery(con);
    // Simulate connection leak by not closing.
    // con.close();
    // Wait for disconnected from mysql...
    Thread.sleep(TimeUnit.SECONDS.toMillis(3));
    // Should return usable connection.
    con = ds.getConnection();
    exexuteQuery(con);
    con.close();
}
Also used : Connection(java.sql.Connection) JDBCConnection(org.hsqldb.jdbc.JDBCConnection) PooledDataSource(org.apache.ibatis.datasource.pooled.PooledDataSource) Ignore(org.junit.Ignore) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 7 with PooledDataSource

use of org.apache.ibatis.datasource.pooled.PooledDataSource in project mybatis-3 by mybatis.

the class PooledDataSourceTest method ShouldReturnRealConnection.

@Test
public void ShouldReturnRealConnection() throws Exception {
    PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
    Connection c = ds.getConnection();
    JDBCConnection realConnection = (JDBCConnection) PooledDataSource.unwrapConnection(c);
}
Also used : Connection(java.sql.Connection) JDBCConnection(org.hsqldb.jdbc.JDBCConnection) JDBCConnection(org.hsqldb.jdbc.JDBCConnection) PooledDataSource(org.apache.ibatis.datasource.pooled.PooledDataSource) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 8 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;
    assertEquals(maxActive, config.getJdbcMaxActiveConnections());
    assertEquals(maxIdle, config.getJdbcMaxIdleConnections());
    assertEquals(maxCheckoutTime, config.getJdbcMaxCheckoutTime());
    assertEquals(maxWaitTime, config.getJdbcMaxWaitTime());
    // Verify that these properties are correctly set in the MyBatis datasource
    DataSource datasource = config.getDbSqlSessionFactory().getSqlSessionFactory().getConfiguration().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());
}
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)

Aggregations

PooledDataSource (org.apache.ibatis.datasource.pooled.PooledDataSource)8 Connection (java.sql.Connection)6 BaseDataTest (org.apache.ibatis.BaseDataTest)4 JDBCConnection (org.hsqldb.jdbc.JDBCConnection)4 Test (org.junit.Test)4 Properties (java.util.Properties)2 ProcessEngineConfigurationImpl (org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 DataSource (javax.sql.DataSource)1 ProcessEngine (org.activiti.engine.ProcessEngine)1 Ignore (org.junit.Ignore)1