01: /* JFox, the OpenSource J2EE Application Server
02: *
03: * Copyright (C) 2002 huihoo.com
04: * Distributable under GNU LGPL license
05: * See the GNU Lesser General Public License for more details.
06: */
07:
08: package org.huihoo.jfox.pool.connection;
09:
10: import java.lang.reflect.Method;
11: import java.sql.SQLException;
12:
13: import org.huihoo.jfox.pool.PoolableObjectInvocationHandler;
14: import org.huihoo.jfox.pool.PoolableObject;
15:
16: /**
17: *
18: * @author <a href="mailto:kelvin_wym@hotmail.com">Kelvin Wu</a>
19: */
20:
21: public class PoolableConnectionInvocationHandler extends
22: PoolableObjectInvocationHandler {
23:
24: public PoolableConnectionInvocationHandler(
25: PoolableObject poolableObject) {
26: super (poolableObject);
27: if (!(poolableObject instanceof PoolableConnection)) {
28: throw new RuntimeException(poolableObject
29: + " is not a PoolableConnection");
30: }
31: }
32:
33: public Object invoke(Object proxy, Method method, Object[] args)
34: throws Throwable {
35:
36: PoolableConnection pconn = (PoolableConnection) this
37: .getPoolableObject();
38:
39: // 拦截 close 方法,并没有关闭连接,而是把 PoolableConnect 设为 false use, 并且返回连接池
40: if (method.getName().equals("close")
41: && method.getParameterTypes().length == 0) {
42: // pconn.setUsing(false);
43: // ObjectPool pool = pconn.getPool();
44: // pool.restoreObject(pconn);
45: pconn.close();
46: return null;
47: } else {
48: Object obj = null;
49: // System.out.println("Handle: " + method.getName());
50: try {
51: // 如果调用的是 PoolableConnetion 本身的方法,比如:activate 并不转交给 java.sql.Connection 执行
52: pconn.getClass().getMethod(method.getName(),
53: method.getParameterTypes());
54: obj = method.invoke(pconn, args);
55: // System.out.println("Handle Method: " + method.getName());
56: } catch (Exception e) { // PoolableConnection 没有该方法,交给 Connection 执行
57: if (!pconn.isUsing()) {
58: throw new SQLException(
59: "The connection has been closed");
60: }
61: obj = method.invoke(pconn.getConnection(), args);
62: }
63: return obj;
64: }
65: }
66: }
|