今天看了下红薯大哥发的oschina的DBManager,根据哪里修改了下,自己也做了个DBManager,但是测试出来的结构跟预想中不一样,好像没有发挥到连接池的作用。请大侠们指导下
DBManager代码
package ben.DBUtils;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.log4j.Logger;
/**
* Created with IntelliJ IDEA.
* User: Ben
* Date: 13-7-3
* Time: 下午5:06
* To change this template use File | Settings | File Templates.
*/
public class DBManager {
//保存数据库连接池的容器
private static HashMap datasourceMap = new HashMap();
private final static ThreadLocal conns = new ThreadLocal();
private static Logger logger = Logger.getLogger(DBManager.class);
static {
InitDataSource(null);
}
/**
* 初始化连接池
* */
private final static void InitDataSource(Properties dbProperties){
try{
if (dbProperties == null){
dbProperties = new Properties();
dbProperties.load(DBManager.class.getClassLoader().getResourceAsStream("dbInfo.properties"));
}
//获取数据源名称
Set keys = new HashSet();
for (Object key : dbProperties.keySet()){
String skey = (String)key;
Integer point = skey.indexOf(".");
keys.add(skey.substring(0, point));
}
//实例化DataSource并放入容器中
for (String key : keys)
{
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(dbProperties.getProperty(key + "." + "Driver"));
ds.setJdbcUrl(dbProperties.getProperty(key + "." + "Url"));
ds.setUser(dbProperties.getProperty(key + "." + "User"));
ds.setPassword(dbProperties.getProperty(key + "." + "Password"));
ds.setMaxPoolSize(Integer.parseInt(dbProperties.getProperty(key + "." + "MaxPoolSize")));
ds.setMinPoolSize(Integer.parseInt(dbProperties.getProperty(key + "." + "MinPoolSize")));
ds.setMaxIdleTime(Integer.parseInt(dbProperties.getProperty(key + "." + "MaxIdleTime")));
ds.setInitialPoolSize(Integer.parseInt(dbProperties.getProperty(key + "." + "InitialPoolSize")));
ds.setAcquireIncrement(Integer.parseInt(dbProperties.getProperty(key + "." + "AcquireIncrement")));
ds.setAcquireRetryAttempts(Integer.parseInt(dbProperties.getProperty(key + "." + "AcquireRetryAttempts")));
ds.setAcquireRetryDelay(Integer.parseInt(dbProperties.getProperty(key + "." + "AcquireRetryDelay")));
ds.setMaxStatements(Integer.parseInt(dbProperties.getProperty(key + "." + "MaxStatements")));
ds.setIdleConnectionTestPeriod(Integer.parseInt(dbProperties.getProperty(key + "." + "IdleConnectionTestPeriod")));
ds.setCheckoutTimeout(Integer.parseInt(dbProperties.getProperty(key + "." + "CheckoutTimeout")));
ds.setTestConnectionOnCheckin(Boolean.parseBoolean(dbProperties.getProperty(key + "." + "TestConnectionOnCheckin")));
ds.setTestConnectionOnCheckout(Boolean.parseBoolean(dbProperties.getProperty(key + "." + "TestConnectionOnCheckout")));
datasourceMap.put(key, ds);
Connection conn = getConnection(key);
DatabaseMetaData metaData = conn.getMetaData();
logger.info("Using DataSource: " + conn.getClass().getName());
closeConnection();
}
}
catch (Exception e){
logger.error("DataSource Error:" + e.toString());
}
}
/**
* 断开连接池
* */
public final static void closeDataSource(String dataSourceName){
try{
ComboPooledDataSource dataSource = datasourceMap.get(dataSourceName);
dataSource.close();
}
catch (Exception e){
logger.error("Unable to destroy DataSource! " + e.toString());
}
}
/**
* 获取1个连接
* @return Connection
* */
public final static Connection getConnection(String dataSourceName) throws SQLException{
Connection conn = conns.get();
if (conn == null || conn.isClosed()){
ComboPooledDataSource dataSource = datasourceMap.get(dataSourceName);
conn = dataSource.getConnection();
conns.set(conn);
}
return conn;
}
/**
* 关闭连接
* */
public final static void closeConnection(){
Connection conn = conns.get();
try{
if (conn != null && !conn.isClosed()){
conn.setAutoCommit(true);
conn.close();
}
}
catch (SQLException ex){
logger.error("Unable to close connection! " + ex.toString());
}
conns.set(null);
}
}
dbInfo.properties
MyWeb.AcquireIncrement = 2
MyWeb.AcquireRetryAttempts = 30
MyWeb.AcquireRetryDelay = 1000
MyWeb.CheckoutTimeout = 3000
MyWeb.Driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
MyWeb.IdleConnectionTestPeriod = 18000
MyWeb.InitialPoolSize = 2
MyWeb.MaxIdleTime = 300
MyWeb.MaxPoolSize = 100
MyWeb.MaxStatements = 1000
MyWeb.MinPoolSize = 2
MyWeb.Password = 123
MyWeb.TestConnectionOnCheckin = false
MyWeb.TestConnectionOnCheckout = false
MyWeb.Url = jdbc:sqlserver://localhost:1433;DatabaseName=MyWeb
MyWeb.User = sa
DemoData.AcquireIncrement = 2
DemoData.AcquireRetryAttempts = 30
DemoData.AcquireRetryDelay = 1000
DemoData.CheckoutTimeout = 3000
DemoData.Driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
DemoData.IdleConnectionTestPeriod = 18000
DemoData.InitialPoolSize = 2
DemoData.MaxIdleTime = 300
DemoData.MaxPoolSize = 100
DemoData.MaxStatements = 1000
DemoData.MinPoolSize = 2
DemoData.Password = 123
DemoData.TestConnectionOnCheckin = false
DemoData.TestConnectionOnCheckout = false
DemoData.Url = jdbc:sqlserver://localhost:1433;DatabaseName=DemoData
DemoData.User = sa
测试代码
package ben.DBUtils;
/**
* Created with IntelliJ IDEA.
* User: Ben
* Date: 13-7-6
* Time: 上午12:28
* To change this template use File | Settings | File Templates.
*/
public class TestDBManager {
public static void main(String[] args) {
for (Integer i=0; i<5; i++){
try{
System.out.println(DBManager.getConnection("MyWeb"));
System.out.println(DBManager.getConnection("DemoData"));
DBManager.closeConnection();
System.out.println("---------------------------");
System.out.println(DBManager.getConnection("MyWeb"));
System.out.println(DBManager.getConnection("DemoData"));
DBManager.closeConnection();
System.out.println("---------------------------");
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
}
构想中,打印出来的应该结构应该是相同的,才说明重复使用了连接。但是怎么打印出来的都不一样呢?
com.mchange.v2.c3p0.impl.NewProxyConnection@ff5c225
com.mchange.v2.c3p0.impl.NewProxyConnection@ff5c225
---------------------------
com.mchange.v2.c3p0.impl.NewProxyConnection@5bafcc2c
com.mchange.v2.c3p0.impl.NewProxyConnection@5bafcc2c
---------------------------
com.mchange.v2.c3p0.impl.NewProxyConnection@ded0b93
com.mchange.v2.c3p0.impl.NewProxyConnection@ded0b93
---------------------------
com.mchange.v2.c3p0.impl.NewProxyConnection@25057fe
com.mchange.v2.c3p0.impl.NewProxyConnection@25057fe
---------------------------
com.mchange.v2.c3p0.impl.NewProxyConnection@198ad24a
com.mchange.v2.c3p0.impl.NewProxyConnection@198ad24a
---------------------------
com.mchange.v2.c3p0.impl.NewProxyConnection@1228bd68
com.mchange.v2.c3p0.impl.NewProxyConnection@1228bd68
---------------------------
com.mchange.v2.c3p0.impl.NewProxyConnection@36db09fa
com.mchange.v2.c3p0.impl.NewProxyConnection@36db09fa
---------------------------
com.mchange.v2.c3p0.impl.NewProxyConnection@35886ba1
com.mchange.v2.c3p0.impl.NewProxyConnection@35886ba1
---------------------------
com.mchange.v2.c3p0.impl.NewProxyConnection@17d8986
com.mchange.v2.c3p0.impl.NewProxyConnection@17d8986
---------------------------
com.mchange.v2.c3p0.impl.NewProxyConnection@2bb9e538
com.mchange.v2.c3p0.impl.NewProxyConnection@2bb9e538
---------------------------
Process finished with exit code 0