`
royzhou1985
  • 浏览: 249669 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

jdbc连接池简单封装

    博客分类:
  • Java
阅读更多
主要实现方法是:
使用一个容器(LinkedList),初始化的时候设定好连接数,生成Connection对象放在容器中
以后每次获取连接的时候都从容器中获取,但是这样有一个问题,当我们关闭连接的时候调用
Connection的close()方法的时候会直接将Connection关闭而不是重新放到容器中……
在这里使用的是代理,真正使用的是通过代理生成的一个存放在内存中的类,在这个类拦截了close()方法,在close()方法中将连接重新放回容器中......


数据库连接池类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;

public class MyDataSource{
	
	private static String url = "jdbc:mysql://localhost:3306/jdbc";
	private static String user = "root";
	private static String password = "";
	
	//初始化连接个数
	private static int initCount = 1;
	
	//最大连接个数
	private static int maxCount = 1;
	
	//当前连接个数
	int currentCount = 0;

	/**
	 * 使用linkedList是由于频繁的对容器进行增加和删除操作,相比ArrayList快
	 */
	LinkedList<Connection> connectionsPool = new LinkedList<Connection>();
	
	/**
	 * 初始化数据连接池中的连接个数
	 * @throws ClassNotFoundException
	 */
	public MyDataSource() throws ClassNotFoundException {
		Class.forName("com.mysql.jdbc.Driver");
		try {
			for (int i = 0; i < initCount; i++) {
				this.connectionsPool.addLast(this.createConnection());
				this.currentCount++;
			}
		} catch (SQLException e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	
	/**
	 * 从数据连接池中获取连接
	 * @return
	 * @throws SQLException
	 */
	public Connection getConnection() throws SQLException {
		synchronized (connectionsPool) {
			if (this.connectionsPool.size() > 0)
				return this.connectionsPool.removeFirst();

			if (this.currentCount < maxCount) {
				this.currentCount++;
				return this.createConnection();
			}

			throw new SQLException("��û��t��");
		}
	}
	
	/**
	 * 连接关闭后返回连接池中
	 * @param conn
	 */
	public void free(Connection conn) {
		this.connectionsPool.addLast(conn);
	}
	
	/**
	 * 创建连接,使用代理模式
	 * 实际上生成的不是java.sql.Connection
	 * proxy.bind(realConn);返回的是经过封装后的Connection
	 * 在其调用close()方法的时候拦截然后在close()方法中将连接放回连接池而不是直接关闭
	 * @return
	 * @throws SQLException
	 */
	private Connection createConnection() throws SQLException {
		Connection realConn = DriverManager.getConnection(url, user, password);
		MyConnectionHandler proxy = new MyConnectionHandler(this);
		return proxy.bind(realConn);
	}
	
	public static void main(String[] args) throws Exception {
		MyDataSource mds = new MyDataSource();
		for(int i=0; i<10; i++) {
			Connection conn = mds.getConnection();
			System.out.println(conn);
			conn.close();
		}
	}
}




代理类
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;

class MyConnectionHandler implements InvocationHandler {
	
	//普通的jdbc Connection
	private Connection realConnection;
	
	//通过代理在内存中生成的类
	private Connection warpedConnection;
	
	private MyDataSource dataSource;
	
	//每个连接最多使用次数
	private int maxUseCount = 5;
	
	//连接当前使用次数
	private int currentUserCount = 0;

	MyConnectionHandler(MyDataSource dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * 使用Proxy代理生成一个类
	 * 注意,生成的类不是我们以文本格式存在
	 * 而是存在内存中的
	 * @param realConn
	 * @return
	 */
	Connection bind(Connection realConn) {
		this.realConnection = realConn;
		/**
		 * Proxy.newProxyInstance(loader, interfaces, h)
		 * 其中loader是类加载器
		 * interfaces是要实现的接口,这里使用的是java.sql.Connection
		 * h是InvocationHandler,调用方法后交给哪个对象处理
		 */
		this.warpedConnection = (Connection) Proxy.newProxyInstance(this
				.getClass().getClassLoader(), new Class[] { Connection.class },
				this);
		return warpedConnection;
	}

	/**
	 * 每个代码实例都具有一个关联的调用处理程序。
	 * 对代理实例调用方法时,
	 * 将对方法调用进行编码并将其指派到它的调用处理程序的 invoke 方法。
	 */
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		//拦截close()方法,重写里面的方法
		if ("close".equals(method.getName())) {
			this.currentUserCount++;
			if (this.currentUserCount < this.maxUseCount)
				this.dataSource.connectionsPool.addLast(this.warpedConnection);
			else {
				this.realConnection.close();
				this.dataSource.currentCount--;
			}
		}
		//其他方法调用普通的jdbc Connection处理
		return method.invoke(this.realConnection, args);
	}

}



上述代码只是简单进行封装,主要是让大家了解一下数据库连接池的实现方式以及了解一下代理的实现,对以后学习Hibernate以及Spring的AOP有较大帮助
分享到:
评论
3 楼 pshaoyi 2015-03-18  
连接创建没问题,在执行的时候就报错了。

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1115)
at com.mysql.jdbc.ConnectionImpl.getMetaData(ConnectionImpl.java:3001)
at com.mysql.jdbc.ConnectionImpl.getMetaData(ConnectionImpl.java:2996)
... 58 more
2 楼 alask2011 2012-02-24  
而且proxy.bind(realConn);好像返回的也不是代理对象,因为代理对象打印出来的时候都是 以 $ 开头的,这个还是普通的对象
1 楼 alask2011 2012-02-24  
封装的还是有问题,真正的连接超出使用次数的时候根本关不掉啊,
就是那个普通的jdbc连接关不掉,我close()之后还是可以打印的
this.realConnection.close();这一句

相关推荐

Global site tag (gtag.js) - Google Analytics