本文整理汇总了Java中org.apache.commons.dbcp.BasicDataSource.setValidationQueryTimeout方法的典型用法代码示例。如果您正苦于以下问题:Java BasicDataSource.setValidationQueryTimeout方法的具体用法?Java BasicDataSource.setValidationQueryTimeout怎么用?Java BasicDataSource.setValidationQueryTimeout使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.dbcp.BasicDataSource
的用法示例。
在下文中一共展示了BasicDataSource.setValidationQueryTimeout方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createDataSource
import org.apache.commons.dbcp.BasicDataSource; //导入方法依赖的package包/类
private BasicDataSource createDataSource(Map<String, String> properties, String urlPattern) {
BasicDataSource dsPool = new BasicDataSource();
dsPool.setDriverClassName(DSPropertyUtil.getPropertyStringValue("driver", properties, null, null, true));
dsPool.setUsername(DSPropertyUtil.getPropertyStringValue("username", properties, null, null, true));
dsPool.setPassword(DSPropertyUtil.getPropertyStringValue("password", properties, null, null, true));
String url = DSPropertyUtil.getPropertyStringValue("url", properties, null, null, true);
if (null != urlPattern) {
url = DSPropertyUtil.replace(url, "{}", urlPattern);
}
dsPool.setUrl(url);
dsPool.setPoolPreparedStatements(DSPropertyUtil.getPropertyBooleanValue("poolingStatements", properties, null, true, true)); // 开启池的prepared
// 池功能
dsPool.setRemoveAbandoned(DSPropertyUtil.getPropertyBooleanValue("removeAbandoned", properties, null, true, true));
dsPool.setRemoveAbandonedTimeout(DSPropertyUtil.getPropertyIntegerValue("removeAbandonedTimeout", properties, null, 1000, true));
dsPool.setLogAbandoned(DSPropertyUtil.getPropertyBooleanValue("logAbandoned", properties, null, true, true));
dsPool.setInitialSize(DSPropertyUtil.getPropertyIntegerValue("initialSize", properties, null, 2, true));
dsPool.setMaxActive(DSPropertyUtil.getPropertyIntegerValue("maxActive", properties, null, 8, true));
dsPool.setMaxIdle(DSPropertyUtil.getPropertyIntegerValue("maxIdle", properties, null, 8, true));
dsPool.setMinIdle(DSPropertyUtil.getPropertyIntegerValue("minIdle", properties, null, 0, true));
dsPool.setMaxWait(DSPropertyUtil.getPropertyIntegerValue("maxWait", properties, null, 10000, true));
dsPool.setTimeBetweenEvictionRunsMillis(DSPropertyUtil.getPropertyIntegerValue("timeBetweenEvictionRunsMillis", properties, null, -1, true));
dsPool.setTestOnBorrow(DSPropertyUtil.getPropertyBooleanValue("testOnBorrow", properties, null, false, true));
dsPool.setTestOnReturn(DSPropertyUtil.getPropertyBooleanValue("testOnReturn", properties, null, false, true));
dsPool.setTestWhileIdle(DSPropertyUtil.getPropertyBooleanValue("testWhileIdle", properties, null, false, true));
String validationQuery = DSPropertyUtil.getPropertyStringValue("validationQuery", properties, null, null, false);
if (null != validationQuery) {
dsPool.setValidationQuery(validationQuery);
}
int timeout = DSPropertyUtil.getPropertyIntegerValue("", properties, null, -1, false);
if (timeout > -1) {
dsPool.setValidationQueryTimeout(timeout);
}
dsPool.setNumTestsPerEvictionRun(DSPropertyUtil.getPropertyIntegerValue("numTestsPerEvictionRun", properties, null, 10, true));
dsPool.setMinEvictableIdleTimeMillis(DSPropertyUtil.getPropertyIntegerValue("minEvictableIdleTimeMillis", properties, null, 60000, true));
// mysql:select 1
// oracle:select 1 from dual
// sqlserver:select 1
// jtds:select 1
boolean openTest = DSPropertyUtil.getPropertyBooleanValue("openTest", properties, null, false, false);
if (openTest) {
try {
Connection conn = dsPool.getConnection();
conn.close();
log.info("test open database success.");
} catch (Exception e) {
throw new DataSourceException("test open database error: " + e.getMessage(), e);
}
}
return dsPool;
}