利用proxy模式 来生成 DataSource

思想 就是 代理 DataSource对象 代理 getConnection方法
在代理connection对象 代理 close方法
/**
*
*/
package com.mjp.core.db.jdbc.ds;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;

import javax.sql.DataSource;

import com.mjp.core.db.tool.DbUtils;
import com.mjp.core.properties.PropertiesFacade;


/**
* @author Administrator
*
*/
public class DbcpDataSource implements DataSource {

private static Vector<Connection> connPool;
private static int poolMaxSize = 10;
private static int poolMinSize = 1;
private String userName;
private String password;
private String driverClass;
private String url;

public DbcpDataSource() {
init();
}

/**
*
*/
private void init() {
this.url = PropertiesFacade.getProperties("database.url");
this.driverClass = PropertiesFacade.getProperties("database.driver");
this.userName = PropertiesFacade.getProperties("database.user");
this.password = PropertiesFacade.getProperties("database.password");
try {
poolMaxSize = Integer.parseInt(PropertiesFacade
.getProperties("database.maxsize"));
} catch (Exception ex) {
poolMaxSize = 10;
}
try {
poolMinSize = Integer.parseInt(PropertiesFacade
.getProperties("database.minsize"));
} catch (Exception ex) {
poolMinSize = 1;
}
System.out.println("url:" + this.url);
System.out.println("driverClass:" + this.driverClass);
System.out.println("userName:" + this.userName);
System.out.println("password:" + this.password);
connPool = new Vector<Connection>();
for (int i = 0; i < poolMaxSize; i++) {
connPool.add(createConnection());
}
}

/**
* @return
*/
private Connection createConnection() {
Connection connection = null;
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

private Connection createProxyConnection(Connection connection){
if(connection == null){
connection = createConnection();
}
ProxyConnectionHandler proxyHandler = new ProxyConnectionHandler();
proxyHandler.setConnection(connection);
return proxyHandler.proxyBind();
}

/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#getConnection()
*/
public Connection getConnection() {

int size = connPool.size();
if (connPool.size() < poolMinSize) {
return createProxyConnection(null);
} else {
Connection connection = (Connection) connPool.get(size - 1);
connPool.remove(size - 1);
return createProxyConnection(connection);
}
}

public static synchronized void releaseConnection(Connection connection) {
if(connPool.size() > poolMaxSize){
DbUtils.closeQuietly(connection);
}else{
connPool.add(connection);
}
}

/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#getConnection(java.lang.String,
* java.lang.String)
*/
public Connection getConnection(String arg0, String arg1)
throws SQLException {
return null;
}

/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#getLogWriter()
*/
public PrintWriter getLogWriter() throws SQLException {
return null;
}

/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#getLoginTimeout()
*/
public int getLoginTimeout() throws SQLException {
return 0;
}

/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter)
*/
public void setLogWriter(PrintWriter arg0) throws SQLException {

}

/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#setLoginTimeout(int)
*/
public void setLoginTimeout(int arg0) throws SQLException {

}

}
=========================
package com.mjp.core.db.jdbc.ds;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;

import com.mjp.core.util.ProxyUtils;


public class ProxyConnectionHandler implements InvocationHandler {

private Connection connection;

public ProxyConnectionHandler(){}

public ProxyConnectionHandler(Connection connection) {
this.connection = connection;
}

/* (non-Javadoc)
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if (method.getName().equals("close")) {
if(connection.getAutoCommit() == false){
try{
connection.commit();
}catch(Exception ex){
connection.rollback();
ex.printStackTrace();
}finally{
try{
connection.setAutoCommit(true);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
DbcpDataSource.releaseConnection(connection);
return null;
} else {
return method.invoke(connection, args);
}
}

public Connection proxyBind() {
Connection proxyConnection = (Connection) Proxy.newProxyInstance(
connection.getClass().getClassLoader(), ProxyUtils.getAllIntefaces(connection.getClass())
, this);
return proxyConnection;
}

public Connection getConnection() {
return connection;
}


public void setConnection(Connection connection) {
this.connection = connection;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Proxy模式是一种常用的设计模式,也称为代理模式。它的作用是在某个对象外部提供一个代理类,用来控制对原始对象的访问。 Proxy模式通常用于以下场景: 1. 远程访问:当客户端需要访问远程对象时,我们可以通过代理类实现远程访问。代理类接收客户端的请求,并负责将请求传递给远程对象。远程对象将处理请求并将结果返回给代理类,代理类再将结果返回给客户端。 2. 安全控制:代理类可以用来限制对原始对象的访问。代理类可以检查客户端是否有足够的权限来访问原始对象,并在必要时拒绝访问。 3. 记录日志:代理类可以用来记录对原始对象的访问。代理类可以记录每个访问的时间、客户端的IP地址、请求的参数和结果等信息,从而方便后续的跟踪和分析。 4. 延迟加载:代理类可以用来实现延迟加载。当客户端请求访问原始对象时,代理类可以先返回一个占位符,并在必要时再加载原始对象。这样可以节省系统资源,提高系统的响应速度。 5. 缓存数据:代理类可以用来实现数据缓存。当客户端请求访问某个数据时,代理类先检查缓存中是否存在该数据,如果存在则直接返回缓存数据,否则才访问原始对象并将结果放入缓存中。 总之,Proxy模式是一种非常有用的设计模式,它可以在许多场合下起到很好的作用。如果您想要提高系统的性能、安全性或可维护性,那么Proxy模式是一种不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值