Search in sources :

Example 16 with PoolProperties

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

the class Bug54978 method testIllegalValidationQueryWithLegalInit.

@Test
public void testIllegalValidationQueryWithLegalInit() throws SQLException {
    PoolProperties poolProperties = new DefaultProperties();
    poolProperties.setMinIdle(0);
    poolProperties.setInitialSize(1);
    poolProperties.setMaxActive(1);
    poolProperties.setMaxWait(5000);
    poolProperties.setMaxAge(100);
    poolProperties.setRemoveAbandoned(false);
    poolProperties.setTestOnBorrow(true);
    poolProperties.setTestOnConnect(false);
    poolProperties.setValidationQuery("sdadsada");
    poolProperties.setInitSQL("SELECT 1");
    final DataSource ds = new DataSource(poolProperties);
    ds.getConnection().close();
}
Also used : DefaultProperties(org.apache.tomcat.jdbc.test.DefaultProperties) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource) Test(org.junit.Test)

Example 17 with PoolProperties

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

the class Bug54978 method testIllegalValidationQuery.

@Test
public void testIllegalValidationQuery() {
    PoolProperties poolProperties = new DefaultProperties();
    poolProperties.setMinIdle(0);
    poolProperties.setInitialSize(1);
    poolProperties.setMaxActive(1);
    poolProperties.setMaxWait(5000);
    poolProperties.setMaxAge(100);
    poolProperties.setRemoveAbandoned(false);
    poolProperties.setTestOnBorrow(true);
    poolProperties.setTestOnConnect(false);
    poolProperties.setValidationQuery("sdadsada");
    final DataSource ds = new DataSource(poolProperties);
    try {
        ds.getConnection().close();
        fail("Validation should have failed.");
    } catch (SQLException x) {
    }
}
Also used : DefaultProperties(org.apache.tomcat.jdbc.test.DefaultProperties) SQLException(java.sql.SQLException) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource) Test(org.junit.Test)

Example 18 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties 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 19 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project platformlayer by platformlayer.

the class TomcatJdbcPoolDataSourceBuilder method buildDataSource.

@Override
public DataSource buildDataSource(String key, JdbcConfiguration jdbcConfig) {
    PoolProperties p = new PoolProperties();
    if (jdbcConfig.driverClassName != null) {
        p.setDriverClassName(jdbcConfig.driverClassName);
    }
    log.warn("Building data source for " + jdbcConfig.jdbcUrl);
    p.setUrl(jdbcConfig.jdbcUrl);
    p.setUsername(jdbcConfig.username);
    p.setPassword(jdbcConfig.password);
    String sqlDebug = null;
    if (jdbcConfig.extraProperties != null) {
        sqlDebug = jdbcConfig.extraProperties.get("sql.debug");
    }
    Properties jdbcProperties = buildDbProperties(jdbcConfig);
    if (!jdbcProperties.isEmpty()) {
        try {
            p.setDbProperties(jdbcProperties);
        } catch (Exception e) {
            throw new IllegalStateException("Unable to set JDBC properties", e);
        }
    }
    if (!Strings.isNullOrEmpty(sqlDebug)) {
        throw new UnsupportedOperationException();
    // pooledDataSource.setLogStatementsEnabled(Boolean.parseBoolean(sqlDebug));
    }
    p.setMinIdle(1);
    p.setInitialSize(1);
    List<String> interceptors = Lists.newArrayList();
    interceptors.add("org.apache.tomcat.jdbc.pool.interceptor.StatementCache");
    if (!interceptors.isEmpty()) {
        p.setJdbcInterceptors(Joiner.on(';').join(interceptors));
    }
    // p.setJmxEnabled(true);
    // p.setTestWhileIdle(false);
    // p.setTestOnBorrow(true);
    // p.setValidationQuery("SELECT 1");
    // p.setTestOnReturn(true);
    // 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");
    org.apache.tomcat.jdbc.pool.DataSource pooledDataSource = new org.apache.tomcat.jdbc.pool.DataSource();
    pooledDataSource.setPoolProperties(p);
    databaseStatistics.register(key, pooledDataSource, new TomcatJdbcPoolMetricsReporter(databaseStatistics.getMetricKey(key), pooledDataSource));
    return pooledDataSource;
}
Also used : Properties(java.util.Properties) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) TomcatJdbcPoolMetricsReporter(org.platformlayer.metrics.TomcatJdbcPoolMetricsReporter) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(javax.sql.DataSource)

Example 20 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project sling by apache.

the class DataSourceFactory method createPoolConfig.

private PoolConfiguration createPoolConfig(Map<String, ?> config) {
    Properties props = new Properties();
    //Copy the other properties first
    Map<String, String> otherProps = PropertiesUtil.toMap(config.get(PROP_DATASOURCE_SVC_PROPS), new String[0]);
    for (Map.Entry<String, String> e : otherProps.entrySet()) {
        set(e.getKey(), e.getValue(), props);
    }
    props.setProperty(org.apache.tomcat.jdbc.pool.DataSourceFactory.OBJECT_NAME, name);
    for (String propName : DummyDataSourceFactory.getPropertyNames()) {
        String value = PropertiesUtil.toString(config.get(propName), null);
        set(propName, value, props);
    }
    //Specify the DataSource such that connection creation logic is handled
    //by us where we take care of OSGi env
    PoolConfiguration poolProperties = org.apache.tomcat.jdbc.pool.DataSourceFactory.parsePoolProperties(props);
    poolProperties.setDataSource(createDriverDataSource(poolProperties));
    return poolProperties;
}
Also used : PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) Properties(java.util.Properties) Map(java.util.Map)

Aggregations

PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)26 DataSource (org.apache.tomcat.jdbc.pool.DataSource)10 Test (org.junit.Test)10 Connection (java.sql.Connection)6 DefaultProperties (org.apache.tomcat.jdbc.test.DefaultProperties)5 Properties (java.util.Properties)4 PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)4 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 DataSource (javax.sql.DataSource)3 InterceptorDefinition (org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorDefinition)3 TrapException (org.apache.tomcat.jdbc.pool.TrapException)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ConnectionPool (org.apache.tomcat.jdbc.pool.ConnectionPool)2 InterceptorProperty (org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorProperty)2