KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > LogicalConnection


1 /*
2
3    Derby - Class org.apache.derby.client.am.LogicalConnection
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21
22 package org.apache.derby.client.am;
23 import org.apache.derby.shared.common.reference.SQLState;
24
25 import java.sql.SQLException JavaDoc;
26
27
28 // A simple delegation wrapper handle for a physical connection.
29
// All methods are forwarded to the underlying physical connection except for close() and isClosed().
30
// When a physical connection is wrapped, it is non-null, when the logical connection
31
// is closed, the wrapped physical connection is always set to null.
32
// Both the finalizer and close() methods will always set the physical connection to null.
33
// After the physical conneciton is set to null,
34
// only the Pooled Connection instance will maintain a handle to the physical connection.
35

36 public class LogicalConnection implements java.sql.Connection JavaDoc {
37     protected Connection physicalConnection_ = null; // reset to null when the logical connection is closed.
38
private org.apache.derby.client.ClientPooledConnection pooledConnection_ = null;
39
40     public LogicalConnection(Connection physicalConnection,
41                              org.apache.derby.client.ClientPooledConnection pooledConnection) throws SqlException {
42         physicalConnection_ = physicalConnection;
43         pooledConnection_ = pooledConnection;
44         try {
45             checkForNullPhysicalConnection();
46         } catch ( SQLException JavaDoc se ) {
47             throw new SqlException(se);
48         }
49     }
50
51     protected void finalize() throws java.lang.Throwable JavaDoc {
52         close();
53     }
54
55     // Used by ClientPooledConnection close when it disassociates itself from the LogicalConnection
56
synchronized public void nullPhysicalConnection() {
57         physicalConnection_ = null;
58     }
59
60     // ------------------------ logical connection close -------------------------
61
// All methods are simply forwarded to the physical connection, except for close() and isClosed().
62

63     synchronized public void close() throws SQLException JavaDoc {
64         try
65         {
66             // we also need to loop thru all the logicalStatements and close them
67
if (physicalConnection_ == null) {
68                 return;
69             }
70             if (physicalConnection_.agent_.loggingEnabled()) {
71                 physicalConnection_.agent_.logWriter_.traceEntry(this, "close");
72             }
73
74             if (physicalConnection_.isClosed()) // connection is closed or has become stale
75
{
76                 pooledConnection_.trashConnection(new SqlException(null,
77                     new ClientMessageId(
78                         SQLState.PHYSICAL_CONNECTION_ALREADY_CLOSED)));
79             } else {
80                 physicalConnection_.closeForReuse();
81                 if (!physicalConnection_.isGlobalPending_()) {
82                     pooledConnection_.recycleConnection();
83                 }
84             }
85             physicalConnection_ = null;
86             pooledConnection_.nullLogicalConnection();
87         }
88         catch ( SqlException se )
89         {
90             throw se.getSQLException();
91         }
92     }
93
94     synchronized public void closeWithoutRecyclingToPool() throws SqlException {
95         if (physicalConnection_ == null) {
96             return;
97         }
98         physicalConnection_.checkForTransactionInProgress();
99         try {
100             if (physicalConnection_.isClosed()) // connection is closed or has become stale
101
{
102                 throw new SqlException(null,
103                     new ClientMessageId(SQLState.NO_CURRENT_CONNECTION)); // no call to trashConnection()
104
} else {
105                 ; // no call to recycleConnection()
106
}
107         } finally {
108             physicalConnection_.closeForReuse(); //poolfix
109
physicalConnection_ = null;
110         }
111     }
112
113     public boolean isClosed() throws SQLException JavaDoc {
114         if (physicalConnection_ == null) {
115             return true;
116         }
117         return physicalConnection_.isClosed();
118     }
119
120     // --------------------------- helper methods --------------------------------
121

122     // this method doesn't wrap in the standard way, because it went out without a throws clause.
123
// Unlike all other LogicalConnection methods, if the physical connection is null, it won't throw an exception, but will return false.
124

125     protected void checkForNullPhysicalConnection() throws SQLException JavaDoc {
126         if (physicalConnection_ == null) {
127             SqlException se = new SqlException(null,
128                 new ClientMessageId(SQLState.NO_CURRENT_CONNECTION));
129             throw se.getSQLException();
130         }
131     }
132
133     // ---------------------- wrapped public entry points ------------------------
134
// All methods are forwarded to the physical connection in a standard way
135

136     synchronized public java.sql.Statement JavaDoc createStatement() throws SQLException JavaDoc {
137         checkForNullPhysicalConnection();
138         return physicalConnection_.createStatement();
139     }
140
141     synchronized public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc {
142         checkForNullPhysicalConnection();
143         return physicalConnection_.prepareStatement(sql);
144     }
145
146     synchronized public PreparedStatement preparePositionedUpdateStatement(String JavaDoc sql, Section querySection) throws SqlException {
147         try {
148             checkForNullPhysicalConnection();
149         } catch ( SQLException JavaDoc se ) {
150             throw new SqlException(se);
151         }
152         return physicalConnection_.preparePositionedUpdateStatement(sql, querySection);
153     }
154
155     synchronized public java.sql.CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc {
156         checkForNullPhysicalConnection();
157         return physicalConnection_.prepareCall(sql);
158     }
159
160     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc {
161         checkForNullPhysicalConnection();
162         return physicalConnection_.nativeSQL(sql);
163     }
164
165     synchronized public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc {
166         checkForNullPhysicalConnection();
167         physicalConnection_.setAutoCommit(autoCommit);
168     }
169
170     public boolean getAutoCommit() throws SQLException JavaDoc {
171         checkForNullPhysicalConnection();
172         return physicalConnection_.getAutoCommit();
173     }
174
175     synchronized public void commit() throws SQLException JavaDoc {
176         checkForNullPhysicalConnection();
177         physicalConnection_.commit();
178     }
179
180     synchronized public void rollback() throws SQLException JavaDoc {
181         checkForNullPhysicalConnection();
182         physicalConnection_.rollback();
183     }
184
185     synchronized public void setTransactionIsolation(int level) throws SQLException JavaDoc {
186         checkForNullPhysicalConnection();
187         physicalConnection_.setTransactionIsolation(level);
188     }
189
190     public int getTransactionIsolation() throws SQLException JavaDoc {
191         checkForNullPhysicalConnection();
192         return physicalConnection_.getTransactionIsolation();
193     }
194
195     public java.sql.SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
196         checkForNullPhysicalConnection();
197         return physicalConnection_.getWarnings();
198     }
199
200     synchronized public void clearWarnings() throws SQLException JavaDoc {
201         checkForNullPhysicalConnection();
202         physicalConnection_.clearWarnings();
203     }
204
205     public java.sql.DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
206         checkForNullPhysicalConnection();
207         return physicalConnection_.getMetaData();
208     }
209
210     synchronized public void setReadOnly(boolean readOnly) throws SQLException JavaDoc {
211         checkForNullPhysicalConnection();
212         physicalConnection_.setReadOnly(readOnly);
213     }
214
215     public boolean isReadOnly() throws SQLException JavaDoc {
216         checkForNullPhysicalConnection();
217         return physicalConnection_.isReadOnly();
218     }
219
220     synchronized public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc {
221         checkForNullPhysicalConnection();
222         physicalConnection_.setCatalog(catalog);
223     }
224
225     public String JavaDoc getCatalog() throws SQLException JavaDoc {
226         checkForNullPhysicalConnection();
227         return physicalConnection_.getCatalog();
228     }
229
230     synchronized public java.sql.Statement JavaDoc createStatement(int resultSetType,
231                                                            int resultSetConcurrency) throws SQLException JavaDoc {
232         checkForNullPhysicalConnection();
233         return physicalConnection_.createStatement(resultSetType, resultSetConcurrency);
234     }
235
236     synchronized public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql,
237                                                                     int resultSetType,
238                                                                     int resultSetConcurrency) throws SQLException JavaDoc {
239         checkForNullPhysicalConnection();
240         return physicalConnection_.prepareStatement(sql, resultSetType, resultSetConcurrency);
241     }
242
243     synchronized public java.sql.CallableStatement JavaDoc prepareCall(String JavaDoc sql,
244                                                                int resultSetType,
245                                                                int resultSetConcurrency) throws SQLException JavaDoc {
246         checkForNullPhysicalConnection();
247         return physicalConnection_.prepareCall(sql, resultSetType, resultSetConcurrency);
248     }
249
250     public java.util.Map JavaDoc getTypeMap() throws SQLException JavaDoc {
251         checkForNullPhysicalConnection();
252         return physicalConnection_.getTypeMap();
253     }
254
255     synchronized public void setTypeMap(java.util.Map JavaDoc map) throws SQLException JavaDoc {
256         checkForNullPhysicalConnection();
257         physicalConnection_.setTypeMap(map);
258     }
259
260     public java.sql.Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency,
261                                               int resultSetHoldability) throws SQLException JavaDoc {
262         checkForNullPhysicalConnection();
263         return physicalConnection_.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
264     }
265
266     public java.sql.CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType,
267                                                   int resultSetConcurrency,
268                                                   int resultSetHoldability) throws SQLException JavaDoc {
269         checkForNullPhysicalConnection();
270         return physicalConnection_.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
271     }
272
273     public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType,
274                                                        int resultSetConcurrency, int resultSetHoldability)
275             throws SQLException JavaDoc {
276         checkForNullPhysicalConnection();
277         return physicalConnection_.prepareStatement(sql, resultSetType, resultSetConcurrency,
278                 resultSetHoldability);
279     }
280
281     public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int autoGeneratedKeys)
282             throws SQLException JavaDoc {
283         checkForNullPhysicalConnection();
284         return physicalConnection_.prepareStatement(sql, autoGeneratedKeys);
285     }
286
287     public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int columnIndexes[])
288             throws SQLException JavaDoc {
289         checkForNullPhysicalConnection();
290         return physicalConnection_.prepareStatement(sql, columnIndexes);
291     }
292
293     public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc columnNames[])
294             throws SQLException JavaDoc {
295         checkForNullPhysicalConnection();
296         return physicalConnection_.prepareStatement(sql, columnNames);
297     }
298
299     public void setHoldability(int holdability) throws SQLException JavaDoc {
300         checkForNullPhysicalConnection();
301         physicalConnection_.setHoldability(holdability);
302     }
303
304     public int getHoldability() throws SQLException JavaDoc {
305         checkForNullPhysicalConnection();
306         return physicalConnection_.getHoldability();
307     }
308
309     public java.sql.Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc {
310         checkForNullPhysicalConnection();
311         return physicalConnection_.setSavepoint();
312     }
313
314     public java.sql.Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc {
315         checkForNullPhysicalConnection();
316         return physicalConnection_.setSavepoint(name);
317     }
318
319     public void rollback(java.sql.Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
320         checkForNullPhysicalConnection();
321         physicalConnection_.rollback(savepoint);
322     }
323
324     public void releaseSavepoint(java.sql.Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
325         checkForNullPhysicalConnection();
326         physicalConnection_.releaseSavepoint(savepoint);
327     }
328
329     //----------------------------------------------------------------------------
330

331     public int getServerVersion() {
332         if (physicalConnection_ == null) {
333             return -1;
334         } else {
335             return physicalConnection_.getServerVersion();
336         }
337     }
338
339 }
340
Popular Tags