org.apache.tomcat.jdbc.pool.PoolConfiguration Java Examples

The following examples show how to use org.apache.tomcat.jdbc.pool.PoolConfiguration. 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: DefaultTestCase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
public org.apache.tomcat.jdbc.pool.DataSource createDefaultDataSource() {
    org.apache.tomcat.jdbc.pool.DataSource datasource = null;
    PoolConfiguration p = new DefaultProperties();
    p.setFairQueue(false);
    p.setJmxEnabled(false);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(false);
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(threadcount);
    p.setInitialSize(threadcount);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(10);
    p.setMinEvictableIdleTimeMillis(10000);
    p.setMinIdle(threadcount);
    p.setLogAbandoned(false);
    p.setRemoveAbandoned(false);
    datasource = new org.apache.tomcat.jdbc.pool.DataSource();
    datasource.setPoolProperties(p);
    return datasource;
}
 
Example #2
Source File: TomcatJdbcDataSourceFactory.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Override
public org.apache.tomcat.jdbc.pool.DataSource createDataSource(
		DataSourceInformation dataSourceInformation) {
	// create a method scoped instance
	PoolConfiguration configurationToUse = new PoolProperties();

	// copy all general properties
	BeanUtils.copyProperties(this, configurationToUse);

	configurationToUse.setDriverClassName(this.databasePlatformSupport
			.getDriverClassNameForDatabase(dataSourceInformation.getDatabaseType()));
	configurationToUse.setUrl(this.databasePlatformSupport.getDatabaseUrlForDatabase(
			dataSourceInformation.getDatabaseType(),
			dataSourceInformation.getHostName(), dataSourceInformation.getPort(),
			dataSourceInformation.getDatabaseName()));
	configurationToUse.setUsername(dataSourceInformation.getUserName());
	configurationToUse.setPassword(dataSourceInformation.getPassword());

	return new org.apache.tomcat.jdbc.pool.DataSource(configurationToUse);
}
 
Example #3
Source File: TestSizePreservation.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
private void initSimplePoolProperties() {
    PoolConfiguration p = new DefaultProperties();
    ds = new org.apache.tomcat.jdbc.pool.DataSource();
    ds.setPoolProperties(p);

    ds.getPoolProperties().setDriverClassName(Driver.class.getName());
    ds.getPoolProperties().setUrl(Driver.url);
    ds.getPoolProperties().setFairQueue(true);
    ds.getPoolProperties().setJmxEnabled(false);
    ds.getPoolProperties().setTestWhileIdle(true);
    ds.getPoolProperties().setTestOnBorrow(false);
    ds.getPoolProperties().setTestOnReturn(false);
    ds.getPoolProperties().setValidationInterval(30000);
    ds.getPoolProperties().setTimeBetweenEvictionRunsMillis(30000);
    ds.getPoolProperties().setInitialSize(100);
    ds.getPoolProperties().setMaxActive(100);
    ds.getPoolProperties().setMinIdle(0);
    ds.getPoolProperties().setMaxIdle(0);
    ds.getPoolProperties().setMaxWait(10000);
    ds.getPoolProperties().setRemoveAbandonedTimeout(10);
    ds.getPoolProperties().setMinEvictableIdleTimeMillis(10000);
    ds.getPoolProperties().setLogAbandoned(false);
    ds.getPoolProperties().setRemoveAbandoned(false);
    ds.getPoolProperties().setUseLock(true);
}
 
Example #4
Source File: DefaultTestCase.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
public org.apache.tomcat.jdbc.pool.DataSource createDefaultDataSource() {
    org.apache.tomcat.jdbc.pool.DataSource datasource = null;
    PoolConfiguration p = new DefaultProperties();
    p.setFairQueue(false);
    p.setJmxEnabled(false);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(false);
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(threadcount);
    p.setInitialSize(threadcount);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(10);
    p.setMinEvictableIdleTimeMillis(10000);
    p.setMinIdle(threadcount);
    p.setLogAbandoned(false);
    p.setRemoveAbandoned(false);
    datasource = new org.apache.tomcat.jdbc.pool.DataSource();
    datasource.setPoolProperties(p);
    return datasource;
}
 
