001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.platform.db;
028:
029: import java.sql.CallableStatement;
030: import java.sql.Connection;
031: import java.sql.DatabaseMetaData;
032: import java.sql.PreparedStatement;
033: import java.sql.SQLException;
034: import java.sql.SQLWarning;
035: import java.sql.Savepoint;
036: import java.sql.Statement;
037: import java.util.ArrayList;
038: import java.util.List;
039: import java.util.Map;
040:
041: import javax.sql.ConnectionEventListener;
042:
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045:
046: /**
047: * Wraps a connection for logging of JDBC connection handling.
048: * @author Volker Bergmann
049: * @since 0.3.04
050: */
051: public class PooledConnection implements java.sql.Connection,
052: javax.sql.PooledConnection {
053:
054: private static final Log jdbcLogger = LogFactory
055: .getLog("org.databene.benerator.JDBC");
056:
057: private static long nextId = 0;
058:
059: private Connection realConnection;
060:
061: private long id;
062:
063: private List<ConnectionEventListener> listeners;
064:
065: public PooledConnection(Connection realConnection) {
066: this .id = nextId();
067: this .realConnection = realConnection;
068: this .listeners = new ArrayList<ConnectionEventListener>();
069: if (jdbcLogger.isDebugEnabled())
070: jdbcLogger.debug("Created connection #" + id + ": "
071: + realConnection);
072: }
073:
074: public void close() throws SQLException {
075: try {
076: realConnection.close();
077: if (jdbcLogger.isDebugEnabled())
078: jdbcLogger.debug("Closed connection #" + id + ": "
079: + realConnection);
080: } catch (SQLException e) {
081: jdbcLogger.error("Error closing connection #" + id + ": "
082: + realConnection, e);
083: throw e;
084: }
085: }
086:
087: public Connection getConnection() throws SQLException {
088: return realConnection;
089: }
090:
091: public void addConnectionEventListener(
092: ConnectionEventListener listener) {
093: listeners.add(listener);
094: }
095:
096: public void removeConnectionEventListener(
097: ConnectionEventListener listener) {
098: listeners.remove(listener);
099: }
100:
101: /**
102: * @throws SQLException
103: * @see java.sql.Connection#clearWarnings()
104: */
105: public void clearWarnings() throws SQLException {
106: realConnection.clearWarnings();
107: }
108:
109: /**
110: * @throws SQLException
111: * @see java.sql.Connection#commit()
112: */
113: public void commit() throws SQLException {
114: realConnection.commit();
115: }
116:
117: /**
118: * @return
119: * @throws SQLException
120: * @see java.sql.Connection#createStatement()
121: */
122: public Statement createStatement() throws SQLException {
123: return realConnection.createStatement();
124: }
125:
126: /**
127: * @param resultSetType
128: * @param resultSetConcurrency
129: * @param resultSetHoldability
130: * @return
131: * @throws SQLException
132: * @see java.sql.Connection#createStatement(int, int, int)
133: */
134: public Statement createStatement(int resultSetType,
135: int resultSetConcurrency, int resultSetHoldability)
136: throws SQLException {
137: return realConnection.createStatement(resultSetType,
138: resultSetConcurrency, resultSetHoldability);
139: }
140:
141: /**
142: * @param resultSetType
143: * @param resultSetConcurrency
144: * @return
145: * @throws SQLException
146: * @see java.sql.Connection#createStatement(int, int)
147: */
148: public Statement createStatement(int resultSetType,
149: int resultSetConcurrency) throws SQLException {
150: return realConnection.createStatement(resultSetType,
151: resultSetConcurrency);
152: }
153:
154: /**
155: * @return
156: * @throws SQLException
157: * @see java.sql.Connection#getAutoCommit()
158: */
159: public boolean getAutoCommit() throws SQLException {
160: return realConnection.getAutoCommit();
161: }
162:
163: /**
164: * @return
165: * @throws SQLException
166: * @see java.sql.Connection#getCatalog()
167: */
168: public String getCatalog() throws SQLException {
169: return realConnection.getCatalog();
170: }
171:
172: /**
173: * @return
174: * @throws SQLException
175: * @see java.sql.Connection#getHoldability()
176: */
177: public int getHoldability() throws SQLException {
178: return realConnection.getHoldability();
179: }
180:
181: /**
182: * @return
183: * @throws SQLException
184: * @see java.sql.Connection#getMetaData()
185: */
186: public DatabaseMetaData getMetaData() throws SQLException {
187: return realConnection.getMetaData();
188: }
189:
190: /**
191: * @return
192: * @throws SQLException
193: * @see java.sql.Connection#getTransactionIsolation()
194: */
195: public int getTransactionIsolation() throws SQLException {
196: return realConnection.getTransactionIsolation();
197: }
198:
199: /**
200: * @return
201: * @throws SQLException
202: * @see java.sql.Connection#getTypeMap()
203: */
204: public Map<String, Class<?>> getTypeMap() throws SQLException {
205: return realConnection.getTypeMap();
206: }
207:
208: /**
209: * @return
210: * @throws SQLException
211: * @see java.sql.Connection#getWarnings()
212: */
213: public SQLWarning getWarnings() throws SQLException {
214: return realConnection.getWarnings();
215: }
216:
217: /**
218: * @return
219: * @throws SQLException
220: * @see java.sql.Connection#isClosed()
221: */
222: public boolean isClosed() throws SQLException {
223: return realConnection.isClosed();
224: }
225:
226: /**
227: * @return
228: * @throws SQLException
229: * @see java.sql.Connection#isReadOnly()
230: */
231: public boolean isReadOnly() throws SQLException {
232: return realConnection.isReadOnly();
233: }
234:
235: /**
236: * @param sql
237: * @return
238: * @throws SQLException
239: * @see java.sql.Connection#nativeSQL(java.lang.String)
240: */
241: public String nativeSQL(String sql) throws SQLException {
242: return realConnection.nativeSQL(sql);
243: }
244:
245: /**
246: * @param sql
247: * @param resultSetType
248: * @param resultSetConcurrency
249: * @param resultSetHoldability
250: * @return
251: * @throws SQLException
252: * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
253: */
254: public CallableStatement prepareCall(String sql, int resultSetType,
255: int resultSetConcurrency, int resultSetHoldability)
256: throws SQLException {
257: return realConnection.prepareCall(sql, resultSetType,
258: resultSetConcurrency, resultSetHoldability);
259: }
260:
261: /**
262: * @param sql
263: * @param resultSetType
264: * @param resultSetConcurrency
265: * @return
266: * @throws SQLException
267: * @see java.sql.Connection#prepareCall(java.lang.String, int, int)
268: */
269: public CallableStatement prepareCall(String sql, int resultSetType,
270: int resultSetConcurrency) throws SQLException {
271: return realConnection.prepareCall(sql, resultSetType,
272: resultSetConcurrency);
273: }
274:
275: /**
276: * @param sql
277: * @return
278: * @throws SQLException
279: * @see java.sql.Connection#prepareCall(java.lang.String)
280: */
281: public CallableStatement prepareCall(String sql)
282: throws SQLException {
283: return realConnection.prepareCall(sql);
284: }
285:
286: /**
287: * @param sql
288: * @param resultSetType
289: * @param resultSetConcurrency
290: * @param resultSetHoldability
291: * @return
292: * @throws SQLException
293: * @see java.sql.Connection#prepareStatement(java.lang.String, int, int, int)
294: */
295: public PreparedStatement prepareStatement(String sql,
296: int resultSetType, int resultSetConcurrency,
297: int resultSetHoldability) throws SQLException {
298: return realConnection.prepareStatement(sql, resultSetType,
299: resultSetConcurrency, resultSetHoldability);
300: }
301:
302: /**
303: * @param sql
304: * @param resultSetType
305: * @param resultSetConcurrency
306: * @return
307: * @throws SQLException
308: * @see java.sql.Connection#prepareStatement(java.lang.String, int, int)
309: */
310: public PreparedStatement prepareStatement(String sql,
311: int resultSetType, int resultSetConcurrency)
312: throws SQLException {
313: return realConnection.prepareStatement(sql, resultSetType,
314: resultSetConcurrency);
315: }
316:
317: /**
318: * @param sql
319: * @param autoGeneratedKeys
320: * @return
321: * @throws SQLException
322: * @see java.sql.Connection#prepareStatement(java.lang.String, int)
323: */
324: public PreparedStatement prepareStatement(String sql,
325: int autoGeneratedKeys) throws SQLException {
326: return realConnection.prepareStatement(sql, autoGeneratedKeys);
327: }
328:
329: /**
330: * @param sql
331: * @param columnIndexes
332: * @return
333: * @throws SQLException
334: * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
335: */
336: public PreparedStatement prepareStatement(String sql,
337: int[] columnIndexes) throws SQLException {
338: return realConnection.prepareStatement(sql, columnIndexes);
339: }
340:
341: /**
342: * @param sql
343: * @param columnNames
344: * @return
345: * @throws SQLException
346: * @see java.sql.Connection#prepareStatement(java.lang.String, java.lang.String[])
347: */
348: public PreparedStatement prepareStatement(String sql,
349: String[] columnNames) throws SQLException {
350: return realConnection.prepareStatement(sql, columnNames);
351: }
352:
353: /**
354: * @param sql
355: * @return
356: * @throws SQLException
357: * @see java.sql.Connection#prepareStatement(java.lang.String)
358: */
359: public PreparedStatement prepareStatement(String sql)
360: throws SQLException {
361: return realConnection.prepareStatement(sql);
362: }
363:
364: /**
365: * @param savepoint
366: * @throws SQLException
367: * @see java.sql.Connection#releaseSavepoint(java.sql.Savepoint)
368: */
369: public void releaseSavepoint(Savepoint savepoint)
370: throws SQLException {
371: realConnection.releaseSavepoint(savepoint);
372: }
373:
374: /**
375: * @throws SQLException
376: * @see java.sql.Connection#rollback()
377: */
378: public void rollback() throws SQLException {
379: realConnection.rollback();
380: }
381:
382: /**
383: * @param savepoint
384: * @throws SQLException
385: * @see java.sql.Connection#rollback(java.sql.Savepoint)
386: */
387: public void rollback(Savepoint savepoint) throws SQLException {
388: realConnection.rollback(savepoint);
389: }
390:
391: /**
392: * @param autoCommit
393: * @throws SQLException
394: * @see java.sql.Connection#setAutoCommit(boolean)
395: */
396: public void setAutoCommit(boolean autoCommit) throws SQLException {
397: realConnection.setAutoCommit(autoCommit);
398: }
399:
400: /**
401: * @param catalog
402: * @throws SQLException
403: * @see java.sql.Connection#setCatalog(java.lang.String)
404: */
405: public void setCatalog(String catalog) throws SQLException {
406: realConnection.setCatalog(catalog);
407: }
408:
409: /**
410: * @param holdability
411: * @throws SQLException
412: * @see java.sql.Connection#setHoldability(int)
413: */
414: public void setHoldability(int holdability) throws SQLException {
415: realConnection.setHoldability(holdability);
416: }
417:
418: /**
419: * @param readOnly
420: * @throws SQLException
421: * @see java.sql.Connection#setReadOnly(boolean)
422: */
423: public void setReadOnly(boolean readOnly) throws SQLException {
424: realConnection.setReadOnly(readOnly);
425: }
426:
427: /**
428: * @return
429: * @throws SQLException
430: * @see java.sql.Connection#setSavepoint()
431: */
432: public Savepoint setSavepoint() throws SQLException {
433: return realConnection.setSavepoint();
434: }
435:
436: /**
437: * @param name
438: * @return
439: * @throws SQLException
440: * @see java.sql.Connection#setSavepoint(java.lang.String)
441: */
442: public Savepoint setSavepoint(String name) throws SQLException {
443: return realConnection.setSavepoint(name);
444: }
445:
446: /**
447: * @param level
448: * @throws SQLException
449: * @see java.sql.Connection#setTransactionIsolation(int)
450: */
451: public void setTransactionIsolation(int level) throws SQLException {
452: realConnection.setTransactionIsolation(level);
453: }
454:
455: /**
456: * @param map
457: * @throws SQLException
458: * @see java.sql.Connection#setTypeMap(java.util.Map)
459: */
460: public void setTypeMap(Map<String, Class<?>> map)
461: throws SQLException {
462: realConnection.setTypeMap(map);
463: }
464:
465: // private helpers ------------------------------------------------------------------------------------------
466:
467: private static synchronized long nextId() {
468: return ++nextId;
469: }
470: }
|