KickJava   Java API By Example, From Geeks To Geeks.

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


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.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21
22 import org.springframework.transaction.support.ResourceHolderSupport;
23 import org.springframework.util.Assert;
24
25 /**
26  * Connection holder, wrapping a JDBC Connection.
27  * {@link DataSourceTransactionManager} binds instances of this class
28  * to the thread, for a specific DataSource.
29  *
30  * <p>Inherits rollback-only support for nested JDBC transactions
31  * and reference count functionality from the base class.
32  *
33  * <p>Note: This is an SPI class, not intended to be used by applications.
34  *
35  * @author Juergen Hoeller
36  * @since 06.05.2003
37  * @see DataSourceTransactionManager
38  * @see DataSourceUtils
39  */

40 public class ConnectionHolder extends ResourceHolderSupport {
41
42     public static final String JavaDoc SAVEPOINT_NAME_PREFIX = "SAVEPOINT_";
43
44
45     private ConnectionHandle connectionHandle;
46
47     private Connection JavaDoc currentConnection;
48
49     private boolean transactionActive = false;
50
51     private Boolean JavaDoc savepointsSupported;
52
53     private int savepointCounter = 0;
54
55
56     /**
57      * Create a new ConnectionHolder for the given ConnectionHandle.
58      * @param connectionHandle the ConnectionHandle to hold
59      */

60     public ConnectionHolder(ConnectionHandle connectionHandle) {
61         Assert.notNull(connectionHandle, "ConnectionHandle must not be null");
62         this.connectionHandle = connectionHandle;
63     }
64
65     /**
66      * Create a new ConnectionHolder for the given JDBC Connection,
67      * wrapping it with a {@link SimpleConnectionHandle},
68      * assuming that there is no ongoing transaction.
69      * @param connection the JDBC Connection to hold
70      * @see SimpleConnectionHandle
71      * @see #ConnectionHolder(java.sql.Connection, boolean)
72      */

73     public ConnectionHolder(Connection JavaDoc connection) {
74         this.connectionHandle = new SimpleConnectionHandle(connection);
75     }
76
77     /**
78      * Create a new ConnectionHolder for the given JDBC Connection,
79      * wrapping it with a {@link SimpleConnectionHandle}.
80      * @param connection the JDBC Connection to hold
81      * @param transactionActive whether the given Connection is involved
82      * in an ongoing transaction
83      * @see SimpleConnectionHandle
84      */

85     public ConnectionHolder(Connection JavaDoc connection, boolean transactionActive) {
86         this(connection);
87         this.transactionActive = transactionActive;
88     }
89
90
91     /**
92      * Return the ConnectionHandle held by this ConnectionHolder.
93      */

94     public ConnectionHandle getConnectionHandle() {
95         return this.connectionHandle;
96     }
97
98     /**
99      * Return whether this holder currently has a Connection.
100      */

101     protected boolean hasConnection() {
102         return (this.connectionHandle != null);
103     }
104
105     /**
106      * Set whether this holder represents an active, JDBC-managed transaction.
107      * @see DataSourceTransactionManager
108      */

109     protected void setTransactionActive(boolean transactionActive) {
110         this.transactionActive = transactionActive;
111     }
112
113     /**
114      * Return whether this holder represents an active, JDBC-managed transaction.
115      */

116     protected boolean isTransactionActive() {
117         return this.transactionActive;
118     }
119
120
121     /**
122      * Override the existing Connection handle with the given Connection.
123      * Reset the handle if given <code>null</code>.
124      * <p>Used for releasing the Connection on suspend (with a <code>null</code>
125      * argument) and setting a fresh Connection on resume.
126      */

127     protected void setConnection(Connection JavaDoc connection) {
128         if (this.currentConnection != null) {
129             this.connectionHandle.releaseConnection(this.currentConnection);
130             this.currentConnection = null;
131         }
132         if (connection != null) {
133             this.connectionHandle = new SimpleConnectionHandle(connection);
134         }
135         else {
136             this.connectionHandle = null;
137         }
138     }
139
140     /**
141      * Return the current Connection held by this ConnectionHolder.
142      * <p>This will be the same Connection until <code>released</code>
143      * gets called on the ConnectionHolder, which will reset the
144      * held Connection, fetching a new Connection on demand.
145      * @see ConnectionHandle#getConnection()
146      * @see #released()
147      */

148     public Connection JavaDoc getConnection() {
149         Assert.notNull(this.connectionHandle, "Active Connection is required");
150         if (this.currentConnection == null) {
151             this.currentConnection = this.connectionHandle.getConnection();
152         }
153         return this.currentConnection;
154     }
155
156     /**
157      * Return whether JDBC 3.0 Savepoints are supported.
158      * Caches the flag for the lifetime of this ConnectionHolder.
159      * @throws SQLException if thrown by the JDBC driver
160      */

161     public boolean supportsSavepoints() throws SQLException JavaDoc {
162         if (this.savepointsSupported == null) {
163             this.savepointsSupported = new Boolean JavaDoc(getConnection().getMetaData().supportsSavepoints());
164         }
165         return this.savepointsSupported.booleanValue();
166     }
167
168     /**
169      * Create a new JDBC 3.0 Savepoint for the current Connection,
170      * using generated savepoint names that are unique for the Connection.
171      * @return the new Savepoint (typed as Object for JDK 1.3 compatibility)
172      * @throws SQLException if thrown by the JDBC driver
173      */

174     public Object JavaDoc createSavepoint() throws SQLException JavaDoc {
175         this.savepointCounter++;
176         return getConnection().setSavepoint(SAVEPOINT_NAME_PREFIX + this.savepointCounter);
177     }
178
179     /**
180      * Releases the current Connection held by this ConnectionHolder.
181      * <p>This is necessary for ConnectionHandles that expect "Connection borrowing",
182      * where each returned Connection is only temporarily leased and needs to be
183      * returned once the data operation is done, to make the Connection available
184      * for other operations within the same transaction. This is the case with
185      * JDO 2.0 DataStoreConnections, for example.
186      * @see org.springframework.orm.jdo.DefaultJdoDialect#getJdbcConnection
187      */

188     public void released() {
189         super.released();
190         if (this.currentConnection != null) {
191             this.connectionHandle.releaseConnection(this.currentConnection);
192             this.currentConnection = null;
193         }
194     }
195
196
197     public void clear() {
198         super.clear();
199         this.transactionActive = false;
200         this.savepointsSupported = null;
201         this.savepointCounter = 0;
202     }
203
204 }
205
Popular Tags