Search in sources :

Example 1 with PoolablePreparedStatement

use of org.datanucleus.store.rdbms.datasource.dbcp2.PoolablePreparedStatement in project tomcat by apache.

the class DriverAdapterCPDS method getPooledConnection.

/**
     * Attempt to establish a database connection.
     * @param username name to be used for the connection
     * @param pass password to be used fur the connection
     */
@Override
public PooledConnection getPooledConnection(final String username, final String pass) throws SQLException {
    getConnectionCalled = true;
    PooledConnectionImpl pci = null;
    // exception upon first invocation.
    try {
        if (connectionProperties != null) {
            connectionProperties.put("user", username);
            connectionProperties.put("password", pass);
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), connectionProperties));
        } else {
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), username, pass));
        }
        pci.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    } catch (final ClassCircularityError e) {
        if (connectionProperties != null) {
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), connectionProperties));
        } else {
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), username, pass));
        }
        pci.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    }
    KeyedObjectPool<PStmtKeyCPDS, PoolablePreparedStatement<PStmtKeyCPDS>> stmtPool = null;
    if (isPoolPreparedStatements()) {
        final GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
        config.setMaxTotalPerKey(Integer.MAX_VALUE);
        config.setBlockWhenExhausted(false);
        config.setMaxWaitMillis(0);
        config.setMaxIdlePerKey(getMaxIdle());
        if (getMaxPreparedStatements() <= 0) {
            // since there is no limit, create a prepared statement pool with an eviction thread
            //  evictor settings are the same as the connection pool settings.
            config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
            config.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun());
            config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
        } else {
            // since there is limit, create a prepared statement pool without an eviction thread
            //  pool has LRU functionality so when the limit is reached, 15% of the pool is cleared.
            // see org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool.clearOldest method
            config.setMaxTotal(getMaxPreparedStatements());
            config.setTimeBetweenEvictionRunsMillis(-1);
            config.setNumTestsPerEvictionRun(0);
            config.setMinEvictableIdleTimeMillis(0);
        }
        stmtPool = new GenericKeyedObjectPool<>(pci, config);
        pci.setStatementPool(stmtPool);
    }
    return pci;
}
Also used : GenericKeyedObjectPoolConfig(org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig) PoolablePreparedStatement(org.apache.tomcat.dbcp.dbcp2.PoolablePreparedStatement)

Example 2 with PoolablePreparedStatement

use of org.datanucleus.store.rdbms.datasource.dbcp2.PoolablePreparedStatement in project datanucleus-rdbms by datanucleus.

the class DriverAdapterCPDS method getPooledConnection.

/**
 * Attempt to establish a database connection.
 * @param username name to be used for the connection
 * @param pass password to be used fur the connection
 */
@Override
public PooledConnection getPooledConnection(String username, String pass) throws SQLException {
    getConnectionCalled = true;
    PooledConnectionImpl pci = null;
    // exception upon first invocation.
    try {
        if (connectionProperties != null) {
            connectionProperties.put("user", username);
            connectionProperties.put("password", pass);
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), connectionProperties));
        } else {
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), username, pass));
        }
        pci.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    } catch (ClassCircularityError e) {
        if (connectionProperties != null) {
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), connectionProperties));
        } else {
            pci = new PooledConnectionImpl(DriverManager.getConnection(getUrl(), username, pass));
        }
        pci.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    }
    KeyedObjectPool<PStmtKeyCPDS, PoolablePreparedStatement<PStmtKeyCPDS>> stmtPool = null;
    if (isPoolPreparedStatements()) {
        GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
        config.setMaxTotalPerKey(Integer.MAX_VALUE);
        config.setBlockWhenExhausted(false);
        config.setMaxWaitMillis(0);
        config.setMaxIdlePerKey(getMaxIdle());
        if (getMaxPreparedStatements() <= 0) {
            // since there is no limit, create a prepared statement pool with an eviction thread
            // evictor settings are the same as the connection pool settings.
            config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
            config.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun());
            config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
        } else {
            // since there is limit, create a prepared statement pool without an eviction thread
            // pool has LRU functionality so when the limit is reached, 15% of the pool is cleared.
            // see org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPool.clearOldest method
            config.setMaxTotal(getMaxPreparedStatements());
            config.setTimeBetweenEvictionRunsMillis(-1);
            config.setNumTestsPerEvictionRun(0);
            config.setMinEvictableIdleTimeMillis(0);
        }
        stmtPool = new GenericKeyedObjectPool<>(pci, config);
        pci.setStatementPool(stmtPool);
    }
    return pci;
}
Also used : GenericKeyedObjectPoolConfig(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPoolConfig) PoolablePreparedStatement(org.datanucleus.store.rdbms.datasource.dbcp2.PoolablePreparedStatement)

