001: // jTDS JDBC Driver for Microsoft SQL Server and Sybase
002: // Copyright (C) 2004 The jTDS Project
003: //
004: // This library is free software; you can redistribute it and/or
005: // modify it under the terms of the GNU Lesser General Public
006: // License as published by the Free Software Foundation; either
007: // version 2.1 of the License, or (at your option) any later version.
008: //
009: // This library is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: // Lesser General Public License for more details.
013: //
014: // You should have received a copy of the GNU Lesser General Public
015: // License along with this library; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: //
018: package net.sourceforge.jtds.jdbcx;
019:
020: import java.sql.*;
021: import java.util.*;
022:
023: import javax.sql.*;
024: import net.sourceforge.jtds.jdbc.*;
025: import net.sourceforge.jtds.jdbcx.proxy.*;
026:
027: /**
028: * jTDS implementation of the <code>PooledConnection</code> interface.
029: *
030: * @version $Id: PooledConnection.java,v 1.11 2005/02/01 23:52:50 alin_sinpalean Exp $
031: */
032: public class PooledConnection implements javax.sql.PooledConnection {
033: private ArrayList listeners = new ArrayList();
034:
035: protected Connection connection;
036:
037: public PooledConnection(Connection connection) {
038: this .connection = connection;
039: }
040:
041: /**
042: * Adds the specified listener to the list.
043: *
044: * @see #fireConnectionEvent
045: * @see #removeConnectionEventListener
046: */
047: public synchronized void addConnectionEventListener(
048: ConnectionEventListener listener) {
049: // Clone the list of listeners to avoid concurrent modifications. See
050: // bug [1113040] Small bug in net.sourceforge.jtds.jdbcx.PooledConnection
051: // for a description of how these can occur. The method still needs to
052: // be synchronized to prevent race conditions.
053: listeners = (ArrayList) listeners.clone();
054: // Now add the listener to the new, cloned list
055: listeners.add(listener);
056: }
057:
058: /**
059: * Closes the database connection.
060: *
061: * @throws SQLException if an error occurs
062: */
063: public synchronized void close() throws SQLException {
064: connection.close();
065: connection = null; // Garbage collect the connection
066: }
067:
068: /**
069: * Fires a new connection event on all listeners.
070: *
071: * @param closed <code>true</code> if <code>close</code> has been called on the
072: * connection; <code>false</code> if the <code>sqlException</code> represents
073: * an error where the connection may not longer be used.
074: * @param sqlException the SQLException to pass to the listeners
075: */
076: public synchronized void fireConnectionEvent(boolean closed,
077: SQLException sqlException) {
078: if (listeners.size() > 0) {
079: ConnectionEvent connectionEvent = new ConnectionEvent(this ,
080: sqlException);
081: Iterator iterator = listeners.iterator();
082:
083: while (iterator.hasNext()) {
084: ConnectionEventListener listener = (ConnectionEventListener) iterator
085: .next();
086:
087: if (closed) {
088: listener.connectionClosed(connectionEvent);
089: } else {
090: try {
091: if (connection == null || connection.isClosed()) {
092: listener
093: .connectionErrorOccurred(connectionEvent);
094: }
095: } catch (SQLException ex) {
096: // Will never occur
097: }
098: }
099: }
100: }
101: }
102:
103: /**
104: * Returns a ConnectionProxy.
105: *
106: * @throws SQLException if an error occurs
107: */
108: public synchronized Connection getConnection() throws SQLException {
109: if (connection == null) {
110: fireConnectionEvent(false, new SQLException(Messages
111: .get("error.jdbcx.conclosed"), "08003"));
112:
113: return null;
114: }
115:
116: // Should the SQLException be captured here for safety in the future even though
117: // no SQLException is being thrown by the ConnectionProxy at the moment???
118: return new ConnectionProxy(this , connection);
119: }
120:
121: /**
122: * Removes the specified listener from the list.
123: *
124: * @see #addConnectionEventListener
125: * @see #fireConnectionEvent
126: */
127: public synchronized void removeConnectionEventListener(
128: ConnectionEventListener listener) {
129: // Clone the list of listeners to avoid concurrent modifications. See
130: // bug [1113040] Small bug in net.sourceforge.jtds.jdbcx.PooledConnection
131: // for a description of how these can occur. The method still needs to
132: // be synchronized to prevent race conditions.
133: listeners = (ArrayList) listeners.clone();
134: // Now remove the listener from the new, cloned list
135: listeners.remove(listener);
136: }
137: }
|