001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.sql.spy;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.L10N;
033:
034: import javax.sql.ConnectionEventListener;
035: import javax.sql.PooledConnection;
036: import java.sql.Connection;
037: import java.sql.SQLException;
038: import java.util.logging.*;
039: import javax.sql.StatementEventListener;
040:
041: /**
042: * Spying on a connection.
043: */
044: public class SpyPooledConnection implements javax.sql.PooledConnection {
045: protected final static Logger log = Log
046: .open(SpyPooledConnection.class);
047: protected final static L10N L = new L10N(SpyPooledConnection.class);
048:
049: protected String _id;
050:
051: private SpyDataSource _spyDataSource;
052: private int _connCount;
053:
054: // The underlying connection
055: private PooledConnection _pconn;
056:
057: /**
058: * Creates a new SpyConnection.
059: */
060: public SpyPooledConnection(PooledConnection conn, String id) {
061: _spyDataSource = new SpyDataSource();
062:
063: _pconn = conn;
064: _id = id;
065: }
066:
067: public void addConnectionEventListener(
068: ConnectionEventListener listener) {
069: _pconn.addConnectionEventListener(listener);
070: }
071:
072: public void removeConnectionEventListener(
073: ConnectionEventListener listener) {
074: _pconn.removeConnectionEventListener(listener);
075: }
076:
077: public Connection getConnection() throws SQLException {
078: try {
079: Connection conn = _pconn.getConnection();
080:
081: String connId = null;
082:
083: if (log.isLoggable(Level.FINE)) {
084: connId = _spyDataSource.createConnectionId();
085:
086: log.fine(_id + ":connect() -> " + connId + ":" + conn);
087: }
088:
089: return new SpyConnection(conn, _spyDataSource, connId);
090: } catch (SQLException e) {
091: log.fine(_id + ":exn-connect(" + e + ")");
092:
093: throw e;
094: }
095: }
096:
097: public void close() throws SQLException {
098: try {
099: _pconn.close();
100:
101: log.fine(_id + ":close()");
102: } catch (SQLException e) {
103: log.fine(_id + ":exn-close(" + e + ")");
104:
105: throw e;
106: }
107: }
108:
109: public String toString() {
110: return "SpyPooledConnection[id=" + _id + ",conn=" + _pconn
111: + "]";
112: }
113:
114: public void addStatementEventListener(
115: StatementEventListener listener) {
116: throw new UnsupportedOperationException("Not supported yet.");
117: }
118:
119: public void removeStatementEventListener(
120: StatementEventListener listener) {
121: throw new UnsupportedOperationException("Not supported yet.");
122: }
123: }
|