Example #5
Source File: TestSizePreservation.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private void initSimplePoolProperties() {
    PoolConfiguration p = new DefaultProperties();
    ds = new org.apache.tomcat.jdbc.pool.DataSource();
    ds.setPoolProperties(p);

    ds.getPoolProperties().setDriverClassName(Driver.class.getName());
    ds.getPoolProperties().setUrl(Driver.url);
    ds.getPoolProperties().setFairQueue(true);
    ds.getPoolProperties().setJmxEnabled(false);
    ds.getPoolProperties().setTestWhileIdle(true);
    ds.getPoolProperties().setTestOnBorrow(false);
    ds.getPoolProperties().setTestOnReturn(false);
    ds.getPoolProperties().setValidationInterval(30000);
    ds.getPoolProperties().setTimeBetweenEvictionRunsMillis(30000);
    ds.getPoolProperties().setInitialSize(100);
    ds.getPoolProperties().setMaxActive(100);
    ds.getPoolProperties().setMinIdle(0);
    ds.getPoolProperties().setMaxIdle(0);
    ds.getPoolProperties().setMaxWait(10000);
    ds.getPoolProperties().setRemoveAbandonedTimeout(10);
    ds.getPoolProperties().setMinEvictableIdleTimeMillis(10000);
    ds.getPoolProperties().setLogAbandoned(false);
    ds.getPoolProperties().setRemoveAbandoned(false);
    ds.getPoolProperties().setUseLock(true);
}
 
Example #6
Source File: DefaultTestCase.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
public org.apache.tomcat.jdbc.pool.DataSource createDefaultDataSource() {
    org.apache.tomcat.jdbc.pool.DataSource datasource = null;
    PoolConfiguration p = new DefaultProperties();
    p.setFairQueue(false);
    p.setJmxEnabled(false);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(false);
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(threadcount);
    p.setInitialSize(threadcount);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(10);
    p.setMinEvictableIdleTimeMillis(10000);
    p.setMinIdle(threadcount);
    p.setLogAbandoned(false);
    p.setRemoveAbandoned(false);
    datasource = new org.apache.tomcat.jdbc.pool.DataSource();
    datasource.setPoolProperties(p);
    return datasource;
}
 
Example #7
Source File: TomcatDataSourceService.java    From qmq with Apache License 2.0 6 votes vote down vote up
@Override
public DataSource makeDataSource(String url, String driverClassName, String username, String pwd) {
    PoolConfiguration p = new PoolProperties();
    p.setMinIdle(0);
    p.setMaxActive(2);
    p.setMaxIdle(0);
    p.setInitialSize(0);
    p.setMaxWait(10000);
    p.setDriverClassName(driverClassName);
    p.setUrl(url);
    p.setUsername(username);
    p.setPassword(pwd);
    p.setValidationQuery("select 1");
    p.setTestOnBorrow(true);

    org.apache.tomcat.jdbc.pool.DataSource datasource = new org.apache.tomcat.jdbc.pool.DataSource();
    datasource.setPoolProperties(p);
    return datasource;
}
 
Example #8
Source File: DBPropertiesTestCase.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Test
public void modifyDataSourceConfig() throws Exception {
    String fileName = this.getClass().getClassLoader().getResource("master-datasources.xml").getPath();
    try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
        }
        String everything = sb.toString();
        RDBMSConfiguration configuration = RDBMSDataSourceReader.loadConfig(everything);
        PoolConfiguration poolConfiguration = RDBMSDataSourceUtils.createPoolConfiguration(configuration);
        Assert.assertEquals("true", poolConfiguration.getDbProperties().getProperty("SetFloatAndDoubleUseBinary"));
    }
}
 
