Search in sources :

Example 6 with BasicDataSource

use of org.apache.commons.dbcp2.BasicDataSource in project spring-boot by spring-projects.

the class DataSourceAutoConfigurationTests method commonsDbcp2ValidatesConnectionByDefault.

@Test
public void commonsDbcp2ValidatesConnectionByDefault() throws Exception {
    org.apache.commons.dbcp2.BasicDataSource dataSource = autoConfigureDataSource(org.apache.commons.dbcp2.BasicDataSource.class, "org.apache.tomcat", "com.zaxxer.hikari");
    assertThat(dataSource.getTestOnBorrow()).isEqualTo(true);
    // Use Connection#isValid()
    assertThat(dataSource.getValidationQuery()).isNull();
}
Also used : BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) Test(org.junit.Test)

Example 7 with BasicDataSource

use of org.apache.commons.dbcp2.BasicDataSource in project spring-boot by spring-projects.

the class JdbcTemplateAutoConfigurationTests method testJdbcTemplateExistsWithCustomDataSource.

@Test
public void testJdbcTemplateExistsWithCustomDataSource() throws Exception {
    this.context.register(TestDataSourceConfiguration.class, DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class);
    assertThat(jdbcTemplate).isNotNull();
    assertThat(jdbcTemplate.getDataSource() instanceof BasicDataSource).isTrue();
}
Also used : NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) Test(org.junit.Test)

Example 8 with BasicDataSource

use of org.apache.commons.dbcp2.BasicDataSource in project spring-boot by spring-projects.

the class CommonsDbcp2DataSourcePoolMetadataTests method getValidationQuery.

@Override
public void getValidationQuery() {
    BasicDataSource dataSource = createDataSource();
    dataSource.setValidationQuery("SELECT FROM FOO");
    assertThat(new CommonsDbcp2DataSourcePoolMetadata(dataSource).getValidationQuery()).isEqualTo("SELECT FROM FOO");
}
Also used : BasicDataSource(org.apache.commons.dbcp2.BasicDataSource)

Example 9 with BasicDataSource

use of org.apache.commons.dbcp2.BasicDataSource in project cayenne by apache.

the class DBCP2DataSourceFactoryTest method testGetDataSource.

