KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > impl > jdbcjobstore > AttributeRestoringConnectionInvocationHandler


1 /*
2  * Copyright 2004-2006 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16 package org.quartz.impl.jdbcjobstore;
17
18 import java.lang.reflect.InvocationHandler JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * <p>
28  * Protects a <code>{@link java.sql.Connection}</code>'s attributes from being permanently modfied.
29  * </p>
30  *
31  * <p>
32  * Wraps a provided <code>{@link java.sql.Connection}</code> such that its auto
33  * commit and transaction isolation attributes can be overwritten, but
34  * will automatically restored to their original values when the connection
35  * is actually closed (and potentially returned to a pool for reuse).
36  * </p>
37  *
38  * @see org.quartz.impl.jdbcjobstore.JobStoreSupport#getConnection()
39  * @see org.quartz.impl.jdbcjobstore.JobStoreCMT#getNonManagedTXConnection()
40  */

41 public class AttributeRestoringConnectionInvocationHandler implements InvocationHandler JavaDoc {
42     private Connection JavaDoc conn;
43     
44     private boolean overwroteOriginalAutoCommitValue;
45     private boolean overwroteOriginalTxIsolationValue;
46
47     // Set if overwroteOriginalAutoCommitValue is true
48
private boolean originalAutoCommitValue;
49
50     // Set if overwroteOriginalTxIsolationValue is true
51
private int originalTxIsolationValue;
52     
53     public AttributeRestoringConnectionInvocationHandler(
54         Connection JavaDoc conn) {
55         this.conn = conn;
56     }
57
58     protected Log getLog() {
59         return LogFactory.getLog(getClass());
60     }
61     
62     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
63         throws Throwable JavaDoc {
64         if (method.getName().equals("setAutoCommit")) {
65             setAutoCommit(((Boolean JavaDoc)args[0]).booleanValue());
66         } else if (method.getName().equals("setTransactionIsolation")) {
67             setTransactionIsolation(((Integer JavaDoc)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     /**
78      * Sets this connection's auto-commit mode to the given state, saving
79      * the original mode. The connection's original auto commit mode is restored
80      * when the connection is closed.
81      */

82     public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc {
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     /**
96      * Attempts to change the transaction isolation level to the given level, saving
97      * the original level. The connection's original transaction isolation level is
98      * restored when the connection is closed.
99      */

100     public void setTransactionIsolation(int level) throws SQLException JavaDoc {
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     /**
114      * Gets the underlying connection to which all operations ultimately
115      * defer. This is provided in case a user ever needs to punch through
116      * the wrapper to access vendor specific methods outside of the
117      * standard <code>java.sql.Connection</code> interface.
118      *
119      * @return The underlying connection to which all operations
120      * ultimately defer.
121      */

122     public Connection JavaDoc getWrappedConnection() {
123         return conn;
124     }
125
126     /**
127      * Attempts to restore the auto commit and transaction isolation connection
128      * attributes of the wrapped connection to their original values (if they
129      * were overwritten).
130      */

131     public void restoreOriginalAtributes() {
132         try {
133             if (overwroteOriginalAutoCommitValue) {
134                 conn.setAutoCommit(originalAutoCommitValue);
135             }
136         } catch (Throwable JavaDoc 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 JavaDoc t) {
145             getLog().warn("Failed restore connection's original transaction isolation setting.", t);
146         }
147     }
148     
149     /**
150      * Attempts to restore the auto commit and transaction isolation connection
151      * attributes of the wrapped connection to their original values (if they
152      * were overwritten), before finally actually closing the wrapped connection.
153      */

154     public void close() throws SQLException JavaDoc {
155         restoreOriginalAtributes();
156         
157         conn.close();
158     }
159 }
160
Popular Tags