Example #9
Source File: TestSizePreservation.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void initSimplePoolProperties() {
    PoolConfiguration p = new DefaultProperties();
    ds = new org.apache.tomcat.jdbc.pool.DataSource();
    ds.setPoolProperties(p);

    ds.getPoolProperties().setDriverClassName(Driver.class.getName());
    ds.getPoolProperties().setUrl(Driver.url);
    ds.getPoolProperties().setFairQueue(true);
    ds.getPoolProperties().setJmxEnabled(false);
    ds.getPoolProperties().setTestWhileIdle(true);
    ds.getPoolProperties().setTestOnBorrow(false);
    ds.getPoolProperties().setTestOnReturn(false);
    ds.getPoolProperties().setValidationInterval(30000);
    ds.getPoolProperties().setTimeBetweenEvictionRunsMillis(30000);
    ds.getPoolProperties().setInitialSize(100);
    ds.getPoolProperties().setMaxActive(100);
    ds.getPoolProperties().setMinIdle(0);
    ds.getPoolProperties().setMaxIdle(0);
    ds.getPoolProperties().setMaxWait(10000);
    ds.getPoolProperties().setRemoveAbandonedTimeout(10);
    ds.getPoolProperties().setMinEvictableIdleTimeMillis(10000);
    ds.getPoolProperties().setLogAbandoned(false);
    ds.getPoolProperties().setRemoveAbandoned(false);
    ds.getPoolProperties().setUseLock(true);
}
 
Example #10
Source File: DBPropertiesTestCase.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyDBConfig() throws DataSourceException {
    RDBMSConfiguration configuration = new RDBMSConfiguration();
    List<RDBMSConfiguration.DataSourceProperty> propertyList = new ArrayList<>();
    RDBMSConfiguration.DataSourceProperty property = new RDBMSConfiguration.DataSourceProperty();
    property.setName("SetFloatAndDoubleUseBinary");
    property.setValue("true");
    propertyList.add(property);
    configuration.setDatabaseProps(propertyList);
    PoolConfiguration poolConfiguration = RDBMSDataSourceUtils.createPoolConfiguration(configuration);
    Assert.assertEquals("true", poolConfiguration.getDbProperties().getProperty("SetFloatAndDoubleUseBinary"));
}
 
Example #11
Source File: SimplePOJOExample.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    PoolConfiguration p = new PoolProperties();
    p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true");
    p.setDriverClassName("com.mysql.jdbc.Driver");
    p.setUsername("root");
    p.setPassword("password");
    p.setJmxEnabled(true);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setValidationQuery("SELECT 1");
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(100);
    p.setInitialSize(10);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(60);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setMinIdle(10);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    p.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    DataSource datasource = new DataSource();
    datasource.setPoolProperties(p);

    Connection con = null;
    try {
      con = datasource.getConnection();
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery("select * from user");
      int cnt = 1;
      while (rs.next()) {
          System.out.println((cnt++)+". Host:" +rs.getString("Host")+" User:"+rs.getString("User")+" Password:"+rs.getString("Password"));
      }
      rs.close();
      st.close();
    } finally {
      if (con!=null) try {con.close();}catch (Exception ignore) {}
    }
}
 
Example #12
Source File: TomEEDataSourceCreator.java    From tomee with Apache License 2.0 5 votes vote down vote up
private static PoolConfiguration readOnly(final PoolConfiguration pool) {
    try {
        return (PoolConfiguration) Proxy.newProxyInstance(TomEEDataSourceCreator.class.getClassLoader(), CONNECTION_POOL_CLASS, new ReadOnlyConnectionpool(pool));
    } catch (final Throwable e) {
        return (PoolConfiguration) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), CONNECTION_POOL_CLASS, new ReadOnlyConnectionpool(pool));
    }
}
 
Example #13
Source File: TomEEDataSourceCreator.java    From tomee with Apache License 2.0 5 votes vote down vote up
public TomEEDataSource(final PoolConfiguration poolConfiguration, final String name) {
    super(readOnly(poolConfiguration));
    try { // just to force the pool to be created and be able to register the mbean
        createPool();
        initJmx(name);
    } catch (final Throwable e) {
        LOGGER.error("Can't create DataSource", e);
    }
    this.name = name;
}
 
Example #14
Source File: TomEEDataSourceCreator.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public CommonDataSource pool(final String name, final String driver, final Properties properties) {
    final PoolConfiguration config = build(TomEEPoolProperties.class, createProperties(name, properties));
    final TomEEDataSource ds = new TomEEDataSource(config, name);
    recipes.put(ds, recipes.remove(config));
    return ds;
}
 
Example #15
Source File: SimplePOJOExample.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    PoolConfiguration p = new PoolProperties();
    p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true");
    p.setDriverClassName("com.mysql.jdbc.Driver");
    p.setUsername("root");
    p.setPassword("password");
    p.setJmxEnabled(true);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setValidationQuery("SELECT 1");
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(100);
    p.setInitialSize(10);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(60);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setMinIdle(10);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    p.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    DataSource datasource = new DataSource();
    datasource.setPoolProperties(p);

    Connection con = null;
    try {
      con = datasource.getConnection();
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery("select * from user");
      int cnt = 1;
      while (rs.next()) {
          System.out.println((cnt++)+". Host:" +rs.getString("Host")+" User:"+rs.getString("User")+" Password:"+rs.getString("Password"));
      }
      rs.close();
      st.close();
    } finally {
      if (con!=null) try {con.close();}catch (Exception ignore) {}
    }
}
 
Example #16
Source File: SimplePOJOAsyncExample.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    PoolConfiguration p = new PoolProperties();
    p.setFairQueue(true);
    p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true");
    p.setDriverClassName("com.mysql.jdbc.Driver");
    p.setUsername("root");
    p.setPassword("password");
    p.setJmxEnabled(true);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setValidationQuery("SELECT 1");
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(100);
    p.setInitialSize(10);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(60);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setMinIdle(10);
    p.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    DataSource datasource = new DataSource();
    datasource.setPoolProperties(p);

    Connection con = null;
    try {
        Future<Connection> future = datasource.getConnectionAsync();
        while (!future.isDone()) {
            System.out.println("Connection is not yet available. Do some background work");
        try {
            Thread.sleep(100); //simulate work
            } catch (InterruptedException x) {
                Thread.interrupted();
            }
        }
        con = future.get(); //should return instantly
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from user");
        int cnt = 1;
        while (rs.next()) {
            System.out.println((cnt++)+". Host:" +rs.getString("Host")+" User:"+rs.getString("User")+" Password:"+rs.getString("Password"));
        }
        rs.close();
        st.close();
    } finally {
        if (con!=null) {
            try {
                con.close();
            } catch (Exception ignore) {
                // Ignore
            }
        }
    }
}
 
Example #17
Source File: TomEEDataSourceCreator.java    From tomee with Apache License 2.0 4 votes vote down vote up
public TomEEConnectionPool(final PoolConfiguration poolProperties, final ClassLoader creationLoader) throws SQLException {
    super(poolProperties);
    this.creationLoader = creationLoader;
}
 
Example #18
Source File: TomEEDataSourceCreator.java    From tomee with Apache License 2.0 4 votes vote down vote up
public ReadOnlyConnectionpool(final PoolConfiguration pool) {
    this.delegate = pool;
}
 
Example #19
Source File: TomEEDataSourceCreator.java    From tomee with Apache License 2.0 4 votes vote down vote up
public TomEEDataSource(final PoolConfiguration properties, final ConnectionPool pool, final String name) {
    super(readOnly(properties));
    this.pool = pool;
    initJmx(name);
    this.name = name;
}
 
Example #20
Source File: ConnectionPool.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public PoolConfiguration getPoolProperties() {
    return pool.getPoolProperties();
}
 
Example #21
Source File: SimplePOJOExample.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    PoolConfiguration p = new PoolProperties();
    p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true");
    p.setDriverClassName("com.mysql.jdbc.Driver");
    p.setUsername("root");
    p.setPassword("password");
    p.setJmxEnabled(true);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setValidationQuery("SELECT 1");
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(100);
    p.setInitialSize(10);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(60);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setMinIdle(10);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    p.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    DataSource datasource = new DataSource();
    datasource.setPoolProperties(p);

    Connection con = null;
    try {
        con = datasource.getConnection();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from user");
        int cnt = 1;
        while (rs.next()) {
            System.out.println((cnt++)+". Host:" +rs.getString("Host")+" User:"+rs.getString("User")+" Password:"+rs.getString("Password"));
        }
        rs.close();
        st.close();
    } finally {
        if (con!=null) {
            try {
                con.close();
            } catch (Exception ignore) {
                // Ignore
            }
        }
    }
}
 
