01: /*
02: * This program is free software; you can redistribute it and/or modify
03: * it under the terms of the GNU General Public License as published by
04: * the Free Software Foundation; either version 2 of the License, or
05: * (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10: * GNU Library General Public License for more details.
11: *
12: * You should have received a copy of the GNU General Public License
13: * along with this program; if not, write to the Free Software
14: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15: */
16: package web.struts;
17:
18: import java.lang.reflect.*;
19: import java.sql.*;
20:
21: /**
22: * 接管数据库连接,防止由于某些数据库不支持事务处理而抛出的异常
23: * @author Winter Lau
24: */
25: class _Connection implements InvocationHandler {
26:
27: private final static Class[] infs = new Class[] { Connection.class };
28:
29: _Connection(Connection conn, boolean coding) {
30: this .conn = conn;
31: this .coding = coding;
32: DatabaseMetaData dm = null;
33: try {
34: dm = conn.getMetaData();
35: supportTransaction = dm.supportsTransactions();
36: } catch (Exception e) {
37: }
38: }
39:
40: /**
41: * 获取对象的代理
42: * @return
43: */
44: public Connection getConnection() {
45: return (Connection) Proxy.newProxyInstance(conn.getClass()
46: .getClassLoader(), infs, this );
47: }
48:
49: void close() throws SQLException {
50: conn.close();
51: }
52:
53: public Object invoke(Object proxy, Method m, Object args[])
54: throws Throwable {
55: String method = m.getName();
56: if ((M_SETAUTOCOMMIT.equals(method) || M_COMMIT.equals(method) || M_ROLLBACK
57: .equals(method))
58: && !isSupportTransaction())
59: return null;
60: Object obj = null;
61: try {
62: obj = m.invoke(conn, args);
63: if (CREATESTATEMENT.equals(method)
64: || PREPAREDSTATEMENT.equals(method))
65: return (new _Statement((Statement) obj, coding))
66: .getStatement();
67: } catch (InvocationTargetException e) {
68: throw e.getTargetException();
69: }
70: return obj;
71: }
72:
73: public boolean isSupportTransaction() {
74: return supportTransaction;
75: }
76:
77: private Connection conn;
78:
79: private boolean supportTransaction;
80:
81: private boolean coding;
82:
83: private static final String PREPAREDSTATEMENT = "prepareStatement";
84:
85: private static final String CREATESTATEMENT = "createStatement";
86:
87: private static final String M_SETAUTOCOMMIT = "setAutoCommit";
88:
89: private static final String M_COMMIT = "commit";
90:
91: private static final String M_ROLLBACK = "rollback";
92: }
|