/**
  * Adds the specified listener to the list.
  *
  * @see #fireConnectionEvent
  * @see #removeConnectionEventListener
  */
 public synchronized void addConnectionEventListener(ConnectionEventListener listener) {
   // Clone the list of listeners to avoid concurrent modifications. See
   // bug [1113040] Small bug in net.sourceforge.jtds.jdbcx.PooledConnection
   // for a description of how these can occur. The method still needs to
   // be synchronized to prevent race conditions.
   listeners = (ArrayList) listeners.clone();
   // Now add the listener to the new, cloned list
   listeners.add(listener);
 }
  /**
   * Fires a new connection event on all listeners.
   *
   * @param closed <code>true</code> if <code>close</code> has been called on the connection; <code>
   *     false</code> if the <code>sqlException</code> represents an error where the connection may
   *     not longer be used.
   * @param sqlException the SQLException to pass to the listeners
   */
  public synchronized void fireConnectionEvent(boolean closed, SQLException sqlException) {
    if (_listeners.size() > 0) {
      ConnectionEvent connectionEvent = new ConnectionEvent(this, sqlException);
      Iterator iterator = _listeners.iterator();

      while (iterator.hasNext()) {
        ConnectionEventListener listener = (ConnectionEventListener) iterator.next();

        if (closed) {
          listener.connectionClosed(connectionEvent);
        } else {
          listener.connectionErrorOccurred(connectionEvent);
        }
      }
    }
  }
  /**
   * Fires a new connection event on all listeners.
   *
   * @param closed <code>true</code> if <code>close</code> has been called on the connection; <code>
   *     false</code> if the <code>sqlException</code> represents an error where the connection may
   *     not longer be used.
   * @param sqlException the SQLException to pass to the listeners
   */
  public synchronized void fireConnectionEvent(boolean closed, SQLException sqlException) {
    if (listeners.size() > 0) {
      ConnectionEvent connectionEvent = new ConnectionEvent(this, sqlException);
      Iterator iterator = listeners.iterator();

      while (iterator.hasNext()) {
        ConnectionEventListener listener = (ConnectionEventListener) iterator.next();

        if (closed) {
          listener.connectionClosed(connectionEvent);
        } else {
          try {
            if (connection == null || connection.isClosed()) {
              listener.connectionErrorOccurred(connectionEvent);
            }
          } catch (SQLException ex) {
            // Will never occur
          }
        }
      }
    }
  }
 /**
  * Adds the specified listener to the list.
  *
  * @see #fireConnectionEvent
  * @see #removeConnectionEventListener
  */
 public synchronized void addConnectionEventListener(ConnectionEventListener listener) {
   _listeners.add(listener);
 }
 /**
  * Removes the specified listener from the list.
  *
  * @see #addConnectionEventListener
  * @see #fireConnectionEvent
  */
 public synchronized void removeConnectionEventListener(ConnectionEventListener listener) {
   _listeners.remove(listener);
 }