import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.sql.DataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
public class JdbcPool implements DataSource{
private static final Log log = LogUtils.getLog(JdbcPool.class);
/**
* @Field: listConnections
* 使用LinkedList集合来存放数据库链接,
* 由于要频繁读写List集合,所以这里使用LinkedList存储数据库连接比较合适
*/
// private static LinkedList<Connection> listConnections = new LinkedList<Connection>();
private static ConcurrentLinkedQueue<Connection> listConnections = new ConcurrentLinkedQueue<Connection>();
private final static String filepath = Path.getCurrentPath() + "/config/db.properties";
static{
//在静态代码块中加载db.properties数据库配置文件
File file = new File(filepath);
Properties prop = new Properties();
try {
InputStream in= new FileInputStream(file);
prop.load(in);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
//数据库连接池的初始化连接数大小
int jdbcPoolInitSize =Integer.parseInt(prop.getProperty("jdbcPoolInitSize"));
//加载数据库驱动
Class.forName(driver);
for (int i = 0; i < jdbcPoolInitSize; i++) {
Connection conn = DriverManager.getConnection(url, username, password);
//将获取到的数据库连接加入到listConnections集合中,listConnections集合此时就是一个存放了数据库连接的连接池
listConnections.add(conn);
}
} catch (Exception e) {
e.printStackTrace();
log.error(e);
}
}
public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public void setLogWriter(PrintWriter out) throws SQLException {
// TODO Auto-generated method stub
}
public void setLoginTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub
}
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
/* 获取数据库连接
* @see javax.sql.DataSource#getConnection()
*/
public Connection getConnection() throws SQLException {
// 如果数据库连接池中的连接对象的个数大于0
if (listConnections.size() > 0) {
// if (!listConnections.isEmpty()) {
// 从listConnections集合中获取一个数据库连接
final Connection conn = listConnections.poll();
// 返回Connection对象的代理对象
return (Connection) Proxy.newProxyInstance(JdbcPool.class.getClassLoader(),
new Class[] { Connection.class }, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!method.getName().equals("close")) {
return method.invoke(conn, args);
} else {
// 如果调用的是Connection对象的close方法,就把conn还给数据库连接池
listConnections.add(conn);
return null;
}
}
});
} else {
return null;
}
}
public Connection getConnection(String username, String password)
throws SQLException {
return null;
}
/**
* 获取多线程的线程池大小
* @return
*/
public static int getExecutorPoolSize(){
return executorPoolSize;
}
/**
* 获取多线程中每个任务中每次查询的数据条数
* @return
*/
public static int getQuerySizeOneTime(){
return querySizeOneTime;
}
/**
* 获取redis数据存储的有效时间
* @return
*/
public static int getExpireTime(){
return expiretime;
}
}
线程安全的jdbc连接池
需积分: 21 107 浏览量
2015-12-08
17:59:51
上传
评论
收藏 3KB RAR 举报
m20103410
- 粉丝: 0
- 资源: 9
最新资源
- 校园帮项目,毕业设计/课程设计/javaWeb/SSM
- C++ plotting library,matplotlib-cpp-master.zip
- 案例源码matplotlib-examples-master.zip
- 基于JavaScript 实现的KMP 算法
- 基于C++实现二叉树的创建,遍历,添加,查找与删除
- 基于C语言实现二叉树的基本操作
- 毕业设计基于STM32的测量温度与压力的数据处理设计C语言完整源码+论文.zip
- 基于MATLAB的PCA算法人脸识别项目源码+GUI界面+说明文档.zip
- 基于STM32的测量温度与压力的数据处理设计源码+论文(毕业设计).zip
- Vision Transformer 网络对不同氨气氧气浓度轨迹RAS 图像数据集的分类,包含训练权重和数据集、迁移学习
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