Search in sources :

Example 6 with DefaultPooledObject

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject 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 7 with DefaultPooledObject

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject in project tomcat by apache.

the class CPDSConnectionFactory method makeObject.

// ***********************************************************************
// java.sql.ConnectionEventListener implementation
// ***********************************************************************
@Override
public synchronized PooledObject<PooledConnectionAndInfo> makeObject() {
    final PooledConnectionAndInfo pci;
    try {
        PooledConnection pc = null;
        if (userPassKey.getUserName() == null) {
            pc = cpds.getPooledConnection();
        } else {
            pc = cpds.getPooledConnection(userPassKey.getUserName(), userPassKey.getPassword());
        }
        if (pc == null) {
            throw new IllegalStateException("Connection pool data source returned null from getPooledConnection");
        }
        // should we add this object as a listener or the pool.
        // consider the validateObject method in decision
        pc.addConnectionEventListener(this);
        pci = new PooledConnectionAndInfo(pc, userPassKey);
        pcMap.put(pc, pci);
    } catch (final SQLException e) {
        throw new RuntimeException(e.getMessage());
    }
    return new DefaultPooledObject<>(pci);
}
Also used : DefaultPooledObject(org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject) PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException)

Example 8 with DefaultPooledObject

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject in project tomcat by apache.

the class KeyedCPDSConnectionFactory method makeObject.

// ***********************************************************************
// java.sql.ConnectionEventListener implementation
// ***********************************************************************
/**
 * Creates a new {@code PooledConnectionAndInfo} from the given {@code UserPassKey}.
 *
 * @param userPassKey
 *            {@code UserPassKey} containing user credentials
 * @throws SQLException
 *             if the connection could not be created.
 * @see org.apache.tomcat.dbcp.pool2.KeyedPooledObjectFactory#makeObject(java.lang.Object)
 */
@Override
public synchronized PooledObject<PooledConnectionAndInfo> makeObject(final UserPassKey userPassKey) throws Exception {
    PooledConnection pooledConnection = null;
    final String userName = userPassKey.getUserName();
    final String password = userPassKey.getPassword();
    if (userName == null) {
        pooledConnection = cpds.getPooledConnection();
    } else {
        pooledConnection = cpds.getPooledConnection(userName, password);
    }
    if (pooledConnection == null) {
        throw new IllegalStateException("Connection pool data source returned null from getPooledConnection");
    }
    // should we add this object as a listener or the pool.
    // consider the validateObject method in decision
    pooledConnection.addConnectionEventListener(this);
    final PooledConnectionAndInfo pci = new PooledConnectionAndInfo(pooledConnection, userPassKey);
    pcMap.put(pooledConnection, pci);
    return new DefaultPooledObject<>(pci);
}
Also used : DefaultPooledObject(org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject) PooledConnection(javax.sql.PooledConnection)

Example 9 with DefaultPooledObject

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

Example 10 with DefaultPooledObject

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject in project tomcat by apache.

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
        Utils.closeQuietly((AutoCloseable) conn);
        // 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.setMaxWait(Duration.ZERO);
        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(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.setClearStatementPoolOnReturn(clearStatementPoolOnReturn);
        poolingConn.setCacheState(cacheState);
    }
    // Register this connection with JMX
    final 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.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig) SQLException(java.sql.SQLException) GenericKeyedObjectPool(org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool) Connection(java.sql.Connection) ObjectName(javax.management.ObjectName) DefaultPooledObject(org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)

Aggregations

DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)8 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)6 PooledConnection (javax.sql.PooledConnection)5 PooledObject (org.apache.commons.pool2.PooledObject)5 URISyntaxException (java.net.URISyntaxException)4 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 PooledObjectFactory (org.apache.commons.pool2.PooledObjectFactory)4 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)4 Test (org.junit.Test)4 InvalidURIException (redis.clients.jedis.exceptions.InvalidURIException)4 JedisException (redis.clients.jedis.exceptions.JedisException)4 ObjectName (javax.management.ObjectName)3 DefaultPooledObject (org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.DefaultPooledObject)3 Jedis (redis.clients.jedis.Jedis)3 JedisPool (redis.clients.jedis.JedisPool)3 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)3 Channel (io.netty.channel.Channel)2 ChannelFuture (io.netty.channel.ChannelFuture)2