package com.sql.pool;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.sql.handler.Deploy;
import com.sql.handler.VectorConnection;
import com.sql.handler.util.LogFactory;
public class FixedConnectionPool extends AbstractConnectionPool{
/*线程安全数组*/
private static volatile VectorConnection<Connection> pool = new VectorConnection<>();
private static volatile VectorConnection<Connection> recordPool = new VectorConnection<>();
private static volatile VectorConnection<Connection> recordNewPool = new VectorConnection<>();
private static volatile Connection selectConnection;
/*连接池中储存的最大数量*/
private static int POOL_SIZE;
/*当线程池耗尽时,最大新建数量*/
private static int NEW_POOL_SIZE;
/*信号量*/
private static Semaphore semp = null;
/*检查机制的循环间隔时间*/
private final static int MS_TIME = 60;
/*不同于synchronized的另一种锁机制*/
private final static Lock SELECT_LOCK = new ReentrantLock();
private final static Lock RELEASE_LOCK = new ReentrantLock();
private final static Lock CONNECTION_LOCK = new ReentrantLock();
/*定时器*/
private static ScheduledExecutorService scheduledExecutorService;
/*构造方法,不做任何工作*/
private FixedConnectionPool() {}
/**
* 做初始化工作
* @throws InterruptedException
*/
private synchronized static void init() throws ClassNotFoundException, SQLException{
Logs.d("正在初始化连接池..");
semp = new Semaphore(NEW_POOL_SIZE);
selectConnection = load();
for (int i = 0; i < POOL_SIZE; i++) {
try {
padCachePool();
} catch (InterruptedException e) {}
}
Logs.d("初始化完毕");
}
/**
* 得到select使用的连接,查询不同于其他三项,查询可以并发进行执行,
* 所以我们只需提供一个连接来供所有用户使用
*/
private static Connection getSelectConnection() throws SQLException, InterruptedException, ClassNotFoundException{
return isOkConnection(selectConnection) ? selectConnection : loadSelectConnection();
}
/**
* 加载查询使用的连接
* @return
* @throws InterruptedException
* @throws SQLException
* @throws ClassNotFoundException
*/
private static Connection loadSelectConnection() throws InterruptedException, SQLException, ClassNotFoundException{
if(SELECT_LOCK.tryLock(8L,TimeUnit.SECONDS)){
loadSelectConnctionHandler();
SELECT_LOCK.unlock();
}
Logs.d("成功拿到连接");
return selectConnection;
}
/**
* 加载查询连接的处理方法
* @throws ClassNotFoundException
* @throws SQLException
*/
private static void loadSelectConnctionHandler() throws ClassNotFoundException, SQLException{
if(selectConnection == null || selectConnection.isClosed()){
selectConnection = load();
}
}
/**
* 返回连接到连接池
* 在这里进行控制,如果连接池里的连接数大于我们规定的数量,则对此连接进行关闭
*/
private static void release(Connection con) throws SQLException, InterruptedException {
if(!isOkConnection(con)){
return;
}
if(pool.contains(con)){
return;
}
releaseHandler(con);
}
/**
* 处理回收连接的方法
* @param con
* @throws InterruptedException
* @throws SQLException
*/
private static void releaseHandler(Connection con) throws InterruptedException, SQLException{
if(pool.size() >= POOL_SIZE){
con.close();
return;
}
if(RELEASE_LOCK.tryLock(8L,TimeUnit.SECONDS)){
if(pool.size() < POOL_SIZE){
Logs.d("成功回收连接");
pool.add(con);
RELEASE_LOCK.unlock();
} else{
con.close();
RELEASE_LOCK.unlock();
}
}
}
/**
* 新建连接
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
private static Connection getNewConnection() throws SQLException, ClassNotFoundException{
return newConnectionHandler();
}
/**
* 建立新连接的处理方法
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
private static Connection newConnectionHandler() throws SQLException, ClassNotFoundException{
if (recordNewPool.size() < NEW_POOL_SIZE){
Connection conn = load();
if(isOkConnection(conn)){
recordNewPool.add(conn);
Logs.d("成功拿到新建的连接");
return conn;
}
}
return null;
}
/** 返回连接池中的一个数据库连接
* 如果连接池中已经耗尽了Connextion
* 则创建新的使用
*/
private static Connection getConnection() throws ClassNotFoundException, InterruptedException, SQLException {
Connection conn = getCacheConnection();
if(isOkConnection(conn)){
return conn;
}
do{
conn = getConnectionHandler();
}while(!isOkConnection(conn));
return conn;
}
/**
* 控制新建连接的数目
* 通过信号量
* @return
* @throws InterruptedException
* @throws ClassNotFoundException
* @throws SQLException
*/
private static Connection controlNewConnection() throws InterruptedException, ClassNotFoundException, SQLException{
Connection conn;
if (recordNewPool.size() < NEW_POOL_SIZE) {
semp.acquire();
if(isMaximum(semp)){
conn = getNewConnection();
semp.release();
return conn;
}
semp.release();
}
return getWaitConnection();
}
/**
* 补充连接池
* @throws InterruptedException
* @throws SQLException
* @throws ClassNotFoundException
*/
private static synchronized void padCachePool() throws InterruptedException, ClassNotFoundException, SQLException{
while((recordPool.size() + pool.size()) < POOL_SIZE){
pool.add(load());
}
}
/**
* 得到连接的处理方法
* @return
* @throws InterruptedException
* @throws SQLException
* @throws ClassNotFoundException
*/
private static Connection getConnectionHandler() throws InterruptedException, SQLException, ClassNotFoundException{
Connection conn = controlNewConnection();
return isOkConnection(conn) ? conn : null;
}
private static Connection getCacheHandlerConnection() throws InterruptedException, SQLException{
Connection conn;
if((recordPool.size() + pool.size()) > POOL_SIZE){
return null;
}
if(CONNECTION_LOCK.tryLock(8L,TimeUnit.SECONDS)){
while((recordPool.size() + pool.size())< POOL_SIZE){
conn = load();
if(isOkConnection(conn)){
recordPool.add(conn);
CONNECTION_LOCK.unlock();
return conn;
}
}
CONNECTION_LOCK.unlock();
}
return null;
}
/**
* 对新建连接通过信号量进行控制,这里提供判断条件
* @return
*/
private static boolean isMaximum(Semaphore semp){
final int i = recordNewPool.size();
final int j = semp.availablePermits();
final int z = NEW_POOL_SIZE - j;
return (z + i) <= NEW_POOL_SIZE ? true : false;
}
/**
* 得到缓存中的连接
* @return
* @throws SQLException
*/
private static Connection getCacheConnection() throws SQLException{
while(pool.size() > 0){
Connection conn = getCacheConnectionHandler();
if (conn != null) {
re
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
XDCPool-master.zip (83个子文件)
XDCPool-master
.classpath 295B
.settings
org.eclipse.jdt.core.prefs 587B
src
com
sql
pool
Text.java 3KB
FixedConnectionPool.java 17KB
ChooseConectionPool.java 1KB
ConnectionLoad.java 701B
OnlyNewConnectionPool.java 15KB
OnlyCacheConnectionPool.java 13KB
CacheConnectionPool.java 9KB
AbstractConnectionPool.java 343B
IBuilder.java 302B
handler
ManyConnectionHandler.java 2KB
SQLHandlerFactor.java 255B
BuilderInstance.java 1KB
SQLContext.java 1KB
XDCPool.java 5KB
TextMyConnection.java 8KB
VectorConnection.java 849B
ManyConnectionInstance.java 361B
DriverClassName.java 311B
SQLHandler.java 4KB
AbstractConnectionAllot.java 4KB
ISQLHandler.java 878B
Deploy.java 2KB
util
SerializableUtil.java 2KB
ISerializableFileName.java 154B
ILogs.java 293B
Logs.java 609B
CacheConnectionArray.java 999B
Arrays.java 1KB
ArrayItem.java 247B
ObjEqual.java 262B
LogsEmpty.java 681B
LogFactory.java 204B
.idea
.name 4B
workspace.xml 50KB
misc.xml 2KB
compiler.xml 686B
modules.xml 248B
encodings.xml 159B
copyright
profiles_settings.xml 74B
bin
com
sql
pool
FixedConnectionPool.class 8KB
Text.class 2KB
OnlyNewConnectionPool$Builder.class 5KB
AbstractConnectionPool.class 582B
OnlyNewConnectionPool.class 7KB
ChooseConectionPool.class 2KB
ChooseConectionPool$TYPE.class 2KB
IBuilder.class 364B
OnlyCacheConnectionPool.class 6KB
Text$1.class 856B
CacheConnectionPool.class 4KB
OnlyCacheConnectionPool$Builder.class 5KB
ConnectionLoad.class 1KB
FixedConnectionPool$Builder.class 5KB
CacheConnectionPool$Builder.class 3KB
handler
ManyConnectionInstance.class 688B
SQLHandlerFactor.class 746B
ManyConnectionHandler.class 3KB
BuilderInstance.class 2KB
VectorConnection.class 1KB
SQLContext.class 1KB
XDCPool.class 3KB
Deploy.class 2KB
TextMyConnection.class 8KB
ISQLHandler.class 584B
DriverClassName.class 530B
SQLHandler.class 2KB
util
ISerializableFileName.class 225B
ObjEqual.class 769B
LogsEmpty.class 952B
Arrays.class 3KB
LogFactory.class 540B
CacheConnectionArray$1.class 1KB
ArrayItem.class 921B
Logs.class 2KB
ILogs.class 240B
SerializableUtil$1.class 747B
CacheConnectionArray.class 2KB
SerializableUtil.class 2KB
AbstractConnectionAllot.class 2KB
XSQL.iml 469B
.project 363B
共 83 条
- 1
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6742
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功