📄 connectionmanage.java
字号:
/**
* @author 肖建军
* 定义数据库连接的管理类
*/
package com.sunrise.chnl.sql;
import java.sql.Connection;
import org.w3c.dom.Document;
import java.util.Properties;
import com.sunrise.chnl.obj.PublicBean;
import com.sunrise.chnl.obj.ReadXml;
public class ConnectionManage {
private ConnectionFactory cf = null;
private ConnectionParam cp = null;
private FactoryParam fp = null;
private String MessageInfo = "0";
public ConnectionManage(){
}
/**
* 获取数据库连接
* @param p_ds_name:数据源名称
* @return
*/
public Connection getConnection(String p_ds_name){
Connection conn = null;
try{
if(cf == null){
startPool(p_ds_name);
}
if(cf !=null){
conn = cf.getFreeConnection();
}
}catch(Exception e){
MessageInfo = "获取连接时出现错误,错误原因:" + e.toString();
PublicBean.writelog("ERR-DC0006:" + MessageInfo,0);
}
return conn;
}
/**
* 获取数据库连接池参数信息
* @param p_ds_name 数据源名称
* @return
*/
private boolean getConfigure(String p_ds_name){
try{
ReadXml read_xml = new ReadXml();
Document documnet = read_xml.getDocument(PublicBean.getConfigFile());
String s_driver = read_xml.getConfig(documnet,"datasource",p_ds_name,"jdbc_driver");
String s_url = read_xml.getConfig(documnet,"datasource",p_ds_name,"databse_url");
String s_user = read_xml.getConfig(documnet,"datasource",p_ds_name,"databse_user");
String s_password = read_xml.getConfig(documnet,"datasource",p_ds_name,"databse_password");
String s_init_pool = read_xml.getConfig(documnet,"datasource",p_ds_name,"init_pool_size");
String s_max_pool = read_xml.getConfig(documnet,"datasource",p_ds_name,"max_pool_size");
String s_time_out = read_xml.getConfig(documnet,"datasource",p_ds_name,"time_out");
String s_manage_policy = read_xml.getConfig(documnet,"datasource",p_ds_name,"manage_policy");
int i_init_pool = 2;
int i_max_pool = 5;
int i_auto_release = 0;
int i_time_out = 0;
Connection conn = null;
try{
i_init_pool = Integer.valueOf(s_init_pool).intValue();
}catch(Exception e){
MessageInfo = "参数设置错误:初始连接数必须为数字型";
PublicBean.writelog("ERR-DC0002:" + MessageInfo,0);
return false;
}
try{
i_max_pool = Integer.valueOf(s_max_pool).intValue();
}catch(Exception e){
MessageInfo = "参数设置错误:最大连接数必须为数字型";
PublicBean.writelog("ERR-DC0003:" + MessageInfo,0);
return false;
}
try{
i_auto_release = Integer.valueOf(s_manage_policy).intValue();
}catch(Exception e){
MessageInfo = "参数设置错误:管理策略必须为数字型";
PublicBean.writelog("ERR-DC0004:" + MessageInfo,0);
return false;
}
try{
i_time_out = Integer.valueOf(s_time_out).intValue();
}catch(Exception e){
MessageInfo = "参数设置错误:超时时长必须为数字型";
PublicBean.writelog("ERR-DC0005:" + MessageInfo,0);
return false;
}
cp = new ConnectionParam(s_driver,s_url,s_user,s_password);
fp = new FactoryParam(i_max_pool,i_init_pool,i_auto_release,i_time_out);
}catch(Exception e){
MessageInfo = "获取数据库连接配置参数错误,错误原因:" + e.toString();
PublicBean.writelog("ERR-DC0001:" + MessageInfo,0);
return false;
}
return true;
}
/**
* 启动连接池
* @param p_ds_name 数据源名称
* @return
*/
public boolean startPool(String p_ds_name){
boolean b_start_reult = true;
try{
if(cf == null){
b_start_reult = getConfigure(p_ds_name);//获取配置参数
if(b_start_reult){
cf = new ConnectionFactory(cp,fp);//启动连接池
MessageInfo = cf.getMessageInfo();
}
}
}catch(Exception e){
MessageInfo = "启动连接池时出现错误,错误原因:" + e.toString();
PublicBean.writelog("ERR-DC0006:" + MessageInfo,0);
b_start_reult = false;
}
return b_start_reult;
}
/**
* 关闭连接池
* @return
*/
public boolean closePool(){
try{
if(cf != null){
cf.close();
MessageInfo = cf.getMessageInfo();
cf = null;
}
}catch(Exception e){
MessageInfo = "关闭连接池时出现错误,错误原因:" + e.toString();
PublicBean.writelog("ERR-DC0006:" + MessageInfo,0);
return false;
}
return true;
}
/**
* 重启连接池
* @param p_ds_name
* @return
*/
public boolean restartPool(String p_ds_name){
boolean b_restart_result = true;
try{
b_restart_result = getConfigure(p_ds_name);//获取配置参数
if(b_restart_result){
if(cf != null){
cf.close();
}
cf = new ConnectionFactory(cp,fp);//启动连接池
MessageInfo = cf.getMessageInfo();
}
}catch(Exception e){
MessageInfo = "重新启动连接池时出现错误,错误原因:" + e.toString();
PublicBean.writelog("ERR-DC0006:" + MessageInfo,0);
b_restart_result = false;
}
return b_restart_result;
}
/**
* @return
*/
public int getCurrentPoolSize(){
if(cf != null) return cf.getCurrentPoolSize();
else return 0;
}
/**
* @return
*/
public int getFreePoolSize(){
if(cf != null) return cf.getFreePoolSize();
else return 0;
}
/**
* @return
*/
public int getOnlinePoolSize(){
if(cf != null) return cf.getOnlinePoolSize();
else return 0;
}
/**
* @return
*/
public boolean getPoolStatus(){
if(cf !=null) return true;
else return false;
}
/**
* @return
*/
public String getMessageInfo(){
return MessageInfo;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -