使用代理模式的数据库连接池
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);
}
}
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);
}
}
相关文章推荐
- 设计模式之代理模式(实现自己的数据库连接池)
- 使用设计模式构建通用数据库访问类
- 哪些情况下,会使用reset log模式打开数据库?
- 学习实践:使用模式,原则实现一个C++数据库访问类
- 使用反射机制调用属性和私有成员与代理模式的介绍
- 数据库使用连接池到eclipse再到安卓studio的完整流程(对象)
- 使用自动生成java文件和自动编译的静态代理模式
- 使用单例模式设计JDBC连接数据库
- Android中使用动态代理模式
- java 使用策略模式操作JDBC数据库
- F#中的异步及并行模式(3 - 上):代理的基本使用
- 遇到当试图还原 master 数据库时,必须以单用户模式使用 RESTORE DATABASE.的解决办法
- 为什么要使用代理模式?
- tomcat 配置 quercus记录:php使用连接池访问数据库
- 使用动态代理实现自定义连接池
- 使用设计模式构建通用数据库访问类(转自天极)
- 七个结构型模式7:代理模式-Proxy Pattern【学习难度:★★★☆☆,使用频率:★★★★☆】
- 20180405-使用Java实现数据库编程-第八章DAO模式
- 代理模式使用场合与注意事项
- 线程池的封装和使用(代理模式工厂模式)