_connection.java

来自「一个Eclipse环境下的数据库连接池算法实现。」· Java 代码 · 共 139 行

JAVA
139
字号
package scut.ailab.connectionpool;

import java.lang.reflect.*;
import java.sql.*;

/*
 * @zhangmm
 * 定义数据库连接的代理类
 */

public class _Connection implements InvocationHandler {

	private Connection connection = null; //定义连接 

	private Statement statRef = null;//定义监控连接

	private PreparedStatement prestatRef = null;//定义监控连接

	private boolean supportTransaction = false; // 是否支持事务

	private boolean isFree = false; //数据库的闲忙状态

	long lastAccessTime = 0;//最后一次访问时间

	String CREATESTATE = "createStatement"; //定义要接管的函数的名字

	String CLOSE = "close";

	String PREPARESTATEMENT = "prepareStatement";

	String COMMIT = "commit";

	String ROLLBACK = "rollback";

	/**
	 * 构造函数,采用私有,防止被直接创建
	 * @param param 连接参数
	 */
	private _Connection(ConnectionParam param) {//记录日志
		try {
			Class.forName(param.getDriver()).newInstance();
			connection = DriverManager.getConnection(param.getUrl(), param
					.getUser(), param.getPassword());
			DatabaseMetaData dm = null;
			dm = connection.getMetaData();
			supportTransaction = dm.supportsTransactions();

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e);
		}
	}

	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		Object obj = null;

		if (CLOSE.equals(method.getName())) {//判断是否调用了close方法,如果是,则把连接设置为无用的状态
			setIsFree(false);//设置为不使用的状态
			if (statRef != null) { //检查是否有后续操作,清除该连接的无用的资源
				statRef.close();
			}
			if (prestatRef != null) {
				prestatRef.close();
			}
			return null;
		}

		if (CREATESTATE.equals(method.getName())) {//判断是否调用了createstatement语句
			obj = method.invoke(connection, args);
			statRef = (Statement) obj;//记录语句
			return obj;
		}
		if (PREPARESTATEMENT.equals(method.getDefaultValue())) {//判断是否调用了preparecreatestatement语句
			obj = method.invoke(connection, args);
			prestatRef = (PreparedStatement) obj;
			return obj;
		}
		if ((COMMIT.equals(method.getName()) || ROLLBACK.equals(method
				.getName()))
				&& (!isSupportTraction())) {//如果不支持事务就不执行事务代码
			return null;
		}
		obj = method.invoke(connection, args);
		lastAccessTime = System.currentTimeMillis();//设置最后一次的访问时间
		return obj;
	}

	/**
	 * 创建连接的工厂,只能被工厂调用
	 * @param factory 要调用工厂,并且一定要被初始化
	 * @param param   连接参数
	 * @return 连接
	 */
	static public _Connection getConnection(ConnectionFactory factory,
			ConnectionParam param) {
		if (factory.isCreate()) {
			_Connection _conn = new _Connection(param);
			return _conn;
		} else {
			return null;
		}
	}

	public Connection getFreeConnection() {
		//返回数据库连接conn的接管类,以便截住close方法
		Class[] cls = connection.getClass().getInterfaces();
		{
			cls = new Class[] { Connection.class };
		}
		Connection con2 = (Connection) Proxy.newProxyInstance(connection
				.getClass().getClassLoader(), cls, this);
		return con2;
		/*Connection con2 = (Connection) Proxy.newProxyInstance(connection
		 .getClass().getClassLoader(), connection.getClass()
		 .getInterfaces(), this);
		 return con2;
		 */

	}

	void close() throws SQLException {
		connection.close();
	}

	public void setIsFree(boolean isFree) {
		this.isFree = isFree;
	}

	public boolean isFree() {
		return isFree;
	}

	public boolean isSupportTraction() {
		return supportTransaction;
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?