Search in sources :

Example 11 with BasicDataSource

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());
    }
}
Also used : Connection(java.sql.Connection) ManagedConnection(org.apache.commons.dbcp2.managed.ManagedConnection) ArrayList(java.util.ArrayList) ManagedConnection(org.apache.commons.dbcp2.managed.ManagedConnection) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) Test(org.junit.Test)

Example 12 with BasicDataSource

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;
}
Also used : BasicDataSource(org.apache.commons.dbcp2.BasicDataSource)

Example 13 with BasicDataSource

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);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 14 with BasicDataSource

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");
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Set(java.util.Set) MBeanExporter(org.springframework.jmx.export.MBeanExporter) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 15 with BasicDataSource

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);
}
Also used : BasicDataSource(org.apache.commons.dbcp2.BasicDataSource)

Aggregations

BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)20 Test (org.junit.Test)10 DataSource (javax.sql.DataSource)3 SQLException (java.sql.SQLException)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 DataSourceMonitorAccessor (com.navercorp.pinpoint.plugin.commons.dbcp2.DataSourceMonitorAccessor)1 Dbcp2DataSourceMonitor (com.navercorp.pinpoint.plugin.commons.dbcp2.Dbcp2DataSourceMonitor)1 Field (java.lang.reflect.Field)1 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 InitialContext (javax.naming.InitialContext)1 ManagedConnection (org.apache.commons.dbcp2.managed.ManagedConnection)1 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)1 JDBCDataSource (org.hsqldb.jdbc.JDBCDataSource)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1 NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)1 MBeanExporter (org.springframework.jmx.export.MBeanExporter)1