KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > datasource > JdbcTransactionObjectSupport


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.datasource;
18
19 import java.sql.Savepoint JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import org.springframework.transaction.CannotCreateTransactionException;
25 import org.springframework.transaction.NestedTransactionNotSupportedException;
26 import org.springframework.transaction.SavepointManager;
27 import org.springframework.transaction.TransactionException;
28 import org.springframework.transaction.TransactionSystemException;
29 import org.springframework.transaction.TransactionUsageException;
30 import org.springframework.transaction.support.SmartTransactionObject;
31 import org.springframework.util.ClassUtils;
32
33 /**
34  * Convenient base class for JDBC-aware transaction objects.
35  * Can contain a ConnectionHolder, and implements the SavepointManager
36  * interface based on that ConnectionHolder.
37  *
38  * <p>Implements the SavepointManager interface to allow for programmatic
39  * management of JDBC 3.0 Savepoints. DefaultTransactionStatus will
40  * automatically delegate to this, as it auto-detects transaction objects
41  * that implement the SavepointManager interface.
42  *
43  * <p>Note that savepoints are only supported for JDBC 3.0.
44  *
45  * @author Juergen Hoeller
46  * @since 1.1
47  * @see java.sql.Savepoint
48  */

49 public abstract class JdbcTransactionObjectSupport implements SavepointManager, SmartTransactionObject {
50
51     private static final Log logger = LogFactory.getLog(JdbcTransactionObjectSupport.class);
52
53     // Determine whether JDK 1.4's Savepoint interface is available,
54
// indicating that Spring's transaction savepoints are supported.
55
private static boolean savepointClassAvailable =
56             ClassUtils.isPresent("java.sql.Savepoint", JdbcTransactionObjectSupport.class.getClassLoader());
57
58
59     private ConnectionHolder connectionHolder;
60
61     private Integer JavaDoc previousIsolationLevel;
62
63     private boolean savepointAllowed;
64
65
66     public void setConnectionHolder(ConnectionHolder connectionHolder) {
67         this.connectionHolder = connectionHolder;
68     }
69
70     public ConnectionHolder getConnectionHolder() {
71         return connectionHolder;
72     }
73
74     public boolean hasConnectionHolder() {
75         return (this.connectionHolder != null);
76     }
77
78     public void setPreviousIsolationLevel(Integer JavaDoc previousIsolationLevel) {
79         this.previousIsolationLevel = previousIsolationLevel;
80     }
81
82     public Integer JavaDoc getPreviousIsolationLevel() {
83         return previousIsolationLevel;
84     }
85
86     public void setSavepointAllowed(boolean savepointAllowed) {
87         this.savepointAllowed = savepointAllowed;
88     }
89
90     public boolean isSavepointAllowed() {
91         return savepointAllowed;
92     }
93
94
95     //---------------------------------------------------------------------
96
// Implementation of SavepointManager
97
//---------------------------------------------------------------------
98

99     /**
100      * This implementation creates a JDBC 3.0 Savepoint and returns it.
101      * @see java.sql.Connection#setSavepoint
102      */

103     public Object JavaDoc createSavepoint() throws TransactionException {
104         if (!savepointClassAvailable) {
105             throw new NestedTransactionNotSupportedException(
106                     "Cannot create a nested JDBC transaction because you are not running on JDK 1.4 or higher");
107         }
108         ConnectionHolder conHolder = getConnectionHolderForSavepoint();
109         try {
110             if (!conHolder.supportsSavepoints()) {
111                 throw new NestedTransactionNotSupportedException(
112                         "Cannot create a nested transaction because savepoints are not supported by your JDBC driver");
113             }
114         }
115         catch (Throwable JavaDoc ex) {
116             throw new NestedTransactionNotSupportedException(
117                     "Cannot create a nested transaction because your JDBC driver is not a JDBC 3.0 driver", ex);
118         }
119         try {
120             return conHolder.createSavepoint();
121         }
122         catch (Throwable JavaDoc ex) {
123             throw new CannotCreateTransactionException("Could not create JDBC savepoint", ex);
124         }
125     }
126
127     /**
128      * This implementation rolls back to the given JDBC 3.0 Savepoint.
129      * @see java.sql.Connection#rollback(java.sql.Savepoint)
130      */

131     public void rollbackToSavepoint(Object JavaDoc savepoint) throws TransactionException {
132         try {
133             getConnectionHolderForSavepoint().getConnection().rollback((Savepoint JavaDoc) savepoint);
134         }
135         catch (Throwable JavaDoc ex) {
136             throw new TransactionSystemException("Could not roll back to JDBC savepoint", ex);
137         }
138     }
139
140     /**
141      * This implementation releases the given JDBC 3.0 Savepoint.
142      * @see java.sql.Connection#releaseSavepoint
143      */

144     public void releaseSavepoint(Object JavaDoc savepoint) throws TransactionException {
145         try {
146             getConnectionHolderForSavepoint().getConnection().releaseSavepoint((Savepoint JavaDoc) savepoint);
147         }
148         catch (Throwable JavaDoc ex) {
149             logger.debug("Could not explicitly release JDBC savepoint", ex);
150         }
151     }
152
153     protected ConnectionHolder getConnectionHolderForSavepoint() throws TransactionException {
154         if (!isSavepointAllowed()) {
155             throw new NestedTransactionNotSupportedException(
156                     "Transaction manager does not allow nested transactions");
157         }
158         if (!hasConnectionHolder()) {
159             throw new TransactionUsageException(
160                     "Cannot create nested transaction if not exposing a JDBC transaction");
161         }
162         return getConnectionHolder();
163     }
164
165 }
166
Popular Tags