KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > web > struts > _Connection


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * 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 JavaDoc[] infs = new Class JavaDoc[]{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 JavaDoc e){}
37     }
38     
39     /**
40      * 获取对象的代理
41      * @return
42      */

43     public Connection getConnection() {
44         return (Connection)Proxy.newProxyInstance(
45             conn.getClass().getClassLoader(),infs, this);
46     }
47
48     void close() throws SQLException {
49         conn.close();
50     }
51
52     public Object JavaDoc invoke(Object JavaDoc proxy, Method m, Object JavaDoc args[]) throws Throwable JavaDoc
53     {
54         String JavaDoc method = m.getName();
55         if ((M_SETAUTOCOMMIT.equals(method) ||
56              M_COMMIT.equals(method) ||
57              M_ROLLBACK.equals(method))
58              && !isSupportTransaction())
59             return null;
60         Object JavaDoc obj = null;
61         try {
62             obj = m.invoke(conn, args);
63             if (CREATESTATEMENT.equals(method)||PREPAREDSTATEMENT.equals(method))
64                 return (new _Statement((Statement) obj, coding)).getStatement();
65         } catch (InvocationTargetException e) {
66             throw e.getTargetException();
67         }
68         return obj;
69     }
70
71     public boolean isSupportTransaction() {
72         return supportTransaction;
73     }
74
75     private Connection conn;
76
77     private boolean supportTransaction;
78
79     private boolean coding;
80
81     private static final String JavaDoc PREPAREDSTATEMENT = "prepareStatement";
82
83     private static final String JavaDoc CREATESTATEMENT = "createStatement";
84
85     private static final String JavaDoc M_SETAUTOCOMMIT = "setAutoCommit";
86
87     private static final String JavaDoc M_COMMIT = "commit";
88
89     private static final String JavaDoc M_ROLLBACK = "rollback";
90 }
Popular Tags