Search in sources :

Example 1 with PoolConfiguration

use of org.apache.tomcat.jdbc.pool.PoolConfiguration in project tomcat by apache.

the class SimplePOJOExample method main.

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
            }
        }
    }
}
Also used : PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource)

Example 2 with PoolConfiguration

use of org.apache.tomcat.jdbc.pool.PoolConfiguration in project tomcat by apache.

the class ConnectionState method reset.

@Override
public void reset(ConnectionPool parent, PooledConnection con) {
    if (parent == null || con == null) {
        //we are resetting, reset our defaults
        autoCommit = null;
        transactionIsolation = null;
        readOnly = null;
        catalog = null;
        return;
    }
    PoolConfiguration poolProperties = parent.getPoolProperties();
    if (poolProperties.getDefaultTransactionIsolation() != DataSourceFactory.UNKNOWN_TRANSACTIONISOLATION) {
        try {
            if (transactionIsolation == null || transactionIsolation.intValue() != poolProperties.getDefaultTransactionIsolation()) {
                con.getConnection().setTransactionIsolation(poolProperties.getDefaultTransactionIsolation());
                transactionIsolation = Integer.valueOf(poolProperties.getDefaultTransactionIsolation());
            }
        } catch (SQLException x) {
            transactionIsolation = null;
            log.error("Unable to reset transaction isolation state to connection.", x);
        }
    }
    if (poolProperties.getDefaultReadOnly() != null) {
        try {
            if (readOnly == null || readOnly.booleanValue() != poolProperties.getDefaultReadOnly().booleanValue()) {
                con.getConnection().setReadOnly(poolProperties.getDefaultReadOnly().booleanValue());
                readOnly = poolProperties.getDefaultReadOnly();
            }
        } catch (SQLException x) {
            readOnly = null;
            log.error("Unable to reset readonly state to connection.", x);
        }
    }
    if (poolProperties.getDefaultAutoCommit() != null) {
        try {
            if (autoCommit == null || autoCommit.booleanValue() != poolProperties.getDefaultAutoCommit().booleanValue()) {
                con.getConnection().setAutoCommit(poolProperties.getDefaultAutoCommit().booleanValue());
                autoCommit = poolProperties.getDefaultAutoCommit();
            }
        } catch (SQLException x) {
            autoCommit = null;
            log.error("Unable to reset autocommit state to connection.", x);
        }
    }
    if (poolProperties.getDefaultCatalog() != null) {
        try {
            if (catalog == null || (!catalog.equals(poolProperties.getDefaultCatalog()))) {
                con.getConnection().setCatalog(poolProperties.getDefaultCatalog());
                catalog = poolProperties.getDefaultCatalog();
            }
        } catch (SQLException x) {
            catalog = null;
            log.error("Unable to reset default catalog state to connection.", x);
        }
    }
}
Also used : PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) SQLException(java.sql.SQLException)

Example 3 with PoolConfiguration

use of org.apache.tomcat.jdbc.pool.PoolConfiguration in project tomcat by apache.

the class Bug51582 method main.

public static void main(String[] args) throws SQLException {
    org.apache.tomcat.jdbc.pool.DataSource datasource = null;
    PoolConfiguration p = new DefaultProperties();
    p.setJmxEnabled(true);
    p.setTestOnBorrow(false);
    p.setTestOnReturn(false);
    p.setValidationInterval(1000);
    p.setTimeBetweenEvictionRunsMillis(2000);
    p.setMaxWait(2000);
    p.setMinEvictableIdleTimeMillis(1000);
    datasource = new org.apache.tomcat.jdbc.pool.DataSource();
    datasource.setPoolProperties(p);
    datasource.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx(threshold=200)");
    ConnectionPool pool = datasource.createPool();
    Connection con = pool.getConnection();
    Statement st = con.createStatement();
    try {
        st.execute("DROP ALIAS SLEEP");
    } catch (Exception ignore) {
    // Ignore
    }
    st.execute("CREATE ALIAS SLEEP AS $$\nboolean sleep() {\n        try {\n            Thread.sleep(10000);\n            return true;        } catch (Exception x) {\n            return false;\n        }\n}\n$$;");
    st.close();
    con.close();
    int iter = 0;
    while ((iter++) < 10) {
        final Connection connection = pool.getConnection();
        final CallableStatement s = connection.prepareCall("{CALL SLEEP()}");
        List<Thread> threadList = new ArrayList<>();
        for (int l = 0; l < 3; l++) {
            final int i = l;
            Thread thread = new Thread() {

                @Override
                public void run() {
                    try {
                        if (i == 0) {
                            Thread.sleep(1000);
                            s.cancel();
                        } else if (i == 1) {
                            // or use some other statement which will block
                            // for a longer time
                            long start = System.currentTimeMillis();
                            System.out.println("[" + getName() + "] Calling SP SLEEP");
                            s.execute();
                            System.out.println("[" + getName() + "] Executed SP SLEEP [" + (System.currentTimeMillis() - start) + "]");
                        } else {
                            Thread.sleep(1000);
                            connection.close();
                        }
                    } catch (InterruptedException e) {
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            };
            threadList.add(thread);
            thread.start();
        }
        for (Thread t : threadList) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool) PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) DefaultProperties(org.apache.tomcat.jdbc.test.DefaultProperties) SQLException(java.sql.SQLException) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement)

Example 4 with PoolConfiguration

use of org.apache.tomcat.jdbc.pool.PoolConfiguration in project tomcat by apache.

the class SimplePOJOAsyncExample method main.

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 {
                //simulate work
                Thread.sleep(100);
            } catch (InterruptedException x) {
                Thread.interrupted();
            }
        }
        //should return instantly
        con = future.get();
        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
            }
        }
    }
}
Also used : PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource)

Example 5 with PoolConfiguration

use of org.apache.tomcat.jdbc.pool.PoolConfiguration in project tomcat by apache.

the class DefaultTestCase method createDefaultDataSource.

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;
}
Also used : PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration)

Aggregations

PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)9 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 Statement (java.sql.Statement)3 DataSource (org.apache.tomcat.jdbc.pool.DataSource)3 PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)3 ResultSet (java.sql.ResultSet)2 ConnectionPool (org.apache.tomcat.jdbc.pool.ConnectionPool)2 CallableStatement (java.sql.CallableStatement)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 DefaultProperties (org.apache.tomcat.jdbc.test.DefaultProperties)1 Driver (org.apache.tomcat.jdbc.test.driver.Driver)1