1 16 package web.struts; 17 18 import java.lang.reflect.*; 19 import java.sql.*; 20 21 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 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 invoke(Object proxy, Method m, Object args[]) throws Throwable 53 { 54 String 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 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 PREPAREDSTATEMENT = "prepareStatement"; 82 83 private static final String CREATESTATEMENT = "createStatement"; 84 85 private static final String M_SETAUTOCOMMIT = "setAutoCommit"; 86 87 private static final String M_COMMIT = "commit"; 88 89 private static final String M_ROLLBACK = "rollback"; 90 } | Popular Tags |