`

利用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;
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics