Java Code Examples for org.apache.tomcat.jdbc.pool.DataSource#setPoolProperties()

The following examples show how to use org.apache.tomcat.jdbc.pool.DataSource#setPoolProperties() . 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: PooledConnectionTomcat.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
private static void setupDataSource(Isolation isolation) {
  ds = new DataSource();

  try {    
    ds.setPoolProperties(getPoolProperties()); //set earlier to avoid overriding the isolation setting
    
    ds.setMaxActive(100);
    
    if (isolation == Isolation.NONE) {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
    } else if (isolation == Isolation.READ_COMMITTED) {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
      ds.setDefaultAutoCommit(false);
    } else {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
      ds.setDefaultAutoCommit(false);
    }
    
    dsSet = true;
    Log.getLogWriter().info("tomcat data source url is set as " + ds.getUrl());
    Log.getLogWriter().info("tomcat data source DefaultTransactionIsolation is set to " + ds.getDefaultTransactionIsolation());
    Log.getLogWriter().info("tomcat data source DefaultAutoCommit is set to " + ds.getDefaultAutoCommit());
  } catch (Exception e) {
    throw new TestException("could not set data source" + TestHelper.getStackTrace(e));
  }
}
 
Example 2
Source File: PooledConnectionTomcat.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
private static void setupDataSource(Isolation isolation) {
  ds = new DataSource();

  try {    
    ds.setPoolProperties(getPoolProperties()); //set earlier to avoid overriding the isolation setting
    
    ds.setMaxActive(100);
    
    if (isolation == Isolation.NONE) {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
    } else if (isolation == Isolation.READ_COMMITTED) {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
      ds.setDefaultAutoCommit(false);
    } else {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
      ds.setDefaultAutoCommit(false);
    }
    
    dsSet = true;
    Log.getLogWriter().info("tomcat data source url is set as " + ds.getUrl());
    Log.getLogWriter().info("tomcat data source DefaultTransactionIsolation is set to " + ds.getDefaultTransactionIsolation());
    Log.getLogWriter().info("tomcat data source DefaultAutoCommit is set to " + ds.getDefaultAutoCommit());
  } catch (Exception e) {
    throw new TestException("could not set data source" + TestHelper.getStackTrace(e));
  }
}
 
Example 3
Source File: PerfTestProcessEngine.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected static javax.sql.DataSource createDatasource(Properties properties) {

    PoolProperties p = new PoolProperties();
    p.setUrl(properties.getProperty("databaseUrl"));
    p.setDriverClassName(properties.getProperty("databaseDriver"));
    p.setUsername(properties.getProperty("databaseUser"));
    p.setPassword(properties.getProperty("databasePassword"));

    p.setJmxEnabled(false);

    p.setMaxActive(100);
    p.setInitialSize(10);

    DataSource datasource = new DataSource();
    datasource.setPoolProperties(p);

    return datasource;
  }
 
Example 4
Source File: DFDbUtil.java    From dfactor with MIT License 5 votes vote down vote up
public static DataSource createMysqlDbSource(String url, String user, String pwd, 
		int initSize, int maxActive, int maxWait, int maxIdle, int minIdle){
	PoolProperties p = new PoolProperties();
	p.setDriverClassName("com.mysql.jdbc.Driver");
    p.setUrl(url);
    p.setUsername(user);
    p.setPassword(pwd);
    p.setJmxEnabled(true);
    p.setTestWhileIdle(true);
    p.setTestOnBorrow(false);
    p.setTestOnReturn(false);
    p.setValidationQuery("SELECT 1");
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(maxActive);
    p.setInitialSize(initSize);
    p.setMaxWait(maxWait);   //conn timeout 
    p.setRemoveAbandonedTimeout(60);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setMaxIdle(maxIdle);
    p.setMinIdle(minIdle);
    p.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
      "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    final DataSource dbSrc = new DataSource();
    dbSrc.setPoolProperties(p);
    return dbSrc;
}
 
Example 5
Source File: TestTomcatJdbcPoolBehavior.java    From aceql-http with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void createPool() throws Exception {
// Modify this code in order to extract a Connection from your
// connection pooling system:

// Driver parameters to use:
String driverClassName = "org.postgresql.Driver";
String url = "jdbc:postgresql://localhost:5432/sampledb";
String username = "user1";
String password = "password1";

// Creating the DataSource bean and populating the values:
// Mandatory values to set
PoolProperties p = new PoolProperties();
p.setDriverClassName(driverClassName);
p.setUrl(url);
p.setUsername(username);
p.setPassword(password);

// Other possible values to set
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(2);
p.setInitialSize(2);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(2);
p.setMaxIdle(2);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setJdbcInterceptors("ConnectionState;StatementFinalizer");

dataSource = new DataSource();
dataSource.setPoolProperties(p);

   }
 
Example 6
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 7
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 8
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 9
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 10
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 11
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) {}
    }
}