Search in sources :

Example 1 with ConnectionFactory

use of org.apache.tomcat.dbcp.dbcp2.ConnectionFactory in project cloudstack by apache.

the class TransactionLegacy method createDataSource.

/**
 * Creates a data source
 */
private static DataSource createDataSource(String uri, String username, String password, Integer maxActive, Integer maxIdle, Long maxWait, Long timeBtwnEvictionRuns, Long minEvictableIdleTime, Boolean testWhileIdle, Boolean testOnBorrow, String validationQuery, Integer isolationLevel) {
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(uri, username, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    GenericObjectPoolConfig config = createPoolConfig(maxActive, maxIdle, maxWait, timeBtwnEvictionRuns, minEvictableIdleTime, testWhileIdle, testOnBorrow);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
    poolableConnectionFactory.setPool(connectionPool);
    if (validationQuery != null) {
        poolableConnectionFactory.setValidationQuery(validationQuery);
    }
    if (isolationLevel != null) {
        poolableConnectionFactory.setDefaultTransactionIsolation(isolationLevel);
    }
    return new PoolingDataSource<>(connectionPool);
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) 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 2 with ConnectionFactory

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

the class BasicManagedDataSource method createConnectionFactory.

@Override
protected ConnectionFactory createConnectionFactory() throws SQLException {
    if (transactionManager == null) {
        throw new SQLException("Transaction manager must be set before a connection can be created");
    }
    // LocalXAConnectionFactory
    if (xaDataSource == null) {
        final ConnectionFactory connectionFactory = super.createConnectionFactory();
        final XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory(getTransactionManager(), getTransactionSynchronizationRegistry(), connectionFactory);
        transactionRegistry = xaConnectionFactory.getTransactionRegistry();
        return xaConnectionFactory;
    }
    // Create the XADataSource instance using the configured class name if it has not been set
    if (xaDataSourceInstance == null) {
        Class<?> xaDataSourceClass = null;
        try {
            xaDataSourceClass = Class.forName(xaDataSource);
        } catch (final Exception t) {
            final String message = "Cannot load XA data source class '" + xaDataSource + "'";
            throw new SQLException(message, t);
        }
        try {
            xaDataSourceInstance = (XADataSource) xaDataSourceClass.getConstructor().newInstance();
        } catch (final Exception t) {
            final String message = "Cannot create XA data source of class '" + xaDataSource + "'";
            throw new SQLException(message, t);
        }
    }
    // finally, create the XAConnectionFactory using the XA data source
    final XAConnectionFactory xaConnectionFactory = new DataSourceXAConnectionFactory(getTransactionManager(), xaDataSourceInstance, getUsername(), Utils.toCharArray(getPassword()), getTransactionSynchronizationRegistry());
    transactionRegistry = xaConnectionFactory.getTransactionRegistry();
    return xaConnectionFactory;
}
Also used : ConnectionFactory(org.apache.tomcat.dbcp.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory) SQLException(java.sql.SQLException) SQLException(java.sql.SQLException)

Example 3 with ConnectionFactory

use of org.apache.tomcat.dbcp.dbcp2.ConnectionFactory in project athenz by yahoo.

the class DataSourceFactory method create.

public static PoolableDataSource create(String url, Properties mysqlConnectionProperties) {
    String driver = null;
    try {
        if (url.indexOf(":mysql:") > 0) {
            driver = System.getProperty(DRIVER_CLASS_NAME, "com.mysql.cj.jdbc.Driver");
            Class.forName(driver);
            ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, mysqlConnectionProperties);
            return create(connectionFactory);
        } else {
            throw new RuntimeException("Cannot figure out how to instantiate this data source: " + url);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Cannot load driver class: " + driver);
    } catch (Exception exc) {
        throw new RuntimeException("Failed to create database source(" + url + ") with driver(" + driver + ")", exc);
    }
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory)

Example 4 with ConnectionFactory

use of org.apache.tomcat.dbcp.dbcp2.ConnectionFactory in project kie-wb-common by kiegroup.

the class DBCPDataSourceProvider method deploy.

@Override
public DataSourceDeploymentInfo deploy(DataSourceDef dataSourceDef) throws Exception {
    DriverDef driverDef = null;
    for (DriverDef _driverDef : driverProvider.getDeployments()) {
        if (_driverDef.getUuid().equals(dataSourceDef.getDriverUuid())) {
            driverDef = _driverDef;
            break;
        }
    }
    if (driverDef == null) {
        throw new Exception("Required driver: " + dataSourceDef.getDriverUuid() + " is not deployed");
    }
    final URI uri = artifactResolver.resolve(driverDef.getGroupId(), driverDef.getArtifactId(), driverDef.getVersion());
    if (uri == null) {
        throw new Exception("Unable to get driver library artifact for driver: " + driverDef);
    }
    final Properties properties = new Properties();
    properties.setProperty("user", dataSourceDef.getUser());
    properties.setProperty("password", dataSourceDef.getPassword());
    final URLConnectionFactory urlConnectionFactory = buildConnectionFactory(uri, driverDef.getDriverClass(), dataSourceDef.getConnectionURL(), properties);
    // Connection Factory that the pool will use for creating connections.
    ConnectionFactory connectionFactory = new DBCPConnectionFactory(urlConnectionFactory);
    // Poolable connection factory
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    // The pool to be used by the ConnectionFactory
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);
    // Finally create DataSource
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
    DataSourceDeploymentInfo deploymentInfo = new DataSourceDeploymentInfo(dataSourceDef.getUuid(), true, dataSourceDef.getUuid(), false);
    deploymentRegistry.put(deploymentInfo.getDeploymentId(), new DBCPDataSource(dataSource));
    deploymentInfos.put(deploymentInfo.getDeploymentId(), deploymentInfo);
    deployedDataSources.put(deploymentInfo.getDeploymentId(), dataSourceDef);
    return deploymentInfo;
}
Also used : URLConnectionFactory(org.kie.workbench.common.screens.datasource.management.util.URLConnectionFactory) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) URI(java.net.URI) DataSourceDeploymentInfo(org.kie.workbench.common.screens.datasource.management.model.DataSourceDeploymentInfo) SQLException(java.sql.SQLException) ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) URLConnectionFactory(org.kie.workbench.common.screens.datasource.management.util.URLConnectionFactory) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) DriverDef(org.kie.workbench.common.screens.datasource.management.model.DriverDef) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 5 with ConnectionFactory

use of org.apache.tomcat.dbcp.dbcp2.ConnectionFactory in project tomee by apache.

the class DbcpManagedDataSource method createConnectionFactory.

@Override
protected ConnectionFactory createConnectionFactory() throws SQLException {
    if (ds instanceof XADataSource) {
        // Create the XAConectionFactory using the XA data source
        final XADataSource xaDataSourceInstance = (XADataSource) ds;
        final XAConnectionFactory xaConnectionFactory = new DataSourceXAConnectionFactory(getTransactionManager(), xaDataSourceInstance, getUsername(), getPassword());
        setTransactionRegistry(xaConnectionFactory.getTransactionRegistry());
        return xaConnectionFactory;
    }
    // If xa data source is not specified a DriverConnectionFactory is created and wrapped with a LocalXAConnectionFactory
    final ConnectionFactory connectionFactory = new DataSourceConnectionFactory(DataSource.class.cast(ds), getUsername(), getPassword());
    final XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory(getTransactionManager(), connectionFactory);
    setTransactionRegistry(xaConnectionFactory.getTransactionRegistry());
    return xaConnectionFactory;
}
Also used : XADataSource(javax.sql.XADataSource) ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DataSourceConnectionFactory(org.apache.commons.dbcp2.DataSourceConnectionFactory) DataSourceXAConnectionFactory(org.apache.commons.dbcp2.managed.DataSourceXAConnectionFactory) LocalXAConnectionFactory(org.apache.commons.dbcp2.managed.LocalXAConnectionFactory) XAConnectionFactory(org.apache.commons.dbcp2.managed.XAConnectionFactory) DataSourceConnectionFactory(org.apache.commons.dbcp2.DataSourceConnectionFactory) DataSourceXAConnectionFactory(org.apache.commons.dbcp2.managed.DataSourceXAConnectionFactory) DataSourceXAConnectionFactory(org.apache.commons.dbcp2.managed.DataSourceXAConnectionFactory) LocalXAConnectionFactory(org.apache.commons.dbcp2.managed.LocalXAConnectionFactory) XAConnectionFactory(org.apache.commons.dbcp2.managed.XAConnectionFactory) LocalXAConnectionFactory(org.apache.commons.dbcp2.managed.LocalXAConnectionFactory) CommonDataSource(javax.sql.CommonDataSource) DataSource(javax.sql.DataSource) XADataSource(javax.sql.XADataSource)

Aggregations

ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)11 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)9 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)7 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)7 SQLException (java.sql.SQLException)4 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)4 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)4 Properties (java.util.Properties)3 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)3 DataSourceConnectionFactory (org.apache.commons.dbcp2.DataSourceConnectionFactory)2 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)2 PoolableConnectionFactory (org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory)2 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Driver (java.sql.Driver)1 List (java.util.List)1 Map (java.util.Map)1 CommonDataSource (javax.sql.CommonDataSource)1 DataSource (javax.sql.DataSource)1