001: /*
002: * PooledConnection.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.jdbc;
023:
024: import java.sql.CallableStatement;
025: import java.sql.Connection;
026: import java.sql.DatabaseMetaData;
027: import java.sql.PreparedStatement;
028: import java.sql.SQLException;
029: import java.sql.SQLWarning;
030: import java.sql.Statement;
031: import java.util.Map;
032:
033: /* ----------------------------------------------------------
034: * CVS NOTE: Changes to the CVS repository prior to the
035: * release of version 3.0.0beta1 has meant a
036: * resetting of CVS revision numbers.
037: * ----------------------------------------------------------
038: */
039:
040: /**
041: * Pooled connection wrapper.
042: *
043: * @author Takis Diakoumis
044: * @version $Revision: 1.6 $
045: * @date $Date: 2006/09/21 15:29:00 $
046: */
047: public class PooledConnection implements Connection {
048:
049: /** this connections use count */
050: private int useCount;
051:
052: /** indicates whether this connection is in use */
053: private boolean inUse;
054:
055: /** indicates whether to close this connection */
056: private boolean closeOnReturn;
057:
058: /** the original auto-commit mode from the real connection */
059: private boolean originalAutoCommit;
060:
061: /** the real JDBC connection that this object wraps */
062: private Connection realConnection;
063:
064: /**
065: * Creates a new PooledConnection object with the
066: * specified connection as the source.
067: *
068: * @param the real java.sql.Connection
069: */
070: public PooledConnection(Connection realConnection) {
071: this (realConnection, false);
072: }
073:
074: /**
075: * Creates a new PooledConnection object with the
076: * specified connection as the source.
077: *
078: * @param the real java.sql.Connection
079: */
080: public PooledConnection(Connection realConnection,
081: boolean closeOnReturn) {
082: useCount = 0;
083: this .realConnection = realConnection;
084: this .closeOnReturn = closeOnReturn;
085:
086: try {
087: originalAutoCommit = realConnection.getAutoCommit();
088: } catch (SQLException e) {
089: // default to true on dump
090: originalAutoCommit = true;
091: }
092: }
093:
094: public boolean equals(Object obj) {
095: if (realConnection == null) {
096: return false;
097: } else {
098: return realConnection.equals(obj);
099: }
100: }
101:
102: public int hashCode() {
103: if (realConnection == null) {
104: return 0;
105: }
106: return realConnection.hashCode();
107: }
108:
109: /**
110: * <p>Determine if the connection is available
111: *
112: * @return true if the connection can be used
113: */
114: public boolean isAvailable() {
115: try {
116: if (realConnection != null) {
117: if (!inUse && !realConnection.isClosed()) {
118: return true;
119: } else {
120: return false;
121: }
122: }
123: return false;
124: } catch (SQLException ex) {
125: return false;
126: }
127: }
128:
129: public void setInUse(boolean inUse) {
130: if (inUse) {
131: useCount++;
132: }
133: this .inUse = inUse;
134: }
135:
136: /**
137: * Closes the underlying connection, and close
138: * any Statements that were not explicitly closed.
139: */
140: public void close() throws SQLException {
141: inUse = false;
142: if (realConnection != null) {
143: if (closeOnReturn) {
144: realConnection.close();
145: realConnection = null;
146: } else {
147: // reset the original auto-commit mode
148: try {
149: realConnection.setAutoCommit(originalAutoCommit);
150: }
151: // ignore errors and don't want to re-throw
152: catch (SQLException e) {
153: }
154: }
155: }
156: }
157:
158: protected void handleException(SQLException e) throws SQLException {
159: throw e;
160: }
161:
162: public Statement createStatement() throws SQLException {
163: checkOpen();
164: return realConnection.createStatement();
165: }
166:
167: public Statement createStatement(int resultSetType,
168: int resultSetConcurrency) throws SQLException {
169: checkOpen();
170: return realConnection.createStatement(resultSetType,
171: resultSetConcurrency);
172: }
173:
174: public PreparedStatement prepareStatement(String sql)
175: throws SQLException {
176: checkOpen();
177: return realConnection.prepareStatement(sql);
178: }
179:
180: public PreparedStatement prepareStatement(String sql,
181: int resultSetType, int resultSetConcurrency)
182: throws SQLException {
183: checkOpen();
184: return realConnection.prepareStatement(sql, resultSetType,
185: resultSetConcurrency);
186: }
187:
188: public CallableStatement prepareCall(String sql)
189: throws SQLException {
190: checkOpen();
191: return realConnection.prepareCall(sql);
192: }
193:
194: public CallableStatement prepareCall(String sql, int resultSetType,
195: int resultSetConcurrency) throws SQLException {
196: checkOpen();
197: return realConnection.prepareCall(sql, resultSetType,
198: resultSetConcurrency);
199: }
200:
201: public void clearWarnings() throws SQLException {
202: checkOpen();
203: try {
204: realConnection.clearWarnings();
205: } catch (SQLException e) {
206: handleException(e);
207: }
208: }
209:
210: public void commit() throws SQLException {
211: checkOpen();
212: try {
213: realConnection.commit();
214: } catch (SQLException e) {
215: handleException(e);
216: }
217: }
218:
219: public boolean getAutoCommit() throws SQLException {
220: checkOpen();
221: try {
222: return realConnection.getAutoCommit();
223: } catch (SQLException e) {
224: handleException(e);
225: return false;
226: }
227: }
228:
229: public String getCatalog() throws SQLException {
230: checkOpen();
231: try {
232: return realConnection.getCatalog();
233: } catch (SQLException e) {
234: handleException(e);
235: return null;
236: }
237: }
238:
239: public DatabaseMetaData getMetaData() throws SQLException {
240: checkOpen();
241: try {
242: return realConnection.getMetaData();
243: } catch (SQLException e) {
244: handleException(e);
245: return null;
246: }
247: }
248:
249: public int getTransactionIsolation() throws SQLException {
250: checkOpen();
251: try {
252: return realConnection.getTransactionIsolation();
253: } catch (SQLException e) {
254: handleException(e);
255: return -1;
256: }
257: }
258:
259: public Map getTypeMap() throws SQLException {
260: checkOpen();
261: try {
262: return realConnection.getTypeMap();
263: } catch (SQLException e) {
264: handleException(e);
265: return null;
266: }
267: }
268:
269: public SQLWarning getWarnings() throws SQLException {
270: checkOpen();
271: try {
272: return realConnection.getWarnings();
273: } catch (SQLException e) {
274: handleException(e);
275: return null;
276: }
277: }
278:
279: public boolean isReadOnly() throws SQLException {
280: checkOpen();
281: try {
282: return realConnection.isReadOnly();
283: } catch (SQLException e) {
284: handleException(e);
285: return false;
286: }
287: }
288:
289: public String nativeSQL(String sql) throws SQLException {
290: checkOpen();
291: try {
292: return realConnection.nativeSQL(sql);
293: } catch (SQLException e) {
294: handleException(e);
295: return null;
296: }
297: }
298:
299: public void rollback() throws SQLException {
300: checkOpen();
301: try {
302: realConnection.rollback();
303: } catch (SQLException e) {
304: handleException(e);
305: }
306: }
307:
308: public void setAutoCommit(boolean autoCommit) throws SQLException {
309: checkOpen();
310: try {
311: realConnection.setAutoCommit(autoCommit);
312: } catch (SQLException e) {
313: handleException(e);
314: }
315: }
316:
317: public void setCatalog(String catalog) throws SQLException {
318: checkOpen();
319: try {
320: realConnection.setCatalog(catalog);
321: } catch (SQLException e) {
322: handleException(e);
323: }
324: }
325:
326: public void setReadOnly(boolean readOnly) throws SQLException {
327: checkOpen();
328: try {
329: realConnection.setReadOnly(readOnly);
330: } catch (SQLException e) {
331: handleException(e);
332: }
333: }
334:
335: public void setTransactionIsolation(int level) throws SQLException {
336: checkOpen();
337: try {
338: realConnection.setTransactionIsolation(level);
339: } catch (SQLException e) {
340: handleException(e);
341: }
342: }
343:
344: public void setTypeMap(Map map) throws SQLException {
345: checkOpen();
346: try {
347: realConnection.setTypeMap(map);
348: } catch (SQLException e) {
349: handleException(e);
350: }
351: }
352:
353: public boolean isClosed() throws SQLException {
354: if (realConnection == null) {
355: return true;
356: }
357: return realConnection.isClosed();
358: }
359:
360: protected void checkOpen() throws SQLException {
361: if (realConnection != null && realConnection.isClosed()) {
362: throw new SQLException("Connection is closed.");
363: }
364: if (realConnection == null) {
365: throw new SQLException("Connection is closed.");
366: }
367: }
368:
369: public int getHoldability() throws SQLException {
370: checkOpen();
371: try {
372: return realConnection.getHoldability();
373: } catch (SQLException e) {
374: handleException(e);
375: return 0;
376: }
377: }
378:
379: public void setHoldability(int holdability) throws SQLException {
380: checkOpen();
381: try {
382: realConnection.setHoldability(holdability);
383: } catch (SQLException e) {
384: handleException(e);
385: }
386: }
387:
388: public java.sql.Savepoint setSavepoint() throws SQLException {
389: checkOpen();
390: try {
391: return realConnection.setSavepoint();
392: } catch (SQLException e) {
393: handleException(e);
394: return null;
395: }
396: }
397:
398: public java.sql.Savepoint setSavepoint(String name)
399: throws SQLException {
400: checkOpen();
401: try {
402: return realConnection.setSavepoint(name);
403: } catch (SQLException e) {
404: handleException(e);
405: return null;
406: }
407: }
408:
409: public void rollback(java.sql.Savepoint savepoint)
410: throws SQLException {
411: checkOpen();
412: try {
413: realConnection.rollback(savepoint);
414: } catch (SQLException e) {
415: handleException(e);
416: }
417: }
418:
419: public void releaseSavepoint(java.sql.Savepoint savepoint)
420: throws SQLException {
421: checkOpen();
422: try {
423: realConnection.releaseSavepoint(savepoint);
424: } catch (SQLException e) {
425: handleException(e);
426: }
427: }
428:
429: public Statement createStatement(int resultSetType,
430: int resultSetConcurrency, int resultSetHoldability)
431: throws SQLException {
432: checkOpen();
433: try {
434: return realConnection.createStatement(resultSetType,
435: resultSetConcurrency, resultSetHoldability);
436: } catch (SQLException e) {
437: handleException(e);
438: return null;
439: }
440: }
441:
442: public PreparedStatement prepareStatement(String sql,
443: int resultSetType, int resultSetConcurrency,
444: int resultSetHoldability) throws SQLException {
445: checkOpen();
446: try {
447: return realConnection.prepareStatement(sql, resultSetType,
448: resultSetConcurrency, resultSetHoldability);
449: } catch (SQLException e) {
450: handleException(e);
451: return null;
452: }
453: }
454:
455: public CallableStatement prepareCall(String sql, int resultSetType,
456: int resultSetConcurrency, int resultSetHoldability)
457: throws SQLException {
458: checkOpen();
459: try {
460: return realConnection.prepareCall(sql, resultSetType,
461: resultSetConcurrency, resultSetHoldability);
462: } catch (SQLException e) {
463: handleException(e);
464: return null;
465: }
466: }
467:
468: public PreparedStatement prepareStatement(String sql,
469: int autoGeneratedKeys) throws SQLException {
470: checkOpen();
471: try {
472: return realConnection.prepareStatement(sql,
473: autoGeneratedKeys);
474: } catch (SQLException e) {
475: handleException(e);
476: return null;
477: }
478: }
479:
480: public PreparedStatement prepareStatement(String sql,
481: int columnIndexes[]) throws SQLException {
482: checkOpen();
483: try {
484: return realConnection.prepareStatement(sql, columnIndexes);
485: } catch (SQLException e) {
486: handleException(e);
487: return null;
488: }
489: }
490:
491: public PreparedStatement prepareStatement(String sql,
492: String columnNames[]) throws SQLException {
493: checkOpen();
494: try {
495: return realConnection.prepareStatement(sql, columnNames);
496: } catch (SQLException e) {
497: handleException(e);
498: return null;
499: }
500: }
501:
502: public Connection getRealConnection() {
503: return realConnection;
504: }
505:
506: public void setRealConnection(Connection realConnection) {
507: this .realConnection = realConnection;
508: }
509:
510: public boolean isCloseOnReturn() {
511: return closeOnReturn;
512: }
513:
514: public void setCloseOnReturn(boolean closeOnReturn) {
515: this .closeOnReturn = closeOnReturn;
516: }
517:
518: public int getUseCount() {
519: return useCount;
520: }
521:
522: }
|