1 16 package org.quartz.impl.jdbcjobstore; 17 18 import java.lang.reflect.InvocationHandler ; 19 import java.lang.reflect.Method ; 20 import java.sql.Connection ; 21 import java.sql.SQLException ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 41 public class AttributeRestoringConnectionInvocationHandler implements InvocationHandler { 42 private Connection conn; 43 44 private boolean overwroteOriginalAutoCommitValue; 45 private boolean overwroteOriginalTxIsolationValue; 46 47 private boolean originalAutoCommitValue; 49 50 private int originalTxIsolationValue; 52 53 public AttributeRestoringConnectionInvocationHandler( 54 Connection conn) { 55 this.conn = conn; 56 } 57 58 protected Log getLog() { 59 return LogFactory.getLog(getClass()); 60 } 61 62 public Object invoke(Object proxy, Method method, Object [] args) 63 throws Throwable { 64 if (method.getName().equals("setAutoCommit")) { 65 setAutoCommit(((Boolean )args[0]).booleanValue()); 66 } else if (method.getName().equals("setTransactionIsolation")) { 67 setTransactionIsolation(((Integer )args[0]).intValue()); 68 } else if (method.getName().equals("close")) { 69 close(); 70 } else { 71 return method.invoke(conn, args); 72 } 73 74 return null; 75 } 76 77 82 public void setAutoCommit(boolean autoCommit) throws SQLException { 83 boolean currentAutoCommitValue = conn.getAutoCommit(); 84 85 if (autoCommit != currentAutoCommitValue) { 86 if (overwroteOriginalAutoCommitValue == false) { 87 overwroteOriginalAutoCommitValue = true; 88 originalAutoCommitValue = currentAutoCommitValue; 89 } 90 91 conn.setAutoCommit(autoCommit); 92 } 93 } 94 95 100 public void setTransactionIsolation(int level) throws SQLException { 101 int currentLevel = conn.getTransactionIsolation(); 102 103 if (level != currentLevel) { 104 if (overwroteOriginalTxIsolationValue == false) { 105 overwroteOriginalTxIsolationValue = true; 106 originalTxIsolationValue = currentLevel; 107 } 108 109 conn.setTransactionIsolation(level); 110 } 111 } 112 113 122 public Connection getWrappedConnection() { 123 return conn; 124 } 125 126 131 public void restoreOriginalAtributes() { 132 try { 133 if (overwroteOriginalAutoCommitValue) { 134 conn.setAutoCommit(originalAutoCommitValue); 135 } 136 } catch (Throwable t) { 137 getLog().warn("Failed restore connection's original auto commit setting.", t); 138 } 139 140 try { 141 if (overwroteOriginalTxIsolationValue) { 142 conn.setTransactionIsolation(originalTxIsolationValue); 143 } 144 } catch (Throwable t) { 145 getLog().warn("Failed restore connection's original transaction isolation setting.", t); 146 } 147 } 148 149 154 public void close() throws SQLException { 155 restoreOriginalAtributes(); 156 157 conn.close(); 158 } 159 } 160 | Popular Tags |