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

时间:2024-06-02 08:08:24

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

//注册数据库的驱动

Class.forName(driver);

//还有一种方式

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

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

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

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();
}
}


}