Java 的动态代理实例(JDBC的数据库的连接池(DataSource))

问题:以下两种方式的区别是什么?

//注册数据库的驱动

Class.forName(driver);

//还有一种方式

//DriverManager.registerDriver(new com.mysql.jdbc.Driver());


==================================================================

package demo.proxy.datasource;


import java.io.PrintWriter;
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.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.logging.Logger;


import javax.sql.DataSource;


public class MyDataSourcePool implements DataSource {

//初始化该连接池,放入10个链接
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://192.168.157.111:3306/hive";
private static String user = "hiveowner";
private static String password ="Welcome_1";

//定义一个链表代表连接池
private static LinkedList<Connection> dataSource = new LinkedList<>();

static{
try{
//注册数据库的驱动
Class.forName(driver);

//还有一种方式
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());

//初始化10个链接
for(int i=0;i<10;i++){
dataSource.add(DriverManager.getConnection(url, user, password));
}
}catch(Exception ex){
ex.printStackTrace();
}
}



@Override
public Connection getConnection() throws SQLException {
// 返回一个连接给客户端
if(dataSource.size() > 0){
//存在链接
//真正的对象
final Connection conn = dataSource.removeFirst();

//生成真正对象的代理对象,重写close方法,还给连接池
Connection proxyConn = (Connection) Proxy.newProxyInstance(MyDataSourcePool.class.getClassLoader(), 
              conn.getClass().getInterfaces(), 
              new InvocationHandler() {

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 重写close方法
if(method.getName().equals("close")){
//调用了close方法 将该链接返回给连接池
dataSource.add(conn);
return null;
}else{
//调用了其他方法
return method.invoke(conn, args);
}
}
});

return proxyConn;
}else{
//没有链接
throw new SQLException("系统忙,请稍后.....");
}
}



@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;
}


}

==================================================================

package demo.proxy.datasource;


import java.sql.Connection;




public class TestDataSource {


public static void main(String[] args) throws Exception {
// 测试连接池
MyDataSourcePool ds = new MyDataSourcePool();


//取出链接使用
for(int i=0; i<11;i++){
Connection conn = ds.getConnection();

System.out.println("第"+i+"个链接:" + conn);

//关闭connection
conn.close();
}
}


}


















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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值