@Test
public void testGetDataSource() throws Exception {
    String baseUrl = getClass().getPackage().getName().replace('.', '/');
    URL url = getClass().getClassLoader().getResource(baseUrl + "/");
    assertNotNull(url);
    DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
    nodeDescriptor.setConfigurationSource(new URLResource(url));
    nodeDescriptor.setParameters("testDBCP2.properties");
    DBCPDataSourceFactory factory = new DBCPDataSourceFactory();
    DataSource dataSource = factory.getDataSource(nodeDescriptor);
    assertNotNull(dataSource);
    assertTrue(dataSource instanceof BasicDataSource);
    try (BasicDataSource basicDataSource = (BasicDataSource) dataSource) {
        assertEquals("com.example.jdbc.Driver", basicDataSource.getDriverClassName());
        assertEquals("jdbc:somedb://localhost/cayenne", basicDataSource.getUrl());
        assertEquals("john", basicDataSource.getUsername());
        assertEquals("secret", basicDataSource.getPassword());
        assertEquals(20, basicDataSource.getMaxTotal());
        assertEquals(5, basicDataSource.getMinIdle());
        assertEquals(8, basicDataSource.getMaxIdle());
        assertEquals(10000, basicDataSource.getMaxWaitMillis());
        assertEquals("select 1 from xyz;", basicDataSource.getValidationQuery());
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) URL(java.net.URL) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 10 with BasicDataSource

use of org.apache.commons.dbcp2.BasicDataSource in project HongsCORE by ihongs.

the class Source method open.

public static Connection open(String jdbc, String name, Properties info) throws SQLException {
    String namc = jdbc + " " + name;
    BasicDataSource pool;
    sourceLock.readLock().lock();
    try {
        pool = sourcePool.get(namc);
        if (pool != null) {
            return pool.getConnection();
        }
    } finally {
        sourceLock.readLock().unlock();
    }
    // SQLite 数据路径处理
    if (name.startsWith("jdbc:sqlite:")) {
        String jurl = name.substring(12);
        Map injt = new HashMap();
        injt.put("CORE_PATH", Core.CORE_PATH);
        injt.put("CONF_PATH", Core.CONF_PATH);
        injt.put("DATA_PATH", Core.DATA_PATH);
        jurl = Tool.inject(jurl, injt);
        if (!new File(jurl).isAbsolute()) {
            jurl = Core.DATA_PATH + "/sqlite/" + jurl;
        }
        if (!new File(jurl).getParentFile().exists()) {
            new File(jurl).getParentFile().mkdirs();
        }
        name = "jdbc:sqlite:" + jurl;
    }
    sourceLock.writeLock().lock();
    try {
        pool = new BasicDataSource();
        sourcePool.put(namc, pool);
        pool.setDriverClassName(jdbc);
        pool.setUrl(/* Source */
        name);
        if (info.containsKey("username")) {
            pool.setUsername(info.getProperty("username"));
        }
        if (info.containsKey("password")) {
            pool.setPassword(info.getProperty("password"));
        }
        // 连接属性
        if (info.containsKey("connectionProperties")) {
            pool.setConnectionProperties(info.getProperty("connectionProperties").trim().replace("&", ";"));
        }
        if (info.containsKey("connectionInitSqls")) {
            pool.setConnectionInitSqls(Arrays.asList(info.getProperty("connectionInitSqls").trim().split("\\s*;\\s*")));
        }
        // 基础设置
        if (info.containsKey("initSize")) {
            pool.setInitialSize(Integer.parseInt(info.getProperty("initSize")));
        }
        if (info.containsKey("maxTotal")) {
            pool.setMaxTotal(Integer.parseInt(info.getProperty("maxTotal")));
        }
        if (info.containsKey("minIdle")) {
            pool.setMinIdle(Integer.parseInt(info.getProperty("minIdle")));
        }
        if (info.containsKey("maxIdle")) {
            pool.setMaxIdle(Integer.parseInt(info.getProperty("maxIdle")));
        }
        if (info.containsKey("maxWait")) {
            pool.setMaxWaitMillis(Long.parseLong(info.getProperty("maxWait")));
        }
        // 回收检测
        if (info.containsKey("logAbandoned")) {
            pool.setLogAbandoned(Boolean.parseBoolean(info.getProperty("logAbandoned")));
        }
        if (info.containsKey("testOnCreate")) {
            pool.setTestOnBorrow(Boolean.parseBoolean(info.getProperty("testOnCreate")));
        }
        if (info.containsKey("testOnBorrow")) {
            pool.setTestOnBorrow(Boolean.parseBoolean(info.getProperty("testOnBorrow")));
        }
        if (info.containsKey("testOnReturn")) {
            pool.setTestOnReturn(Boolean.parseBoolean(info.getProperty("testOnReturn")));
        }
        if (info.containsKey("testWhileIdle")) {
            pool.setTestWhileIdle(Boolean.parseBoolean(info.getProperty("testWhileIdle")));
        }
        if (info.containsKey("validationQuery")) {
            pool.setValidationQuery(info.getProperty("validationQuery"));
        }
        if (info.containsKey("validationQueryTimeout")) {
            pool.setValidationQueryTimeout(Integer.parseInt(info.getProperty("validationQueryTimeout")));
        }
        if (info.containsKey("removeAbandonedTimeout")) {
            pool.setRemoveAbandonedTimeout(Integer.parseInt(info.getProperty("removeAbandonedTimeout")));
        }
        if (info.containsKey("removeAbandonedOnBorrow")) {
            pool.setRemoveAbandonedOnBorrow(Boolean.parseBoolean(info.getProperty("removeAbandonedOnBorrow")));
        }
        if (info.containsKey("removeAbandonedOnMaintenance")) {
            pool.setRemoveAbandonedOnMaintenance(Boolean.parseBoolean(info.getProperty("removeAbandonedOnMaintenance")));
        }
        // 其他设置
        if (info.containsKey("poolPreparedStatements")) {
            pool.setPoolPreparedStatements(Boolean.parseBoolean(info.getProperty("poolPreparedStatements")));
        }
        if (info.containsKey("maxOpenPreparedStatements")) {
            pool.setMaxOpenPreparedStatements(Integer.parseInt(info.getProperty("maxOpenPreparedStatements")));
        }
        if (info.containsKey("numTestsPerEvictionRun")) {
            pool.setNumTestsPerEvictionRun(Integer.parseInt(info.getProperty("numTestsPerEvictionRun")));
        }
        if (info.containsKey("maxConnLifetimeMillis")) {
            pool.setMaxConnLifetimeMillis(Long.parseLong(info.getProperty("maxConnLifetimeMillis")));
        }
        if (info.containsKey("minEvictableIdleTimeMillis")) {
            pool.setMinEvictableIdleTimeMillis(Long.parseLong(info.getProperty("minEvictableIdleTimeMillis")));
        }
        if (info.containsKey("softMinEvictableIdleTimeMillis")) {
            pool.setSoftMinEvictableIdleTimeMillis(Long.parseLong(info.getProperty("softMinEvictableIdleTimeMillis")));
        }
        if (info.containsKey("timeBetweenEvictionRunsMillis")) {
            pool.setTimeBetweenEvictionRunsMillis(Long.parseLong(info.getProperty("timeBetweenEvictionRunsMillis")));
        }
        return pool.getConnection();
    } finally {
        sourceLock.writeLock().unlock();
    }
}
Also used : HashMap(java.util.HashMap) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Aggregations

BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)22 Test (org.junit.Test)11 DataSource (javax.sql.DataSource)4 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 File (java.io.File)1 Field (java.lang.reflect.Field)1 URL (java.net.URL)1 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 InitialContext (javax.naming.InitialContext)1 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)1 URLResource (org.apache.cayenne.resource.URLResource)1 ManagedConnection (org.apache.commons.dbcp2.managed.ManagedConnection)1 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)1