Example 3 with PoolablePreparedStatement

use of org.datanucleus.store.rdbms.datasource.dbcp2.PoolablePreparedStatement in project datanucleus-rdbms by datanucleus.

the class PoolingConnection method makeObject.

/**
 * {@link KeyedPooledObjectFactory} method for creating {@link PoolablePreparedStatement}s or
 * {@link PoolableCallableStatement}s. The <code>stmtType</code> field in the key determines whether a
 * PoolablePreparedStatement or PoolableCallableStatement is created.
 *
 * @param key
 *            the key for the {@link PreparedStatement} to be created
 * @see #createKey(String, int, int, StatementType)
 */
@SuppressWarnings("resource")
@Override
public PooledObject<DelegatingPreparedStatement> makeObject(final PStmtKey key) throws Exception {
    if (null == key) {
        throw new IllegalArgumentException("Prepared statement key is null or invalid.");
    }
    if (key.getStmtType() == StatementType.PREPARED_STATEMENT) {
        final PreparedStatement statement = (PreparedStatement) key.createStatement(getDelegate());
        // Unable to find way to avoid this
        @SuppressWarnings({ "rawtypes", "unchecked" }) final PoolablePreparedStatement pps = new PoolablePreparedStatement(statement, key, pstmtPool, this);
        return new DefaultPooledObject<>(pps);
    }
    final CallableStatement statement = (CallableStatement) key.createStatement(getDelegate());
    final PoolableCallableStatement pcs = new PoolableCallableStatement(statement, key, pstmtPool, this);
    return new DefaultPooledObject<>(pcs);
}
Also used : DefaultPooledObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject) CallableStatement(java.sql.CallableStatement) PreparedStatement(java.sql.PreparedStatement)

Example 4 with PoolablePreparedStatement

use of org.datanucleus.store.rdbms.datasource.dbcp2.PoolablePreparedStatement in project datanucleus-rdbms by datanucleus.

the class PooledConnectionImpl method makeObject.

/**
 * My {@link KeyedPooledObjectFactory} method for creating {@link PreparedStatement}s.
 *
 * @param key
 *            The key for the {@link PreparedStatement} to be created.
 */
@SuppressWarnings("resource")
@Override
public PooledObject<DelegatingPreparedStatement> makeObject(final PStmtKey key) throws Exception {
    if (null == key) {
        throw new IllegalArgumentException("Prepared statement key is null or invalid.");
    }
    if (key.getStmtType() == StatementType.PREPARED_STATEMENT) {
        final PreparedStatement statement = (PreparedStatement) key.createStatement(connection);
        // Unable to find way to avoid this
        @SuppressWarnings({ "rawtypes", "unchecked" }) final PoolablePreparedStatement pps = new PoolablePreparedStatement(statement, key, pStmtPool, delegatingConnection);
        return new DefaultPooledObject<>(pps);
    }
    final CallableStatement statement = (CallableStatement) key.createStatement(connection);
    @SuppressWarnings("unchecked") final PoolableCallableStatement pcs = new PoolableCallableStatement(statement, key, pStmtPool, (DelegatingConnection<Connection>) delegatingConnection);
    return new DefaultPooledObject<>(pcs);
}
Also used : DefaultPooledObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject) PoolableCallableStatement(org.datanucleus.store.rdbms.datasource.dbcp2.PoolableCallableStatement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) DelegatingConnection(org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingConnection) PreparedStatement(java.sql.PreparedStatement) DelegatingPreparedStatement(org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingPreparedStatement) PoolablePreparedStatement(org.datanucleus.store.rdbms.datasource.dbcp2.PoolablePreparedStatement) PoolableCallableStatement(org.datanucleus.store.rdbms.datasource.dbcp2.PoolableCallableStatement) PoolablePreparedStatement(org.datanucleus.store.rdbms.datasource.dbcp2.PoolablePreparedStatement)

