tools.c3p0.database.DBConnectionManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-autotest-tool Show documentation
Show all versions of java-autotest-tool Show documentation
This is an integration of autotest tools
package tools.c3p0.database;
/**
* Created by zhengyu06 on 2017/9/12
*/
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.log4j.Logger;
import org.testng.Reporter;
import tools.config.SystemConfig;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class DBConnectionManager {
private static Logger log = Logger.getLogger(DatabaseAccessImpl.class);
private static Map poolMap = new ConcurrentHashMap();
public static List getDBDetails(String dbInfo){
List list = new ArrayList<>();
int tempLength = 0;
String str = SystemConfig.getConfigInfomation(dbInfo);
Reporter.log(dbInfo+"="+str, true);
String[] tempGroup = str.split(",");
for (int i=0; i list = getDBDetails(dbInfo);
log.debug(">>>>>>>>>>>>>>>>>>>>> Begin");
// 建立数据库连接池
// String DRIVER_NAME = SystemConfig.getConfigInfomation("MYSQL_DRIVER_NAME"); // 驱动器
// String DATABASE_URL = SystemConfig.getConfigInfomation("MYSQL_DATABASE_URL"); // 数据库连接url
// String DATABASE_USER = SystemConfig.getConfigInfomation("MYSQL_DATABASE_USER"); // 数据库用户名
// String DATABASE_PASSWORD = SystemConfig.getConfigInfomation("MYSQL_DATABASE_PASSWORD"); // 数据库密�1�7码
String DRIVER_NAME = list.get(0).trim();
Reporter.log("DRIVER_NAME="+DRIVER_NAME,true);
String DATABASE_URL = list.get(1).trim();
Reporter.log("DATABASE_URL="+DATABASE_URL,true);
String DATABASE_USER = list.get(2).trim();
Reporter.log("DATABASE_USER="+DATABASE_USER,true);
String DATABASE_PASSWORD = list.get(3).trim();
Reporter.log("DATABASE_PASSWORD="+DATABASE_PASSWORD,true);
int Min_PoolSize = 2;
int Max_PoolSize = 10;
int Acquire_Increment = 1;
int Initial_PoolSize = 2;
int Idle_Test_Period = 3000;// 每隔3000s测试连接是否可以正常使用
String Validate = SystemConfig.getConfigInfomation("Validate");// 每次连接验证连接是否可用
if (Validate.equals("")) {
Validate = "false";
}
// 最小连接数
try {
Min_PoolSize = Integer.parseInt(SystemConfig.getConfigInfomation("Min_PoolSize"));
Reporter.log("test: "+ SystemConfig.getConfigInfomation("Min_PoolSize"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 增量条数
try {
Acquire_Increment = Integer.parseInt(SystemConfig.getConfigInfomation("Acquire_Increment"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 最大连接数
try {
Max_PoolSize = Integer.parseInt(SystemConfig.getConfigInfomation("Max_PoolSize"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 初始化连接数
try {
Initial_PoolSize = Integer.parseInt(SystemConfig.getConfigInfomation("Initial_PoolSize"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 每隔3000s测试连接是否可以正常使用
try {
Idle_Test_Period = Integer.parseInt(SystemConfig.getConfigInfomation("Idle_Test_Period"));
} catch (Exception ex) {
ex.printStackTrace();
}
try {
cpds = new ComboPooledDataSource();//创建新的ComboPooledDataSource对象。
cpds.setDriverClass(DRIVER_NAME); // 驱动器
cpds.setJdbcUrl(DATABASE_URL); // 数据库url
cpds.setUser(DATABASE_USER); // 用户名
cpds.setPassword(DATABASE_PASSWORD); //密码
/*以下c3p0的设置为固定值,各个库的连接池均一样*/
cpds.setInitialPoolSize(Initial_PoolSize); // 初始化连接池大小
cpds.setMinPoolSize(Min_PoolSize); // 最少连接数
cpds.setMaxPoolSize(Max_PoolSize); // 最大连接数
cpds.setAcquireIncrement(Acquire_Increment); // 连接数的增量
cpds.setIdleConnectionTestPeriod(Idle_Test_Period); // �1�7测连接有效的时间间隔
cpds.setTestConnectionOnCheckout(Boolean.getBoolean(Validate)); // 每次连接验证连接是否可用
cpds.setAcquireRetryAttempts(3);
cpds.setAcquireRetryDelay(10);
cpds.setIdentityToken("111111");
} catch (Exception ex) {
ex.printStackTrace();
}
log.debug(">>>>>>>>>>>>>>>>>>>>> End");
return cpds;
}
public static Connection getConnection(String dbInfo) {// 获取数据库连接
Connection connection = null;
try {
if (!poolMap.containsKey(dbInfo)) {
// init(dbInfo);//让ComboPooledDataSource类的对象初始化设置。
poolMap.put(dbInfo, init(dbInfo));//可以通过map来存储多个数据库。这是理念,本项目并未实现此程度。
Reporter.log(dbInfo + " has been initiated.", true);
}
connection = poolMap.get(dbInfo).getConnection(); // 通过ComboPooledDataSource类的getConnection()获取连接
Reporter.log(dbInfo + " has got a new Connection.",true);
} catch (SQLException ex) {
ex.printStackTrace();
}
return connection;
}
public static Map getPoolMap(){
return poolMap;
}
public static void release() {
int mapSize = poolMap.size();
Reporter.log("poolMap.Size()="+mapSize,true);
Set mapKeySet = null;
ComboPooledDataSource cpds = null;
try {
if (mapSize > 0){
mapKeySet = poolMap.keySet();
Iterator iterator = mapKeySet.iterator();
while (iterator.hasNext()){
String currDbInfo = iterator.next();
cpds = poolMap.get(currDbInfo);
if (cpds != null) {
cpds.close();
}
poolMap.remove(currDbInfo);
}
Reporter.log("All DB Connections of All Connection Pools have been closed.",true);
}
//以上方法内容将 poolMap中的全部数据库连接池 关闭。
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void release(String dbInfo) {
int mapSize = poolMap.size();
Reporter.log("poolMap.Size()="+mapSize,true);
Set mapKeySet = null;
ComboPooledDataSource cpds = null;
try {
if (mapSize > 0 && poolMap.containsKey(dbInfo)){
cpds = poolMap.get(dbInfo);
if (cpds != null) {
cpds.close();
}
poolMap.remove(dbInfo);
Reporter.log("All Connections of "+dbInfo+" DB's Connection Pool have been closed.",true);
}
//以上方法内容将 poolMap中的全部数据库连接池 关闭。
} catch (Exception ex) {
ex.printStackTrace();
}
}
}