JDBC连接池
来源:互联网 发布:淘宝签单时间怎么算 编辑:程序博客网 时间:2024/06/05 06:34
package exercise;
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.util.LinkedList;
import java.util.Properties;
public class ConnPool {
private String driver;
private String url;
private String username;
private String password;
private int init_count; //初始化中的数量
private int max_count; //最大的数量
private int curr_count; //当前连接的数量
private LinkedList<Connection> mList = new LinkedList<>();
public ConnPool() throws Exception{
for(int i=0;i<init_count;i++){
Connection conn = creatConn();
curr_count++;
mList.add(conn);
}
}
public ConnPool(Properties pro) throws Exception{
this();
this.driver=pro.getProperty("driver");
this.url=pro.getProperty("url");
this.username=pro.getProperty("username");
this.password=pro.getProperty("username");
this.init_count=Integer.parseInt(pro.getProperty("init_count"));
this.max_count=Integer.parseInt(pro.getProperty("max_count"));
}
//
private Connection creatConn() throws Exception{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
Connection proxy = (Connection) Proxy.newProxyInstance(
conn.getClass().getClassLoader(),
new Class[] {Connection.class},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
Object result = null;
if("close".equals(method.getName())){
if(init_count>mList.size()){
mList.add(conn);
}else{
result= method.invoke(conn, args);
}
}else{
result= method.invoke(conn, args);
}
return result;
}
}
);
return proxy ;
}
public Connection getConnection() throws Exception{
if(mList.size()>0){
return mList.removeFirst();
}
if(curr_count<max_count){
curr_count++;
return creatConn();
}
throw new Exception("达到最大连接数....");
}
}
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.util.LinkedList;
import java.util.Properties;
public class ConnPool {
private String driver;
private String url;
private String username;
private String password;
private int init_count; //初始化中的数量
private int max_count; //最大的数量
private int curr_count; //当前连接的数量
private LinkedList<Connection> mList = new LinkedList<>();
public ConnPool() throws Exception{
for(int i=0;i<init_count;i++){
Connection conn = creatConn();
curr_count++;
mList.add(conn);
}
}
public ConnPool(Properties pro) throws Exception{
this();
this.driver=pro.getProperty("driver");
this.url=pro.getProperty("url");
this.username=pro.getProperty("username");
this.password=pro.getProperty("username");
this.init_count=Integer.parseInt(pro.getProperty("init_count"));
this.max_count=Integer.parseInt(pro.getProperty("max_count"));
}
//
private Connection creatConn() throws Exception{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
Connection proxy = (Connection) Proxy.newProxyInstance(
conn.getClass().getClassLoader(),
new Class[] {Connection.class},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
Object result = null;
if("close".equals(method.getName())){
if(init_count>mList.size()){
mList.add(conn);
}else{
result= method.invoke(conn, args);
}
}else{
result= method.invoke(conn, args);
}
return result;
}
}
);
return proxy ;
}
public Connection getConnection() throws Exception{
if(mList.size()>0){
return mList.removeFirst();
}
if(curr_count<max_count){
curr_count++;
return creatConn();
}
throw new Exception("达到最大连接数....");
}
}
阅读全文
0 0
- JDBC连接池
- Jdbc连接池介绍
- JDBC与连接池
- JDBC与连接池
- Jdbc连接池介绍
- JDBC连接池实现
- jdbc连接池
- jdbc连接池
- JDBC+MySQL连接池
- jdbc连接池
- JDBC 连接池代码
- JDBC之连接池
- JDBC之连接池
- JDBC 连接池
- jdbc连接池实现
- jdbc连接池
- jdbc连接池总结
- Spring2 JDBC 连接池
- JAVA监听器
- ThreadLocal 源码解析和使用
- JS操作JSON总结
- 微信小程序和微信公众号开发注意事项(有些是从微信开发文档中摘抄出来的)
- Service中判断状态栏是否隐藏
- JDBC连接池
- 1011. A+B和C (15)
- 获取一组数两两组合情况
- Python 实现中值滤波、均值滤波
- windows消息循环机制
- IOS 数据结构总结
- Spark的核心RDD(Resilient Distributed Datasets弹性分布式数据集)
- springMVC 注入对象失败
- 二层三层路由转发图示