Search in sources :

Example 1 with PoolableConnection

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

the class BasicDataSource method createConnectionPool.

/**
 * Creates a connection pool for this datasource.  This method only exists
 * so subclasses can replace the implementation class.
 *
 * This implementation configures all pool properties other than
 * timeBetweenEvictionRunsMillis.  Setting that property is deferred to
 * {@link #startPoolMaintenance()}, since setting timeBetweenEvictionRunsMillis
 * to a positive value causes {@link GenericObjectPool}'s eviction timer
 * to be started.
 */
protected void createConnectionPool(PoolableConnectionFactory factory) {
    // Create an object pool to contain our active connections
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    updateJmxName(config);
    // Disable JMX on the underlying pool if the DS is not registered.
    config.setJmxEnabled(registeredJmxName != null);
    GenericObjectPool<PoolableConnection> gop;
    if (abandonedConfig != null && (abandonedConfig.getRemoveAbandonedOnBorrow() || abandonedConfig.getRemoveAbandonedOnMaintenance())) {
        gop = new GenericObjectPool<>(factory, config, abandonedConfig);
    } else {
        gop = new GenericObjectPool<>(factory, config);
    }
    gop.setMaxTotal(maxTotal);
    gop.setMaxIdle(maxIdle);
    gop.setMinIdle(minIdle);
    gop.setMaxWaitMillis(maxWaitMillis);
    gop.setTestOnCreate(testOnCreate);
    gop.setTestOnBorrow(testOnBorrow);
    gop.setTestOnReturn(testOnReturn);
    gop.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    gop.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    gop.setTestWhileIdle(testWhileIdle);
    gop.setLifo(lifo);
    gop.setSwallowedExceptionListener(new SwallowedExceptionLogger(log, logExpiredConnections));
    gop.setEvictionPolicyClassName(evictionPolicyClassName);
    factory.setPool(gop);
    connectionPool = gop;
}
Also used : GenericObjectPoolConfig(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig)

Example 2 with PoolableConnection

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

the class PoolableConnectionFactory method makeObject.

@Override
public PooledObject<PoolableConnection> makeObject() throws Exception {
    Connection conn = _connFactory.createConnection();
    if (conn == null) {
        throw new IllegalStateException("Connection factory returned null from createConnection");
    }
    try {
        initializeConnection(conn);
    } catch (SQLException sqle) {
        // Make sure the connection is closed
        try {
            conn.close();
        } catch (SQLException ignore) {
        // ignore
        }
        // Rethrow original exception so it is visible to caller
        throw sqle;
    }
    long connIndex = connectionIndex.getAndIncrement();
    if (poolStatements) {
        conn = new PoolingConnection(conn);
        GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
        config.setMaxTotalPerKey(-1);
        config.setBlockWhenExhausted(false);
        config.setMaxWaitMillis(0);
        config.setMaxIdlePerKey(1);
        config.setMaxTotal(maxOpenPreparedStatements);
        if (dataSourceJmxName != null) {
            StringBuilder base = new StringBuilder(dataSourceJmxName.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);
        }
        KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = new GenericKeyedObjectPool<>((PoolingConnection) conn, config);
        ((PoolingConnection) conn).setStatementPool(stmtPool);
        ((PoolingConnection) conn).setCacheState(_cacheState);
    }
    // Register this connection with JMX
    ObjectName connJmxName;
    if (dataSourceJmxName == null) {
        connJmxName = null;
    } else {
        connJmxName = new ObjectName(dataSourceJmxName.toString() + Constants.JMX_CONNECTION_BASE_EXT + connIndex);
    }
    PoolableConnection pc = new PoolableConnection(conn, _pool, connJmxName, _disconnectionSqlCodes, _fastFailValidation);
    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 3 with PoolableConnection

use of org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnection in project tomee by apache.

the class BasicManagedDataSource method createDataSourceInstance.

@Override
protected DataSource createDataSourceInstance() throws SQLException {
    final TransactionRegistry transactionRegistry = getTransactionRegistry();
    if (transactionRegistry == null) {
        throw new IllegalStateException("TransactionRegistry has not been set");
    }
    if (getConnectionPool() == null) {
        throw new IllegalStateException("Pool has not been set");
    }
    final PoolingDataSource<PoolableConnection> pds = new ManagedDataSource<PoolableConnection>(getConnectionPool(), transactionRegistry) {

        @Override
        public Connection getConnection() throws SQLException {
            return new ManagedConnection<PoolableConnection>(getPool(), transactionRegistry, isAccessToUnderlyingConnectionAllowed()) {

                @Override
                public void close() throws SQLException {
                    if (!isClosedInternal()) {
                        try {
                            if (null != getDelegateInternal()) {
                                super.close();
                            }
                        } finally {
                            setClosedInternal(true);
                        }
                    }
                }

                @Override
                public boolean isClosed() throws SQLException {
                    return isClosedInternal() || null != getDelegateInternal() && getDelegateInternal().isClosed();
                }
            };
        }
    };
    pds.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    return pds;
}
Also used : PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) TransactionRegistry(org.apache.commons.dbcp2.managed.TransactionRegistry) ManagedConnection(org.apache.commons.dbcp2.managed.ManagedConnection) ManagedDataSource(org.apache.commons.dbcp2.managed.ManagedDataSource)

Example 4 with PoolableConnection

use of org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnection in project athenz by yahoo.

the class DataSourceFactory method create.

static PoolableDataSource create(ConnectionFactory connectionFactory) {
    // setup our pool config object
    GenericObjectPoolConfig config = setupPoolConfig();
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    // Set max lifetime of a connection in milli-secs, after which it will
    // always fail activation, passivation, and validation.
    // Value of -1 means infinite life time. The default value
    // defined in this class is 10 minutes.
    long connTtlMillis = retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_TTL, MAX_TTL_CONN_MS);
    poolableConnectionFactory.setMaxConnLifetimeMillis(connTtlMillis);
    if (LOG.isInfoEnabled()) {
        LOG.info("Setting Time-To-Live interval for live connections (" + connTtlMillis + ") milli-secs");
    }
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
    poolableConnectionFactory.setPool(connectionPool);
    AthenzDataSource dataSource = new AthenzDataSource(connectionPool);
    return dataSource;
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 5 with PoolableConnection

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

PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)8 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)6 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)6 SQLException (java.sql.SQLException)4 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)4 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)4 Connection (java.sql.Connection)3 Properties (java.util.Properties)3 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)3 ObjectName (javax.management.ObjectName)2 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)2 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Driver (java.sql.Driver)1 TransactionManager (javax.transaction.TransactionManager)1 DriverConnectionFactory (org.apache.commons.dbcp2.DriverConnectionFactory)1 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)1 LocalXAConnectionFactory (org.apache.commons.dbcp2.managed.LocalXAConnectionFactory)1 ManagedConnection (org.apache.commons.dbcp2.managed.ManagedConnection)1