Search in sources :

Example 1 with DelegatingPreparedStatement

use of org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement 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)

Example 2 with DelegatingPreparedStatement

use of org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement in project tomcat by apache.

the class PooledConnectionImpl method passivateObject.

/**
 * My {@link KeyedPooledObjectFactory} method for passivating {@link PreparedStatement}s. Currently invokes
 * {@link PreparedStatement#clearParameters}.
 *
 * @param key
 *            ignored
 * @param pooledObject
 *            a wrapped {@link PreparedStatement}
 */
@Override
public void passivateObject(final PStmtKey key, final PooledObject<DelegatingPreparedStatement> pooledObject) throws Exception {
    final DelegatingPreparedStatement dps = pooledObject.getObject();
    dps.clearParameters();
    dps.passivate();
}
Also used : DelegatingPreparedStatement(org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement)

Example 3 with DelegatingPreparedStatement

use of org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement in project tomcat by apache.

the class PoolableManagedConnectionFactory method makeObject.

/**
 * Uses the configured XAConnectionFactory to create a {@link PoolableManagedConnection}. Throws
 * <code>IllegalStateException</code> if the connection factory returns null. Also initializes the connection using
 * configured initialization SQL (if provided) and sets up a prepared statement pool associated with the
 * PoolableManagedConnection if statement pooling is enabled.
 */
@Override
public synchronized PooledObject<PoolableConnection> makeObject() throws Exception {
    Connection conn = getConnectionFactory().createConnection();
    if (conn == null) {
        throw new IllegalStateException("Connection factory returned null from createConnection");
    }
    initializeConnection(conn);
    if (getPoolStatements()) {
        conn = new PoolingConnection(conn);
        final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
        config.setMaxTotalPerKey(-1);
        config.setBlockWhenExhausted(false);
        config.setMaxWait(Duration.ZERO);
        config.setMaxIdlePerKey(1);
        config.setMaxTotal(getMaxOpenPreparedStatements());
        final ObjectName dataSourceJmxName = getDataSourceJmxName();
        final long connIndex = getConnectionIndex().getAndIncrement();
        if (dataSourceJmxName != null) {
            final StringBuilder base = new StringBuilder(dataSourceJmxName.toString());
            base.append(Constants.JMX_CONNECTION_BASE_EXT);
            base.append(connIndex);
            config.setJmxNameBase(base.toString());
            config.setJmxNamePrefix(Constants.JMX_STATEMENT_POOL_PREFIX);
        } else {
            config.setJmxEnabled(false);
        }
        final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = new GenericKeyedObjectPool<>((PoolingConnection) conn, config);
        ((PoolingConnection) conn).setStatementPool(stmtPool);
        ((PoolingConnection) conn).setCacheState(getCacheState());
    }
    final PoolableManagedConnection pmc = new PoolableManagedConnection(transactionRegistry, conn, getPool(), getDisconnectionSqlCodes(), isFastFailValidation());
    pmc.setCacheState(getCacheState());
    return new DefaultPooledObject<>(pmc);
}
Also used : GenericKeyedObjectPoolConfig(org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig) DelegatingPreparedStatement(org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement) GenericKeyedObjectPool(org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool) Connection(java.sql.Connection) PoolableConnection(org.apache.tomcat.dbcp.dbcp2.PoolableConnection) PoolingConnection(org.apache.tomcat.dbcp.dbcp2.PoolingConnection) PStmtKey(org.apache.tomcat.dbcp.dbcp2.PStmtKey) ObjectName(javax.management.ObjectName) PoolingConnection(org.apache.tomcat.dbcp.dbcp2.PoolingConnection) DefaultPooledObject(org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)

Aggregations

DelegatingPreparedStatement (org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement)3 Connection (java.sql.Connection)2 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)2 CallableStatement (java.sql.CallableStatement)1 PreparedStatement (java.sql.PreparedStatement)1 ObjectName (javax.management.ObjectName)1 PooledConnection (javax.sql.PooledConnection)1 DelegatingConnection (org.apache.tomcat.dbcp.dbcp2.DelegatingConnection)1 PStmtKey (org.apache.tomcat.dbcp.dbcp2.PStmtKey)1 PoolableCallableStatement (org.apache.tomcat.dbcp.dbcp2.PoolableCallableStatement)1 PoolableConnection (org.apache.tomcat.dbcp.dbcp2.PoolableConnection)1 PoolablePreparedStatement (org.apache.tomcat.dbcp.dbcp2.PoolablePreparedStatement)1 PoolingConnection (org.apache.tomcat.dbcp.dbcp2.PoolingConnection)1 GenericKeyedObjectPool (org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool)1 GenericKeyedObjectPoolConfig (org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig)1