您的位置:首页 > 数据库

使用代理模式的数据库连接池

2013-09-09 22:41 239 查看
1、首先生成连接池,此处的链接池的思想是:把多个Connecion对象保存到LinkedList列表里面,主要是LinkedList提供了双向队列、栈的功能,并且它的插入、删除元素的性能比较好。然后当需要链接的时候就从链表中获取,当链表释放的时候不把链接关闭,而是把它添加到链表的末尾。这样就达到了链接的循环的使用。

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.LinkedList;

import java.util.Properties;

public class PoolConnection {
private static final String PROP_FILE = "mysql.ini";
private int maxCount = 10;// 定义连接池的最多链接数
private int minCount = 5;// 定义连接池的最少链接数
int current = 0;// 当前的链接数
// 生成一个链表装数据库链接
LinkedList<Connection> poolconnection = new LinkedList<Connection>();
public LinkedList<Connection> getPoolconnection() {
return poolconnection;
}
public void setPoolconnection(LinkedList<Connection> poolconnection) {
this.poolconnection = poolconnection;
}
// poolConnection构造方法
public PoolConnection() {
for (int i = 0; i < minCount; i++) {
poolconnection.add(this.createConnection());
}
}
// 创建Connection
public Connection createConnection() {
Connection conn = null;
ProxyConnection poxyconnection = null;
Connection  connection2 = null;
Properties connProp = new Properties();
try {
connProp.load(new FileInputStream(PROP_FILE));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String DRIVER = connProp.getProperty("driver");
String URL = connProp.getProperty("url");
String USER = connProp.getProperty("user");
String USERPASSWORD = connProp.getProperty("pass");
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USER, USERPASSWORD);
poxyconnection = new ProxyConnection(this);
connection2= (Connection)poxyconnection.bind(conn); 
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection2;
}
// 获取Connection链接,当当前的链接数小于最小的链接数的时候就直接在连接池获取,当链接数大于最小链接数就生成其它的数据连接。
public Connection getConnection() throws SQLException {
if (current < minCount) {
current++;
return this.poolconnection.removeFirst();
}
if (current < maxCount) {
current++;
return this.createConnection();
}
throw new SQLException("链接不够了");
}

}

2、运用代理实现Connection,重写Connection中的close方法。给用户的感觉是关闭了链接,但是实际上重写以后是把数据库链接添加到链表的末尾。这里使用的动态代理。

package com.wxw.jdbc;

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

import java.lang.reflect.Proxy;

import java.sql.Connection;

public class ProxyConnection implements InvocationHandler {

public Connection conn;//真实的被代理的类的实例
public PoolConnection poolconnection;
public Connection connection;//代理类的实例

public ProxyConnection(PoolConnection poolconnection) {
this.poolconnection = poolconnection;
}
public Connection bind(Connection conn){
this.conn = conn;
connection = (Connection) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{Connection.class}, this);

return connection;

}

@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if("close".equals(method.getName())){
poolconnection.getPoolconnection().addLast(connection);
poolconnection.current--;
}
return method.invoke(conn, args);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息