001: /*
002:
003: Derby - Class org.apache.derby.client.am.LogicalConnection
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.client.am;
023:
024: import org.apache.derby.shared.common.reference.SQLState;
025:
026: import java.sql.SQLException;
027:
028: // A simple delegation wrapper handle for a physical connection.
029: // All methods are forwarded to the underlying physical connection except for close() and isClosed().
030: // When a physical connection is wrapped, it is non-null, when the logical connection
031: // is closed, the wrapped physical connection is always set to null.
032: // Both the finalizer and close() methods will always set the physical connection to null.
033: // After the physical conneciton is set to null,
034: // only the Pooled Connection instance will maintain a handle to the physical connection.
035:
036: public class LogicalConnection implements java.sql.Connection {
037: protected Connection physicalConnection_ = null; // reset to null when the logical connection is closed.
038: private org.apache.derby.client.ClientPooledConnection pooledConnection_ = null;
039:
040: public LogicalConnection(
041: Connection physicalConnection,
042: org.apache.derby.client.ClientPooledConnection pooledConnection)
043: throws SqlException {
044: physicalConnection_ = physicalConnection;
045: pooledConnection_ = pooledConnection;
046: try {
047: checkForNullPhysicalConnection();
048: } catch (SQLException se) {
049: throw new SqlException(se);
050: }
051: }
052:
053: protected void finalize() throws java.lang.Throwable {
054: close();
055: }
056:
057: // Used by ClientPooledConnection close when it disassociates itself from the LogicalConnection
058: synchronized public void nullPhysicalConnection() {
059: physicalConnection_ = null;
060: }
061:
062: // ------------------------ logical connection close -------------------------
063: // All methods are simply forwarded to the physical connection, except for close() and isClosed().
064:
065: synchronized public void close() throws SQLException {
066: try {
067: // we also need to loop thru all the logicalStatements and close them
068: if (physicalConnection_ == null) {
069: return;
070: }
071: if (physicalConnection_.agent_.loggingEnabled()) {
072: physicalConnection_.agent_.logWriter_.traceEntry(this ,
073: "close");
074: }
075:
076: if (physicalConnection_.isClosed()) // connection is closed or has become stale
077: {
078: pooledConnection_
079: .trashConnection(new SqlException(
080: null,
081: new ClientMessageId(
082: SQLState.PHYSICAL_CONNECTION_ALREADY_CLOSED)));
083: } else {
084: physicalConnection_.closeForReuse();
085: if (!physicalConnection_.isGlobalPending_()) {
086: pooledConnection_.recycleConnection();
087: }
088: }
089: physicalConnection_ = null;
090: pooledConnection_.nullLogicalConnection();
091: } catch (SqlException se) {
092: throw se.getSQLException();
093: }
094: }
095:
096: synchronized public void closeWithoutRecyclingToPool()
097: throws SqlException {
098: if (physicalConnection_ == null) {
099: return;
100: }
101: physicalConnection_.checkForTransactionInProgress();
102: try {
103: if (physicalConnection_.isClosed()) // connection is closed or has become stale
104: {
105: throw new SqlException(null, new ClientMessageId(
106: SQLState.NO_CURRENT_CONNECTION)); // no call to trashConnection()
107: } else {
108: ; // no call to recycleConnection()
109: }
110: } finally {
111: physicalConnection_.closeForReuse(); //poolfix
112: physicalConnection_ = null;
113: }
114: }
115:
116: public boolean isClosed() throws SQLException {
117: if (physicalConnection_ == null) {
118: return true;
119: }
120: return physicalConnection_.isClosed();
121: }
122:
123: // --------------------------- helper methods --------------------------------
124:
125: // this method doesn't wrap in the standard way, because it went out without a throws clause.
126: // Unlike all other LogicalConnection methods, if the physical connection is null, it won't throw an exception, but will return false.
127:
128: protected void checkForNullPhysicalConnection() throws SQLException {
129: if (physicalConnection_ == null) {
130: SqlException se = new SqlException(null,
131: new ClientMessageId(SQLState.NO_CURRENT_CONNECTION));
132: throw se.getSQLException();
133: }
134: }
135:
136: // ---------------------- wrapped public entry points ------------------------
137: // All methods are forwarded to the physical connection in a standard way
138:
139: synchronized public java.sql.Statement createStatement()
140: throws SQLException {
141: checkForNullPhysicalConnection();
142: return physicalConnection_.createStatement();
143: }
144:
145: synchronized public java.sql.PreparedStatement prepareStatement(
146: String sql) throws SQLException {
147: checkForNullPhysicalConnection();
148: return physicalConnection_.prepareStatement(sql);
149: }
150:
151: synchronized public PreparedStatement preparePositionedUpdateStatement(
152: String sql, Section querySection) throws SqlException {
153: try {
154: checkForNullPhysicalConnection();
155: } catch (SQLException se) {
156: throw new SqlException(se);
157: }
158: return physicalConnection_.preparePositionedUpdateStatement(
159: sql, querySection);
160: }
161:
162: synchronized public java.sql.CallableStatement prepareCall(
163: String sql) throws SQLException {
164: checkForNullPhysicalConnection();
165: return physicalConnection_.prepareCall(sql);
166: }
167:
168: public String nativeSQL(String sql) throws SQLException {
169: checkForNullPhysicalConnection();
170: return physicalConnection_.nativeSQL(sql);
171: }
172:
173: synchronized public void setAutoCommit(boolean autoCommit)
174: throws SQLException {
175: checkForNullPhysicalConnection();
176: physicalConnection_.setAutoCommit(autoCommit);
177: }
178:
179: public boolean getAutoCommit() throws SQLException {
180: checkForNullPhysicalConnection();
181: return physicalConnection_.getAutoCommit();
182: }
183:
184: synchronized public void commit() throws SQLException {
185: checkForNullPhysicalConnection();
186: physicalConnection_.commit();
187: }
188:
189: synchronized public void rollback() throws SQLException {
190: checkForNullPhysicalConnection();
191: physicalConnection_.rollback();
192: }
193:
194: synchronized public void setTransactionIsolation(int level)
195: throws SQLException {
196: checkForNullPhysicalConnection();
197: physicalConnection_.setTransactionIsolation(level);
198: }
199:
200: public int getTransactionIsolation() throws SQLException {
201: checkForNullPhysicalConnection();
202: return physicalConnection_.getTransactionIsolation();
203: }
204:
205: public java.sql.SQLWarning getWarnings() throws SQLException {
206: checkForNullPhysicalConnection();
207: return physicalConnection_.getWarnings();
208: }
209:
210: synchronized public void clearWarnings() throws SQLException {
211: checkForNullPhysicalConnection();
212: physicalConnection_.clearWarnings();
213: }
214:
215: public java.sql.DatabaseMetaData getMetaData() throws SQLException {
216: checkForNullPhysicalConnection();
217: return physicalConnection_.getMetaData();
218: }
219:
220: synchronized public void setReadOnly(boolean readOnly)
221: throws SQLException {
222: checkForNullPhysicalConnection();
223: physicalConnection_.setReadOnly(readOnly);
224: }
225:
226: public boolean isReadOnly() throws SQLException {
227: checkForNullPhysicalConnection();
228: return physicalConnection_.isReadOnly();
229: }
230:
231: synchronized public void setCatalog(String catalog)
232: throws SQLException {
233: checkForNullPhysicalConnection();
234: physicalConnection_.setCatalog(catalog);
235: }
236:
237: public String getCatalog() throws SQLException {
238: checkForNullPhysicalConnection();
239: return physicalConnection_.getCatalog();
240: }
241:
242: synchronized public java.sql.Statement createStatement(
243: int resultSetType, int resultSetConcurrency)
244: throws SQLException {
245: checkForNullPhysicalConnection();
246: return physicalConnection_.createStatement(resultSetType,
247: resultSetConcurrency);
248: }
249:
250: synchronized public java.sql.PreparedStatement prepareStatement(
251: String sql, int resultSetType, int resultSetConcurrency)
252: throws SQLException {
253: checkForNullPhysicalConnection();
254: return physicalConnection_.prepareStatement(sql, resultSetType,
255: resultSetConcurrency);
256: }
257:
258: synchronized public java.sql.CallableStatement prepareCall(
259: String sql, int resultSetType, int resultSetConcurrency)
260: throws SQLException {
261: checkForNullPhysicalConnection();
262: return physicalConnection_.prepareCall(sql, resultSetType,
263: resultSetConcurrency);
264: }
265:
266: public java.util.Map getTypeMap() throws SQLException {
267: checkForNullPhysicalConnection();
268: return physicalConnection_.getTypeMap();
269: }
270:
271: synchronized public void setTypeMap(java.util.Map map)
272: throws SQLException {
273: checkForNullPhysicalConnection();
274: physicalConnection_.setTypeMap(map);
275: }
276:
277: public java.sql.Statement createStatement(int resultSetType,
278: int resultSetConcurrency, int resultSetHoldability)
279: throws SQLException {
280: checkForNullPhysicalConnection();
281: return physicalConnection_.createStatement(resultSetType,
282: resultSetConcurrency, resultSetHoldability);
283: }
284:
285: public java.sql.CallableStatement prepareCall(String sql,
286: int resultSetType, int resultSetConcurrency,
287: int resultSetHoldability) throws SQLException {
288: checkForNullPhysicalConnection();
289: return physicalConnection_.prepareCall(sql, resultSetType,
290: resultSetConcurrency, resultSetHoldability);
291: }
292:
293: public java.sql.PreparedStatement prepareStatement(String sql,
294: int resultSetType, int resultSetConcurrency,
295: int resultSetHoldability) throws SQLException {
296: checkForNullPhysicalConnection();
297: return physicalConnection_.prepareStatement(sql, resultSetType,
298: resultSetConcurrency, resultSetHoldability);
299: }
300:
301: public java.sql.PreparedStatement prepareStatement(String sql,
302: int autoGeneratedKeys) throws SQLException {
303: checkForNullPhysicalConnection();
304: return physicalConnection_.prepareStatement(sql,
305: autoGeneratedKeys);
306: }
307:
308: public java.sql.PreparedStatement prepareStatement(String sql,
309: int columnIndexes[]) throws SQLException {
310: checkForNullPhysicalConnection();
311: return physicalConnection_.prepareStatement(sql, columnIndexes);
312: }
313:
314: public java.sql.PreparedStatement prepareStatement(String sql,
315: String columnNames[]) throws SQLException {
316: checkForNullPhysicalConnection();
317: return physicalConnection_.prepareStatement(sql, columnNames);
318: }
319:
320: public void setHoldability(int holdability) throws SQLException {
321: checkForNullPhysicalConnection();
322: physicalConnection_.setHoldability(holdability);
323: }
324:
325: public int getHoldability() throws SQLException {
326: checkForNullPhysicalConnection();
327: return physicalConnection_.getHoldability();
328: }
329:
330: public java.sql.Savepoint setSavepoint() throws SQLException {
331: checkForNullPhysicalConnection();
332: return physicalConnection_.setSavepoint();
333: }
334:
335: public java.sql.Savepoint setSavepoint(String name)
336: throws SQLException {
337: checkForNullPhysicalConnection();
338: return physicalConnection_.setSavepoint(name);
339: }
340:
341: public void rollback(java.sql.Savepoint savepoint)
342: throws SQLException {
343: checkForNullPhysicalConnection();
344: physicalConnection_.rollback(savepoint);
345: }
346:
347: public void releaseSavepoint(java.sql.Savepoint savepoint)
348: throws SQLException {
349: checkForNullPhysicalConnection();
350: physicalConnection_.releaseSavepoint(savepoint);
351: }
352:
353: //----------------------------------------------------------------------------
354:
355: public int getServerVersion() {
356: if (physicalConnection_ == null) {
357: return -1;
358: } else {
359: return physicalConnection_.getServerVersion();
360: }
361: }
362:
363: }
|