package com.hh.util;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
/**
*
* (Project name):MySQLConnectonPool
* <p>
* (Package name):com.lixunhui.util
* <p>
* (Class name):ConnectionPool
* <p>
* (Description):
* <p>
* (Author) :[email protected]
* <p>
* (Create Date):2013-3-30 1:58:35
* <p>
* :hh
* <p>
* :2013-3-30 1:58:35
* <p>
* :
* <p>
* (Version): @version 1.0
*/
public class ConnectionPool {
static Logger log=Logger.getLogger(ConnectionPool.class);
private static BoneCP connectionPool;
/**
* Connection
*
* @Author [email protected]
* @Description
* @return
*/
public static Connection getConnection() {
Connection connection = null;
int totalConnection=0;
int freeConnection=0;
int busyConnection=0;
if(null==connectionPool){
log.debug(" , ");
String username = ResourcesProp.getValue("username");
String password = ResourcesProp.getValue("password");
String url = ResourcesProp.getValue("url");
String driver = ResourcesProp.getValue("driver");
try {
Class.forName(driver);
BoneCPConfig config = new BoneCPConfig();
// URL
config.setJdbcUrl(url);
//
config.setUsername(username);
//
config.setPassword(password);
// 60
config.setIdleConnectionTestPeriod(60);
//
config.setIdleMaxAge(240);
//
config.setMaxConnectionsPerPartition(100);
//
config.setMinConnectionsPerPartition(5);
// ,
config.setAcquireIncrement(5);
config.setReleaseHelperThreads(10);
config.setPartitionCount(3);
connectionPool = new BoneCP(config);
connection = connectionPool.getConnection();
totalConnection=connectionPool.getTotalCreatedConnections();
freeConnection=connectionPool.getTotalFree();
busyConnection=connectionPool.getTotalLeased();
log.debug(" , "+totalConnection+" "
+freeConnection+" "+busyConnection);
} catch (Exception e) {
log.error(" , "+totalConnection+" "
+freeConnection+" "+busyConnection, e);
e.printStackTrace();
}
}else{
log.debug(" , ");
try {
connection=connectionPool.getConnection();
totalConnection=connectionPool.getTotalCreatedConnections();
freeConnection=connectionPool.getTotalFree();
busyConnection=connectionPool.getTotalLeased();
log.debug(" , "+totalConnection+" "
+freeConnection+" "+busyConnection);
} catch (SQLException e) {
log.error(" ",e);
e.printStackTrace();
}
}
return connection;
}
}