JDK API动态代理模拟数据库连接池

JDK API动态代理模拟数据库连接池

实现:在MyDataSource类中,编写静态方法getConnection,使该方法能够返回一个Connection的代理对象,该代理对象对Connection的close方法进行增强,使close方法不再直接关闭数据库连接而是将数据库连接返回到MyDataSource的连接池(链表或是其他的数据结构)中。

这样,外部通过MyDataSource获取Connection代理对象,外部调用Connection代理对象的close方法,即可将Connection对象归还到MyDataSource的连接池中。

PS:
MyDataSourceAdapter.java可有可无,不使用MyDataSourceAdapter.java的话,请在MyDataSource.java中实现DataSource接口

MyDataSourceAdapter.java

package DataSrouceTest;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import javax.sql.DataSource;

public abstract class MyDataSourceAdapter implements DataSource {

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}

MyDataSource.java

package DataSrouceTest;

import java.io.IOException;
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.sql.SQLException;
import java.util.LinkedList;
import java.util.Properties;

public class MyDataSource extends MyDataSourceAdapter {

	private static LinkedList<Connection> pool;
	private static final int DEFAULT_SIZE = 5;
	private static String username;
	private static String password;
	private static String url;

	static {
		pool = new LinkedList<Connection>();
		Properties prop = new Properties();
		try {
			prop.load(MyDataSource.class.getClassLoader().getResourceAsStream("db_server.properties"));
			username = prop.getProperty("username");
			password = prop.getProperty("password");
			url = prop.getProperty("url");
			try {
				Class.forName(prop.getProperty("driverClassName"));
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < DEFAULT_SIZE; i++) {
			try {
				pool.add(DriverManager.getConnection(url, username, password));
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 返回一个Connection的代理对象
	 */
	@Override
	public Connection getConnection() throws SQLException {
		Connection connection = pool.remove();
		System.out.println("数据库连接被借走,当前数据库连接池中连接数为:" + pool.size());
		Connection proxy = (Connection) Proxy.newProxyInstance(connection.getClass().getClassLoader(),
				new Class<?>[] { Connection.class }, new InvocationHandler() {
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						if ("close".equals(method.getName())) {
							pool.add(connection);
							System.out.println("数据库连接被归还,当前数据库连接池中连接数为:" + pool.size());
							return null;
						} else {
							return method.invoke(connection, args);
						}
					}
				});
		return proxy;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值