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