Example 5 with PoolablePreparedStatement

use of org.datanucleus.store.rdbms.datasource.dbcp2.PoolablePreparedStatement in project tomcat by apache.

the class PooledConnectionImpl method makeObject.

/**
 * My {@link KeyedPooledObjectFactory} method for creating {@link PreparedStatement}s.
 *
 * @param key
 *            The key for the {@link PreparedStatement} to be created.
 */
@Override
public PooledObject<DelegatingPreparedStatement> makeObject(final PStmtKey key) throws Exception {
    if (null == key) {
        throw new IllegalArgumentException("Prepared statement key is null or invalid.");
    }
    if (key.getStmtType() == StatementType.PREPARED_STATEMENT) {
        final PreparedStatement statement = (PreparedStatement) key.createStatement(connection);
        // Unable to find way to avoid this
        @SuppressWarnings({ "rawtypes", "unchecked" }) final PoolablePreparedStatement pps = new PoolablePreparedStatement(statement, key, pStmtPool, delegatingConnection);
        return new DefaultPooledObject<>(pps);
    }
    final CallableStatement statement = (CallableStatement) key.createStatement(connection);
    @SuppressWarnings("unchecked") final PoolableCallableStatement pcs = new PoolableCallableStatement(statement, key, pStmtPool, (DelegatingConnection<Connection>) delegatingConnection);
    return new DefaultPooledObject<>(pcs);
}
Also used : DefaultPooledObject(org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject) PoolableCallableStatement(org.apache.tomcat.dbcp.dbcp2.PoolableCallableStatement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) DelegatingConnection(org.apache.tomcat.dbcp.dbcp2.DelegatingConnection) DelegatingPreparedStatement(org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement) PreparedStatement(java.sql.PreparedStatement) PoolablePreparedStatement(org.apache.tomcat.dbcp.dbcp2.PoolablePreparedStatement) PoolableCallableStatement(org.apache.tomcat.dbcp.dbcp2.PoolableCallableStatement) PoolablePreparedStatement(org.apache.tomcat.dbcp.dbcp2.PoolablePreparedStatement)

Aggregations

CallableStatement (java.sql.CallableStatement)3 PreparedStatement (java.sql.PreparedStatement)3 Connection (java.sql.Connection)2 PooledConnection (javax.sql.PooledConnection)2 PoolablePreparedStatement (org.apache.tomcat.dbcp.dbcp2.PoolablePreparedStatement)2 PoolablePreparedStatement (org.datanucleus.store.rdbms.datasource.dbcp2.PoolablePreparedStatement)2 DefaultPooledObject (org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject)2 DelegatingConnection (org.apache.tomcat.dbcp.dbcp2.DelegatingConnection)1 DelegatingPreparedStatement (org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement)1 PoolableCallableStatement (org.apache.tomcat.dbcp.dbcp2.PoolableCallableStatement)1 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)1 GenericKeyedObjectPoolConfig (org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig)1 DelegatingConnection (org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingConnection)1 DelegatingPreparedStatement (org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingPreparedStatement)1 PoolableCallableStatement (org.datanucleus.store.rdbms.datasource.dbcp2.PoolableCallableStatement)1 GenericKeyedObjectPoolConfig (org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPoolConfig)1