Search in sources :

Example 1 with DelegatingPreparedStatement

use of org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingPreparedStatement 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 2 with DelegatingPreparedStatement

use of org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingPreparedStatement 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 3 with DelegatingPreparedStatement

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

the class PoolableConnectionFactory method makeObject.

@Override
public PooledObject<PoolableConnection> makeObject() throws Exception {
    Connection conn = connectionFactory.createConnection();
    if (conn == null) {
        throw new IllegalStateException("Connection factory returned null from createConnection");
    }
    try {
        initializeConnection(conn);
    } catch (final SQLException sqle) {
        // Make sure the connection is closed
        try {
            conn.close();
        } catch (final SQLException ignore) {
        // ignore
        }
        // Rethrow original exception so it is visible to caller
        throw sqle;
    }
    final long connIndex = connectionIndex.getAndIncrement();
    if (poolStatements) {
        conn = new PoolingConnection(conn);
        final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
        config.setMaxTotalPerKey(-1);
        config.setBlockWhenExhausted(false);
        config.setMaxWaitMillis(0);
        config.setMaxIdlePerKey(1);
        config.setMaxTotal(maxOpenPreparedStatements);
        if (dataSourceJmxObjectName != null) {
            final StringBuilder base = new StringBuilder(dataSourceJmxObjectName.toString());
            base.append(Constants.JMX_CONNECTION_BASE_EXT);
            base.append(Long.toString(connIndex));
            config.setJmxNameBase(base.toString());
            config.setJmxNamePrefix(Constants.JMX_STATEMENT_POOL_PREFIX);
        } else {
            config.setJmxEnabled(false);
        }
        final PoolingConnection poolingConn = (PoolingConnection) conn;
        final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = new GenericKeyedObjectPool<>(poolingConn, config);
        poolingConn.setStatementPool(stmtPool);
        poolingConn.setCacheState(cacheState);
    }
    // Register this connection with JMX
    ObjectName connJmxName;
    if (dataSourceJmxObjectName == null) {
        connJmxName = null;
    } else {
        connJmxName = new ObjectName(dataSourceJmxObjectName.toString() + Constants.JMX_CONNECTION_BASE_EXT + connIndex);
    }
    final PoolableConnection pc = new PoolableConnection(conn, pool, connJmxName, disconnectionSqlCodes, fastFailValidation);
    pc.setCacheState(cacheState);
    return new DefaultPooledObject<>(pc);
}
Also used : GenericKeyedObjectPoolConfig(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPoolConfig) SQLException(java.sql.SQLException) GenericKeyedObjectPool(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericKeyedObjectPool) Connection(java.sql.Connection) ObjectName(javax.management.ObjectName) DefaultPooledObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject)

Example 4 with DelegatingPreparedStatement

use of org.datanucleus.store.rdbms.datasource.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 5 with DelegatingPreparedStatement

use of org.datanucleus.store.rdbms.datasource.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)

Aggregations

Connection (java.sql.Connection)4 CallableStatement (java.sql.CallableStatement)3 PreparedStatement (java.sql.PreparedStatement)3 DelegatingPreparedStatement (org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement)3 DefaultPooledObject (org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject)3 ObjectName (javax.management.ObjectName)2 PooledConnection (javax.sql.PooledConnection)2 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)2 DelegatingPreparedStatement (org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingPreparedStatement)2 SQLException (java.sql.SQLException)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 DelegatingConnection (org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingConnection)1 PoolableCallableStatement (org.datanucleus.store.rdbms.datasource.dbcp2.PoolableCallableStatement)1