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);
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations