Tomcat 动态数据库连接池
package com.boguan.bte.util;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import com.boguan.bte.constant.IDBConstants;
/**
* 数据库工具类 名称: DBToolKit.java<br>
* 描述: <br>
* 类型: JAVA<br>
* 最近修改时间:2016年6月23日 下午6:02:50<br>
*
* @since 2016年6月23日
* @author
*/
public class DBToolKit {
/**
* 数据库用户
*/
public static String user;
/**
* 数据库密码
*/
public static String password;
/**
* 数据库驱动
*/
private static String url;
/**
* 驱动
*/
private static String driver;
/**
* 数据库连接池名称
*/
private static String JNDI;
/**
* 数据库连接池资源对象
*/
private static PoolProperties p = new PoolProperties();
/**
* 多库数据库连接池集合
*/
private static Map<String,Object> dataSources = new HashMap<String,Object>();
/**
* 构造器实现多例模式
*/
private DBToolKit(){
}
/**
* 动态获取数据库连接池
* @param dataBase
* @return
* @throws SQLException
*/
public static Connection getDynamicConnection(String dataBase) throws SQLException{
Connection conn = null;
DataSource datasource = null;
if(dataSources.containsKey(dataBase)){
datasource = (DataSource) dataSources.get(dataBase);
}else{
datasource = new DataSource();
p.setUrl(url + dataBase);
datasource.setPoolProperties(p);
dataSources.put(dataBase, datasource);
}
conn = datasource.getConnection();
return conn;
}
/**
* 初始化资源
*/
static {
user = PropertiesUtil.getDbProperty(IDBConstants.DBUSER);
password = PropertiesUtil.getDbProperty(IDBConstants.PASSWORD);
url = PropertiesUtil.getDbProperty(IDBConstants.URL);
driver = PropertiesUtil.getDbProperty(IDBConstants.DRIVER);
JNDI = PropertiesUtil.getDbProperty(IDBConstants.JNDI);
// 初始化数据库连接池资源文件对象
p.setDriverClassName(driver);
p.setUsername(user);
p.setPassword(password);
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
// p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
+ "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
}
/**
* 获取数据库连接池
*
* @return Connection
* @throws NamingException
* @throws SQLException
*/
public static Connection getConnection() throws NamingException, SQLException {
Context initCtx = new InitialContext();
DataSource ds = (DataSource) initCtx.lookup(JNDI);
Connection conn = ds.getConnection();
return conn;
}
/**
* 获取默认的数据库连接池配置
*
* @return Connection
* @throws NamingException
* @throws SQLException
*/
public static Connection getDefaultConnection() throws NamingException, SQLException {
Connection conn = getDynamicConnection(IDBConstants.BASEDATABASE);
return conn;
}
}