use of org.apache.tomcat.dbcp.dbcp2.ConnectionFactory in project Openfire by igniterealtime.
the class EmbeddedConnectionProvider method start.
@Override
public void start() {
File databaseDir = new File(JiveGlobals.getHomeDirectory(), File.separator + "embedded-db");
// If the database doesn't exist, create it.
if (!databaseDir.exists()) {
databaseDir.mkdirs();
}
try {
serverURL = "jdbc:hsqldb:" + databaseDir.getCanonicalPath() + File.separator + "openfire";
} catch (IOException ioe) {
Log.error("EmbeddedConnectionProvider: Error starting connection pool: ", ioe);
}
final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(serverURL, "sa", "");
final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
poolableConnectionFactory.setMaxConnLifetimeMillis((long) (0.5 * JiveConstants.DAY));
final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMinIdle(3);
poolConfig.setMaxTotal(25);
final GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, poolConfig);
poolableConnectionFactory.setPool(connectionPool);
dataSource = new PoolingDataSource<>(connectionPool);
}
use of org.apache.tomcat.dbcp.dbcp2.ConnectionFactory in project druid by druid-io.
the class BasicDataSourceExtTest method testCreateConnectionFactory.
@Test
public void testCreateConnectionFactory() throws Exception {
MetadataStorageConnectorConfig connectorConfig = new MetadataStorageConnectorConfig() {
private final List<String> passwords = Lists.newArrayList("pwd1", "pwd2");
@Override
public String getUser() {
return "testuser";
}
@Override
public String getPassword() {
return passwords.remove(0);
}
};
BasicDataSourceExt basicDataSourceExt = new BasicDataSourceExt(connectorConfig);
basicDataSourceExt.setConnectionProperties("p1=v1");
basicDataSourceExt.addConnectionProperty("p2", "v2");
Driver driver = EasyMock.mock(Driver.class);
Capture<String> uriArg = Capture.newInstance();
Capture<Properties> propsArg = Capture.newInstance();
EasyMock.expect(driver.connect(EasyMock.capture(uriArg), EasyMock.capture(propsArg))).andReturn(null).times(2);
EasyMock.replay(driver);
basicDataSourceExt.setDriver(driver);
ConnectionFactory connectionFactory = basicDataSourceExt.createConnectionFactory();
Properties expectedProps = new Properties();
expectedProps.put("p1", "v1");
expectedProps.put("p2", "v2");
expectedProps.put("user", connectorConfig.getUser());
Assert.assertNull(connectionFactory.createConnection());
Assert.assertEquals(connectorConfig.getConnectURI(), uriArg.getValue());
expectedProps.put("password", "pwd1");
Assert.assertEquals(expectedProps, propsArg.getValue());
Assert.assertNull(connectionFactory.createConnection());
Assert.assertEquals(connectorConfig.getConnectURI(), uriArg.getValue());
expectedProps.put("password", "pwd2");
Assert.assertEquals(expectedProps, propsArg.getValue());
}
use of org.apache.tomcat.dbcp.dbcp2.ConnectionFactory in project cloudstack by apache.
the class TransactionLegacy method getDefaultDataSource.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static DataSource getDefaultDataSource(final String database) {
final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://localhost:3306/" + database, "cloud", "cloud");
final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
final GenericObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);
return new PoolingDataSource(connectionPool);
}
use of org.apache.tomcat.dbcp.dbcp2.ConnectionFactory in project tomcat by apache.
the class BasicManagedDataSource method createPoolableConnectionFactory.
/**
* Creates the PoolableConnectionFactory and attaches it to the connection pool.
*
* @param driverConnectionFactory
* JDBC connection factory created by {@link #createConnectionFactory()}
* @throws SQLException
* if an error occurs creating the PoolableConnectionFactory
*/
@Override
protected PoolableConnectionFactory createPoolableConnectionFactory(final ConnectionFactory driverConnectionFactory) throws SQLException {
PoolableConnectionFactory connectionFactory = null;
try {
connectionFactory = new PoolableManagedConnectionFactory((XAConnectionFactory) driverConnectionFactory, getRegisteredJmxName());
connectionFactory.setValidationQuery(getValidationQuery());
connectionFactory.setValidationQueryTimeout(getValidationQueryTimeoutDuration());
connectionFactory.setConnectionInitSql(getConnectionInitSqls());
connectionFactory.setDefaultReadOnly(getDefaultReadOnly());
connectionFactory.setDefaultAutoCommit(getDefaultAutoCommit());
connectionFactory.setDefaultTransactionIsolation(getDefaultTransactionIsolation());
connectionFactory.setDefaultCatalog(getDefaultCatalog());
connectionFactory.setDefaultSchema(getDefaultSchema());
connectionFactory.setCacheState(getCacheState());
connectionFactory.setPoolStatements(isPoolPreparedStatements());
connectionFactory.setClearStatementPoolOnReturn(isClearStatementPoolOnReturn());
connectionFactory.setMaxOpenPreparedStatements(getMaxOpenPreparedStatements());
connectionFactory.setMaxConn(getMaxConnDuration());
connectionFactory.setRollbackOnReturn(getRollbackOnReturn());
connectionFactory.setAutoCommitOnReturn(getAutoCommitOnReturn());
connectionFactory.setDefaultQueryTimeout(getDefaultQueryTimeoutDuration());
connectionFactory.setFastFailValidation(getFastFailValidation());
connectionFactory.setDisconnectionSqlCodes(getDisconnectionSqlCodes());
validateConnectionFactory(connectionFactory);
} catch (final RuntimeException e) {
throw e;
} catch (final Exception e) {
throw new SQLException("Cannot create PoolableConnectionFactory (" + e.getMessage() + ")", e);
}
return connectionFactory;
}
Aggregations