use of org.apache.commons.dbcp2.BasicDataSource in project tomee by apache.
the class XAPoolTest method check.
@Test
public void check() throws SQLException {
assertNotNull(ds);
final BasicDataSource tds = ManagedDataSourceWithRecovery.class.cast(ds);
// db not yet used so no connection at all (dbcp behavior)
assertEquals(0, tds.getNumIdle());
try (final Connection c = ds.getConnection()) {
assertNotNull(c);
// just to do something and force the connection init
final Connection connection = c.getMetaData().getConnection();
assertThat(connection, instanceOf(ManagedConnection.class));
assertTrue(connection.toString().contains("URL=jdbc:hsqldb:mem:dbcpxa, UserName=SA, HSQL Database Engine Driver"));
}
// here we close the connection so we are back in the initial state
assertEquals(0, tds.getNumActive());
assertEquals(3, tds.getNumIdle());
for (int it = 0; it < 5; it++) {
// ensures it always works and not only the first time
final Collection<Connection> connections = new ArrayList<>(25);
for (int i = 0; i < 25; i++) {
final Connection connection = ds.getConnection();
connections.add(connection);
// trigger connection retrieving otherwise nothing is done (pool is not used)
connection.getMetaData();
}
assertEquals(25, tds.getNumActive());
assertEquals(0, tds.getNumIdle());
for (final Connection toClose : connections) {
toClose.close();
}
assertEquals(0, tds.getNumActive());
assertEquals(25, tds.getNumIdle());
}
}
use of org.apache.commons.dbcp2.BasicDataSource in project druid by druid-io.
the class SQLMetadataConnector method getDatasource.
protected BasicDataSource getDatasource() {
MetadataStorageConnectorConfig connectorConfig = getConfig();
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUsername(connectorConfig.getUser());
dataSource.setPassword(connectorConfig.getPassword());
String uri = connectorConfig.getConnectURI();
dataSource.setUrl(uri);
dataSource.setValidationQuery(getValidationQuery());
dataSource.setTestOnBorrow(true);
return dataSource;
}
use of org.apache.commons.dbcp2.BasicDataSource in project spring-boot by spring-projects.
the class JndiDataSourceAutoConfigurationTests method dataSourceIsAvailableFromJndi.
@Test
public void dataSourceIsAvailableFromJndi() throws IllegalStateException, NamingException {
DataSource dataSource = new BasicDataSource();
configureJndi("foo", dataSource);
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.jndi-name:foo");
this.context.register(JndiDataSourceAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(DataSource.class)).isEqualTo(dataSource);
}
use of org.apache.commons.dbcp2.BasicDataSource in project spring-boot by spring-projects.
the class JndiDataSourceAutoConfigurationTests method mbeanDataSourceIsExcludedFromExport.
@SuppressWarnings("unchecked")
@Test
public void mbeanDataSourceIsExcludedFromExport() throws IllegalStateException, NamingException {
DataSource dataSource = new BasicDataSource();
configureJndi("foo", dataSource);
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.jndi-name:foo");
this.context.register(JndiDataSourceAutoConfiguration.class, MBeanExporterConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(DataSource.class)).isEqualTo(dataSource);
MBeanExporter exporter = this.context.getBean(MBeanExporter.class);
Set<String> excludedBeans = (Set<String>) new DirectFieldAccessor(exporter).getPropertyValue("excludedBeans");
assertThat(excludedBeans).containsExactly("dataSource");
}
use of org.apache.commons.dbcp2.BasicDataSource in project spring-boot by spring-projects.
the class CommonsDbcp2DataSourcePoolMetadataTests method createDataSourceMetadata.
private CommonsDbcp2DataSourcePoolMetadata createDataSourceMetadata(int minSize, int maxSize) {
BasicDataSource dataSource = createDataSource();
dataSource.setMinIdle(minSize);
dataSource.setMaxTotal(maxSize);
return new CommonsDbcp2DataSourcePoolMetadata(dataSource);
}
Aggregations