jdbc 连接池动态代理模式

动态代理的用法:关键是理解动态代理

package com.dsp.pool;

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.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;


import javax.sql.DataSource;


import com.dsp.Util.jdbcUtil;


public class MyDataSource implements DataSource {
private static List<Connection> pool = Collections
.synchronizedList(new ArrayList<Connection>());
static {
for (int i = 0; i < 10; i++) {
try {
Connection conn = jdbcUtil.getConnection();
pool.add(conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


public Connection getConnection() throws SQLException {
if (pool.size() > 0) {
final Connection conn = pool.remove(0);
//弄一个动态代理模式 调用任何方法之前都会经过 invoke 去调用你想用的方法
Connection proxyConn = (Connection) Proxy.newProxyInstance(conn
.getClass().getClassLoader(), conn.getClass()
.getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if ("close".equals(method.getName())) {
System.out.println("把链接返回连接池");
return pool.add(conn);
}
return method.invoke(conn, args);
}
});
return proxyConn;
}else{
throw new RuntimeException("服务器繁忙");
}

}


public PrintWriter getLogWriter() throws SQLException {
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;
}


}


用法例子:



public class datasourceTest {
public static void main(String[] args) {
MyDataSource myDataSource = new MyDataSource();
try {
Connection conn = myDataSource.getConnection();
System.out.println(conn.getClass().getName());
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


java创建代理类:



public static void main(String[] args) {
final SpringBrother bitch = new SpringBrother();//不能是final的
//用他的子类作为代理类
/*
Class superclass:父类型
Callback callback:如何代理
*/
SpringBrother middleman = (SpringBrother) Enhancer.create(SpringBrother.class, new MethodInterceptor(){
//匿名内部类:具体策略
//只要执行任何方法,都会经过该方法
/*
返回值:当前调用的方法的返回值
Object proxy:当前代理对象的引用。
Method method:当前执行的方法。
Object[] args:当前指定的方法用到的参数
*/
public Object intercept(Object proxy, Method method, Object[] args,
MethodProxy arg3) throws Throwable {
if("sing".equals(method.getName())){
float money = (Float)args[0];
if(money>10000){
method.invoke(bitch, money/2);
}
}
if("dance".equals(method.getName())){
float money = (Float)args[0];
if(money>20000){
method.invoke(bitch, money/2);
}
}
return null;
}

});
middleman.sing(100000);
middleman.dance(200000);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值