Example #22
Source File: SimplePOJOAsyncExample.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    PoolConfiguration p = new PoolProperties();
    p.setFairQueue(true);
    p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true");
    p.setDriverClassName("com.mysql.jdbc.Driver");
    p.setUsername("root");
    p.setPassword("password");
    p.setJmxEnabled(true);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setValidationQuery("SELECT 1");
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(100);
    p.setInitialSize(10);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(60);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setMinIdle(10);
    p.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    DataSource datasource = new DataSource();
    datasource.setPoolProperties(p);

    Connection con = null;
    try {
      Future<Connection> future = datasource.getConnectionAsync();
      while (!future.isDone()) {
          System.out.println("Connection is not yet available. Do some background work");
          try {
              Thread.sleep(100); //simulate work
          }catch (InterruptedException x) {
              Thread.interrupted();
          }
      }
      con = future.get(); //should return instantly
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery("select * from user");
      int cnt = 1;
      while (rs.next()) {
          System.out.println((cnt++)+". Host:" +rs.getString("Host")+" User:"+rs.getString("User")+" Password:"+rs.getString("Password"));
      }
      rs.close();
      st.close();
    } finally {
      if (con!=null) try {con.close();}catch (Exception ignore) {}
    }
}
 
Example #23
Source File: DalTomcatDataSource.java    From das with Apache License 2.0 4 votes vote down vote up
public DalTomcatDataSource(PoolConfiguration poolProperties) {
    super(poolProperties);
}
 
Example #24
Source File: ConnectionPool.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public PoolConfiguration getPoolProperties() {
    return pool.getPoolProperties();
}
 
Example #25
Source File: DalConnectionPool.java    From dal with Apache License 2.0 4 votes vote down vote up
public DalConnectionPool(PoolConfiguration prop) throws SQLException {
    super(prop);
}
 
Example #26
Source File: DalTomcatDataSource.java    From dal with Apache License 2.0 4 votes vote down vote up
public DalTomcatDataSource(PoolConfiguration poolProperties) {
    super(poolProperties);
}
 
Example #27
Source File: SimplePOJOAsyncExample.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    PoolConfiguration p = new PoolProperties();
    p.setFairQueue(true);
    p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true");
    p.setDriverClassName("com.mysql.jdbc.Driver");
    p.setUsername("root");
    p.setPassword("password");
    p.setJmxEnabled(true);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setValidationQuery("SELECT 1");
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(100);
    p.setInitialSize(10);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(60);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setMinIdle(10);
    p.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    DataSource datasource = new DataSource();
    datasource.setPoolProperties(p);

    Connection con = null;
    try {
      Future<Connection> future = datasource.getConnectionAsync();
      while (!future.isDone()) {
          System.out.println("Connection is not yet available. Do some background work");
          try {
              Thread.sleep(100); //simulate work
          }catch (InterruptedException x) {
              Thread.interrupted();
          }
      }
      con = future.get(); //should return instantly
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery("select * from user");
      int cnt = 1;
      while (rs.next()) {
          System.out.println((cnt++)+". Host:" +rs.getString("Host")+" User:"+rs.getString("User")+" Password:"+rs.getString("Password"));
      }
      rs.close();
      st.close();
    } finally {
      if (con!=null) try {con.close();}catch (Exception ignore) {}
    }
}
 
Example #28
Source File: ConnectionPool.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
public PoolConfiguration getPoolProperties() {
    return pool.getPoolProperties();
}
 
Example #29
Source File: DalConnectionPool.java    From das with Apache License 2.0 4 votes vote down vote up
public DalConnectionPool(PoolConfiguration prop) throws SQLException {
    super(prop);
}