Пример #1
0
  @Test
  public void test2PoolCleaners() throws Exception {
    datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(2000);
    datasource.getPoolProperties().setTestWhileIdle(true);

    DataSource ds2 = new DataSource(datasource.getPoolProperties());

    Assert.assertEquals(
        "Pool cleaner should not be started yet.", 0, ConnectionPool.getPoolCleaners().size());
    Assert.assertNull("Pool timer should be null", ConnectionPool.getPoolTimer());
    Assert.assertEquals(
        "Pool cleaner threads should not be present.", 0, countPoolCleanerThreads());

    datasource.getConnection().close();
    ds2.getConnection().close();
    Assert.assertEquals(
        "Pool cleaner should have 2 cleaner.", 2, ConnectionPool.getPoolCleaners().size());
    Assert.assertNotNull("Pool timer should not be null", ConnectionPool.getPoolTimer());
    Assert.assertEquals("Pool cleaner threads should be 1.", 1, countPoolCleanerThreads());

    datasource.close();
    Assert.assertEquals(
        "Pool cleaner should have 1 cleaner.", 1, ConnectionPool.getPoolCleaners().size());
    Assert.assertNotNull("Pool timer should not be null", ConnectionPool.getPoolTimer());

    ds2.close();
    Assert.assertEquals(
        "Pool shutdown, no cleaners should be present.",
        0,
        ConnectionPool.getPoolCleaners().size());
    Assert.assertNull("Pool timer should be null after shutdown", ConnectionPool.getPoolTimer());
    Assert.assertEquals(
        "Pool cleaner threads should not be present after close.", 0, countPoolCleanerThreads());
  }
Пример #2
0
  public static Connection connectMySql() {
    Connection conn = null;
    try {
      conn = datasource.getConnection();

    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return conn;
  }
Пример #3
0
 @SuppressWarnings("unused")
 private static Connection createTomcatJdbcPoolWrappedConnection() throws SQLException {
   // set up database
   DataSource ds = new DataSource();
   ds.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");
   ds.setUrl("jdbc:hsqldb:mem:test");
   ds.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.StatementDecoratorInterceptor");
   Connection connection = ds.getConnection();
   insertRecords(connection);
   return connection;
 }