jmcnally    02/01/13 20:33:38

  Modified:    proposals/jdbc2pool/org/apache/torque/jdbc2pool/adapter Tag:
                        JDBC2POOL_BRANCH PooledConnectionImpl.java
  Log:
  getConnection will throw exception if called after close().
  removed logging from this class, just throw exceptions.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.2   +54 -23    
jakarta-turbine-torque/proposals/jdbc2pool/org/apache/torque/jdbc2pool/adapter/PooledConnectionImpl.java
  
  Index: PooledConnectionImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-torque/proposals/jdbc2pool/org/apache/torque/jdbc2pool/adapter/PooledConnectionImpl.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- PooledConnectionImpl.java 30 Dec 2001 17:29:44 -0000      1.1.2.1
  +++ PooledConnectionImpl.java 14 Jan 2002 04:33:38 -0000      1.1.2.2
  @@ -56,7 +56,8 @@
   
   import java.util.Map;
   import java.util.Iterator;
  -import java.io.PrintStream;
  +import java.util.EventObject;
  +import java.util.Vector;
   import java.sql.Connection;
   import java.sql.DatabaseMetaData;
   import java.sql.PreparedStatement;
  @@ -64,24 +65,23 @@
   import java.sql.Statement;
   import java.sql.SQLWarning;
   import java.sql.SQLException;
  -import java.util.EventObject;
  -import java.util.Vector;
   import javax.sql.ConnectionEvent;
   import javax.sql.ConnectionEventListener;
   import javax.sql.PooledConnection;
  -import org.apache.torque.Torque;
  -import org.apache.log4j.Category;
   
   /**
    * Implementation of PooledConnection that is returned by
    * PooledConnectionDataSource.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>John D. McNally</a>
  - * @version $Id: PooledConnectionImpl.java,v 1.1.2.1 2001/12/30 17:29:44 jmcnally 
Exp $
  + * @version $Id: PooledConnectionImpl.java,v 1.1.2.2 2002/01/14 04:33:38 jmcnally 
Exp $
    */
   public class PooledConnectionImpl 
       implements PooledConnection
   {
  +    private static final String CLOSED = 
  +        "Attempted to use PooledConnection after closed() was called.";
  +
       /**
        * The JDBC database connection that represents the physical db connection.
        */
  @@ -90,7 +90,7 @@
       /**
        * The JDBC database logical connection.
        */
  -    private Connection torqueConnection = null;
  +    private Connection logicalConnection = null;
   
       /**
        * ConnectionEventListeners
  @@ -98,10 +98,10 @@
       private Vector eventListeners;
   
       /**
  -     * Log4j logging category.
  +     * flag set to true, once close() is called.
        */
  -    private Category category;
  -    
  +    boolean isClosed;
  +
       /**
        * Wrap the real connection.
        */
  @@ -109,7 +109,7 @@
       {
           this.connection = connection;
           eventListeners = new Vector();
  -        category = Category.getInstance(getClass().getName());
  +        isClosed = false;
       }
   
       /**
  @@ -123,13 +123,33 @@
           }
       }
   
  +    /**
  +     * Closes the physical connection and marks this 
  +     * <code>PooledConnection</code> so that it may not be used 
  +     * to generate any more logical <code>Connection</code>s.
  +     *
  +     * @exception SQLException if an error occurs
  +     */
       public void close()
           throws SQLException
       {        
  +        isClosed = true;
           connection.close();
       }
   
       /**
  +     * Throws an SQLException, if isClosed() is true
  +     */
  +    private void assertOpen()
  +        throws SQLException 
  +    {
  +        if ( isClosed ) 
  +        {
  +            throw new SQLException(CLOSED);
  +        }
  +    }
  +
  +    /**
        * Returns a JDBC connection.
        *
        * @return The database connection.
  @@ -137,8 +157,9 @@
       public Connection getConnection()
           throws SQLException
       {
  +        assertOpen();
           // make sure the last connection is marked as closed
  -        if ( torqueConnection != null && !torqueConnection.isClosed() ) 
  +        if ( logicalConnection != null && !logicalConnection.isClosed() ) 
           {
               // should notify pool of error so the pooled connection can
               // be removed !FIXME!
  @@ -147,8 +168,8 @@
           }
   
           // the spec requires that this return a new Connection instance.
  -        torqueConnection = new ConnectionImpl(this, connection);
  -        return torqueConnection;
  +        logicalConnection = new ConnectionImpl(this, connection);
  +        return logicalConnection;
       }
   
       /**
  @@ -160,23 +181,33 @@
       }
   
       /**
  -     * The finalizer helps prevent <code>ConnectionPool</code> leakage.
  +     * Closes the physical connection and checks that the logical connection
  +     * was closed as well.
        */
       protected void finalize()
           throws Throwable
       {
  -        // If this DBConnection object is finalized while linked
  -        // to a ConnectionPool, it means that it was taken from a pool
  -        // and not returned.  We log this fact, close the underlying
  -        // Connection, and return it to the ConnectionPool.
  -        category.warn("A PooledConnection was finalized, without being " + 
  -                        "returned to the ConnectionPool it belonged to");
  -        
           // Closing the Connection ensures that if anyone tries to use it,
           // an error will occur.
  -        connection.close();
  +        try
  +        {
  +            connection.close();
  +        }
  +        catch (Exception ignored)
  +        {
  +        }
  +
  +        // make sure the last connection is marked as closed
  +        if ( logicalConnection != null && !logicalConnection.isClosed() ) 
  +        {
  +            throw new SQLException("PooledConnection was gc'ed, without" +
  +                                   "its last Connection being closed.");
  +        }        
       }
   
  +    /**
  +     * sends a connectionClosed event.
  +     */
       void notifyListeners()
       {
           ConnectionEvent event = new ConnectionEvent(this);
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to