📄 connectionpool.java
字号:
ConnStruct cs = null;
for (int i = 0; i < al_All.size(); i++) {
cs = al_All.get(i);
if (cs.GetEnable() == false || cs.GetAllot() == false ||
cs.GetUseDegree() == _MaxRepeatDegree) //不可以分配跳出本次循环。
continue;
if (cs.GetAllot() == true)
al.add(cs);
}
if (al.size() == 0)
return GetConnectionFormPool_Return(key, null, ConnLevel_None); //发出异常
else
return GetConnectionFormPool_Return(key,((ConnStruct) al.get(al.size() / 2)), ConnLevel_None); //返回连接
}
/**
* 申请一个连接资源,优先级-低,线程安全
* @param key Object 申请者
* @return Connection 申请到的连接对象
* @throws OccasionExecption
*/
private Connection GetConnectionFormPool_Bottom(Object key) throws OccasionException {
ConnStruct cs = null;
ConnStruct csTemp = null;
for (int i = 0; i < al_All.size(); i++)
{
csTemp = al_All.get(i);
if (csTemp.GetEnable() == false || csTemp.GetAllot() == false || csTemp.GetUseDegree() == _MaxRepeatDegree)//不可以分配跳出本次循环。
{
csTemp = null;
continue;
}
else//不是最合适的放置到最佳选择中
{
if (cs != null)
{
if (csTemp.GetUseDegree() > cs.GetUseDegree())
//与上一个最佳选择选出一个最佳的放置到cs中
cs = csTemp;
}
else
cs = csTemp;
}
}
return GetConnectionFormPool_Return(key, cs, ConnLevel_Bottom);//返回最合适的连接
}
/**
* 返回Connection对象,同时做获得连接时的必要操作
* @param key Object 申请者
* @param cs ConnStruct ConnStruct对象
* @param connLevel int 级别
* @return Connection 是否为只读属性
* @throws OccasionExecption
*/
private Connection GetConnectionFormPool_Return(Object key, ConnStruct cs, int connLevel) throws OccasionException {
try {
if (cs == null)
throw new Exception();
cs.Repeat();
hs_UseConn.put(key, cs);
if (connLevel == ConnLevel_ReadOnly) {
cs.SetAllot(false);
cs.SetIsRepeat(false);
}
} catch (Exception e) {
throw new OccasionException(); //连接资源耗尽,或错误的访问时机。
} finally {
UpdateAttribute(); //更新属性
}
return cs.GetConnection();
}
/**
* 释放申请的数据库连接对象,线程安全
* @param key Object 表示数据库连接申请者
* @throws NotKeyExecption 无法释放,不存在的key
* @throws PoolNotRunException 服务未启动
*/
public void DisposeConnection(Object key) throws NotKeyException, PoolNotRunException {
synchronized (hs_UseConn) {
ConnStruct cs = null;
if (_PoolState == PoolState_Run) {
if (!hs_UseConn.containsKey(key))
throw new NotKeyException(); //无法释放,不存在的key
cs = hs_UseConn.get(key);
cs.SetIsRepeat(true);
if (cs.GetAllot() == false)
if (cs.GetEnable() == true)
cs.SetAllot(true);
try {
cs.Remove();
} catch (Exception e) { //如果在设置连接期间出现问题就立刻将该连接设置失效
cs.Close();
cs.SetConnectionLost();
}
hs_UseConn.remove(key);
} else
throw new PoolNotRunException(); //服务未启动
}
UpdateAttribute(); //更新属性
}
//--------------------------------------------------------------------
/**
* 返回包装后的新的连接对象
* @param connString String 连接字符串
* @param driveString String 驱动字符串
* @return ConnStruct 装后的新的连接对象
*/
private ConnStruct CreateConnectionTemp(String connString, String driveString,String userID,String password) {
ConnStruct cs = null;
Connection con = null;
try {
con = CreateConnection(connString, driveString,userID,password);
cs = new ConnStruct(con, Calendar.getInstance().getTime());
return cs;
} catch (CreateConnectionException ex) {
return null;
}
}
/**
* <p>用指定类型创建连接</p>
*
* 内部实现方法:<br>
* protected Connection CreateConnection(String connString, String driveString,String userID,String password) throws CreateConnectionException {<br>
* try {<br>
* Class.forName(driveString);<br>
* Connection con = java.sql.DriverManager.getConnection(connString, userID, password);<br>
* return con;<br>
* } catch (Exception e) {<br>
* throw new CreateConnectionException();<br>
* }<br>
* }<br>
*
* <p>使用示例:</p>
* ConnectionPool c = new ConnectionPool() {<br>
* protected Connection CreateConnection(String connString, String driveString, String userID, String password) throws CreateConnectionException {<br>
* try {<br>
* Class.forName(driveString);<br>
* Connection con = java.sql.DriverManager.getConnection(connString, userID, password);<br>
* System.out.println("new db connection");<br>
* return con;<br>
* } catch (Exception e) {throw new CreateConnectionException();}<br>
* }<br>
* };<br>
* 在示例中使用匿名类来确定如何连接到数据库以及在每次创建数据库连接时要执行的方法,在实际使用当中您可以继承该类以重写该方法达到该目的
*
* @param conn String 连接字符串,该值可以由set_String方法设置
* @param driveString String 驱动字符串,该值可以由set_String方法设置
* @param userID String 登陆数据库使用的帐号,该值可以由set_DataBaseUser方法设置
* @param password String 登陆数据库使用的帐号密码,该值可以由set_DataBaseUser方法设置
* @return Connection 返回创建的连接
* @throws CreateConnectionExecption 如果在创建中出的现异
*/
protected Connection CreateConnection(String connString, String driveString,String userID,String password) throws CreateConnectionException {
try {
Class.forName(driveString);
Connection con = java.sql.DriverManager.getConnection(connString, userID, password);
return con;
} catch (Exception e) {
throw new CreateConnectionException();
}
}
//-------------------------------------------------------------------------------------------
/**
* 启动连接池服务,同步调用
* @throws PoolNotStopException
*/
public void StartServices() throws PoolNotStopException
{ StartServices(false); }
/**
* 启动连接池服务
* @param ansy 指定调用方式是否为异步调用,True为使用异步调用,异步调用指,用户调用该方法后,无须等待创建结束就可继续做其他操作
* @throws PoolNotStopException 服务已经运行或者未完全结束
*/
public void StartServices(boolean ansy) throws PoolNotStopException {
synchronized (this) {
threadCreate.createThreadMode = 0; //工作模式0
threadCreate.createThreadProcessRun = true;
threadCreate.createThreadProcessTemp = _MinConnection;
if (_PoolState == PoolState_UnInitialize)
threadCreate.start();
else if (_PoolState == PoolState_Stop)
threadCreate.interrupt();
else
throw new PoolNotStopException(); //服务已经运行或者未完全结束
threadCheck.StartTimer();
threadCheck.interrupt();//开始运行
}
if (!ansy)
while (threadCreate.getState()!=Thread.State.WAITING) {
//等待可能存在的创建线程结束
try {Thread.sleep(50);}
catch (InterruptedException e) { e.printStackTrace(); }
}
}
/**
* 停止服务同步的方法,线程安全
* @throws ResCallBackException 资源未全部回首
* @throws PoolNotRunException 服务没有运行
*/
public void StopServices() throws ResCallBackException, PoolNotRunException {
StopServices(false);
}
/**
* 停止服务,线程安全
* @param needs boolean 是否必须退出;如果指定为false与StartServices()功能相同,如果指定为true。将未收回的连接资源关闭,这将是危险的。认为可能你的程序正在使用此资源。
* @throws ResCallBackException 服务未启动
* @throws PoolNotRunException 服务没有运行
*/
public void StopServices(boolean needs) throws PoolNotRunException, ResCallBackException {
synchronized (this) {
if (_PoolState == PoolState_Run) {
synchronized (hs_UseConn) {
if (needs == true) //必须退出
hs_UseConn.clear();
else
if (hs_UseConn.size() != 0)
throw new ResCallBackException(); //连接池资源未全部回收
}
threadCheck.StopTimer();
while (threadCreate.getState()!=Thread.State.WAITING) {
//等待threadCreate事件结束
try {Thread.sleep(50);}
catch (InterruptedException e) { e.printStackTrace(); }
}
threadCreate.createThreadProcessRun = false;
while (threadCreate.getState() != Thread.State.WAITING) {
//等待可能存在的创建线程结束
try {Thread.sleep(50);}
catch (InterruptedException e) { e.printStackTrace(); }
}
synchronized (al_All) {
for (int i = 0; i < al_All.size(); i++)
al_All.get(i).Dispose();
al_All.clear();
}
_PoolState = PoolState_Stop;
} else
throw new PoolNotRunException(); //服务未启动
}
UpdateAttribute(); //更新属性
}
/**
* 如果想销毁线程池请务必调用该方法,该将终止线程池内部处于等待的线程。
*/
@SuppressWarnings("deprecation")
public synchronized void Dispose() {
try {
this.StopServices();
threadCreate.stop();
threadCheck.stop();
al_All=null;
hs_UseConn=null;
} catch (Exception e) {}
}
//-------------------------------------------------------------------------------------------
/**
* 测试ConnStruct是否过期
* @param cs ConnStruct 被测试的ConnStruct
*/
private void TestConnStruct(ConnStruct cs) {
//此次被分配出去的连接是否在此次之后失效
if (cs.GetUseDegree() == _MaxRepeatDegree)
cs.SetConnectionLost(); //超过使用次数
Calendar c = Calendar.getInstance();
c.setTime(cs.GetCreateTime());
c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + _Exist);
if (c.getTime().after(new Date()))
cs.SetConnectionLost(); //连接超时
}
/**
* 更新属性
*/
private synchronized void UpdateAttribute() {
int temp_readOnlyFormPool = 0;//连接池已经分配多少只读连接
int temp_potentRealFormPool = 0;//连接池中存在的实际连接数(有效的实际连接)
int temp_spareRealFormPool = 0;//空闲的实际连接
int temp_useRealFormPool = 0;//已分配的实际连接
int temp_spareFormPool =get_MaxConnectionFormPool();//目前可以提供的连接数
//---------------------------------
synchronized (hs_UseConn)
{
_UseFormPool = hs_UseConn.size();
}
//---------------------------------
ConnStruct cs = null;
synchronized (al_All)
{
_RealFormPool = al_All.size();
for (int i = 0; i < al_All.size(); i++)
{
cs = al_All.get(i);
//只读
if (cs.GetAllot() == false && cs.GetIsUse() == true && cs.GetIsRepeat() == false)
temp_readOnlyFormPool++;
//有效的实际连接
if (cs.GetEnable() == true)
temp_potentRealFormPool++;
//空闲的实际连接
if (cs.GetEnable() == true && cs.GetIsUse() == false)
temp_spareRealFormPool++;
//已分配的实际连接
if (cs.GetIsUse() == true)
temp_useRealFormPool++;
//目前可以提供的连接数
if (cs.GetAllot() == true)
temp_spareFormPool = temp_spareFormPool - cs.GetRepeatNow();
else
temp_spareFormPool = temp_spareFormPool - _MaxRepeatDegree;
}
}
_ReadOnlyFormPool = temp_readOnlyFormPool;
_PotentRealFormPool = temp_potentRealFormPool;
_SpareRealFormPool = temp_spareRealFormPool;
_UseRealFormPool = temp_useRealFormPool;
_SpareFormPool = temp_spareFormPool;
}
/**
* 获得该连接池的版本
* @return 返回版本信息
*/
public String getVerstion(){
return "1.2";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -