jmcnally    02/01/13 18:32:49

  Modified:    proposals/jdbc2pool/org/apache/torque/jdbc2pool/adapter Tag:
                        JDBC2POOL_BRANCH ConnectionImpl.java
  Log:
  removed logging from this class, it should be handled elsewhere.
  added documentation as well as a check for isClosed() before executing
  most other methods.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.2   +205 -259  
jakarta-turbine-torque/proposals/jdbc2pool/org/apache/torque/jdbc2pool/adapter/ConnectionImpl.java
  
  Index: ConnectionImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-torque/proposals/jdbc2pool/org/apache/torque/jdbc2pool/adapter/ConnectionImpl.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- ConnectionImpl.java       30 Dec 2001 17:29:44 -0000      1.1.2.1
  +++ ConnectionImpl.java       14 Jan 2002 02:32:49 -0000      1.1.2.2
  @@ -68,27 +68,34 @@
   import javax.sql.ConnectionEvent;
   import javax.sql.ConnectionEventListener;
   import javax.sql.PooledConnection;
  -import org.apache.torque.Torque;
  -import org.apache.log4j.Category;
   
   /**
  - * This class wraps the JDBC <code>Connection</code> class.
  + * This class is used as the <code>Connection</code> that will be returned
  + * from <code>PooledConnectionImpl.getConnection()</code>.  It expects a
  + * <code>Connection</code> that is created by a jdbc 1.x driver or datasource
  + * as well as the PooleConnectionImpl that instantiated this object as 
  + * arguments to the constructor.  All methods except for close() and isClosed()
  + * are wrappers around the jdbc 1.x <code>Connection</code>.  close() will
  + * result in the isClosed() method returning true as well as notifying the
  + * listeners registered with the PooledConnectionImpl of the connectionClosed
  + * event.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>John D. McNally</a>
  - * @version $Id: ConnectionImpl.java,v 1.1.2.1 2001/12/30 17:29:44 jmcnally Exp $
  + * @version $Id: ConnectionImpl.java,v 1.1.2.2 2002/01/14 02:32:49 jmcnally Exp $
    */
   public class ConnectionImpl 
       implements Connection
   {
  +    private static final String CLOSED = 
  +        "Attempted to use Connection after closed() was called.";
  +
       /**
        * The JDBC database connection.
        */
       private Connection connection;
   
       /**
  -     * The JDBC PooledConnection (if supported by the JDBC driver). If this
  -     * is null, then this class uses the classic Connection object to manage
  -     * connection. Else, the PooledConnection object is used.
  +     * The object that instantiated this object
        */
        private PooledConnectionImpl pooledConnection;
   
  @@ -98,23 +105,17 @@
       boolean isClosed;
   
       /**
  -     * Log4j logging category.
  -     */
  -    private Category category;
  -    
  -    /**
  -     * Creates a <code>TorqueConnection</code>.
  +     * Creates a <code>ConnectionImpl</code>.
        *
  -     * @param pooledConnection The PooledConnection used in the close method.
  +     * @param pooledConnection The PooledConnection that is calling the ctor.
        * @param connection The JDBC connection to wrap.
        */
  -    protected ConnectionImpl(PooledConnectionImpl pooledConnection, 
  -                           Connection connection)
  +    ConnectionImpl(PooledConnectionImpl pooledConnection, 
  +                   Connection connection)
       {
           this.pooledConnection = pooledConnection;
           this.connection = connection;
           isClosed = false;
  -        category = Category.getInstance(getClass().getName());
       }
   
   
  @@ -124,15 +125,27 @@
       protected void finalize()
           throws Throwable
       {
  -        if (!isClosed())
  +        if (!isClosed)
           {
               // 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 TorqueConnection was finalized, "
  -                      + "without being returned "
  -                      + "to the ConnectionPool it belonged to" );
  +            throw new SQLException("A ConnectionImpl was finalized "
  +                      + "without being closed which will cause leakage of "
  +                      + " PooledConnections from the ConnectionPool." );
  +        }
  +    }
  +
  +    /**
  +     * Throws an SQLException, if isClosed() is true
  +     */
  +    private void assertOpen()
  +        throws SQLException 
  +    {
  +        if ( isClosed ) 
  +        {
  +            throw new SQLException(CLOSED);
           }
       }
   
  @@ -140,18 +153,17 @@
       // java.sql.Connection implementation using wrapped Connection
       // ***********************************************************************
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public void clearWarnings() 
           throws SQLException 
       {
  -        try
  -        {
  -            connection.clearWarnings();
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  +        assertOpen();
  +        connection.clearWarnings();
       }
   
       /**
  @@ -168,357 +180,291 @@
       }
   
       /**
  -     * Commit the connection.
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
        */
       public void commit()
           throws SQLException
       {
  -        try
  -        {
  -            connection.commit();
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  +        assertOpen();
  +        connection.commit();
       }
   
       /**
  -     * Create a Java SQL statement for this connection.
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
        *
  -     * @return A new <code>Statement</code>.
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
        */
       public Statement createStatement()
  +        throws SQLException 
       {
  -        Statement stmt = null;
  -        try
  -        {
  -            stmt = connection.createStatement();
  -        }
  -        catch (SQLException e)
  -        {
  -            e.printStackTrace();
  -            category.error(e);
  -        }
  -        return stmt;
  +        assertOpen();
  +        return connection.createStatement();
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public Statement createStatement(int resultSetType, 
                                        int resultSetConcurrency) 
           throws SQLException 
       {
  -        Statement stmt = null;
  -        try
  -        {
  -            stmt = connection
  +        assertOpen();
  +        return connection
                   .createStatement(resultSetType, resultSetConcurrency);
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return stmt;
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public boolean getAutoCommit() 
           throws SQLException 
       {
  -        boolean b = false;
  -        try
  -        {
  -            b= connection.getAutoCommit();
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return b;
  +        assertOpen();
  +        return connection.getAutoCommit();
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public String getCatalog() 
           throws SQLException 
       {
  -        String catalog = null;
  -        try
  -        {
  -            catalog = connection.getCatalog();
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return catalog;
  +        assertOpen();
  +        return connection.getCatalog();
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public DatabaseMetaData getMetaData() 
           throws SQLException 
       {
  -        DatabaseMetaData dmd = null;
  -        try
  -        {
  -            dmd = connection.getMetaData();
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return dmd;
  +        assertOpen();
  +        return connection.getMetaData();
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public int getTransactionIsolation() 
           throws SQLException 
       {
  -        int level = 0;
  -        try
  -        {
  -            level = connection.getTransactionIsolation();
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return level;
  +        assertOpen();
  +        return connection.getTransactionIsolation();
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public Map getTypeMap() 
           throws SQLException 
       {
  -        Map map = null;
  -        try
  -        {
  -            map = connection.getTypeMap();
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return map;
  +        assertOpen();
  +        return connection.getTypeMap();
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public SQLWarning getWarnings() 
           throws SQLException 
       {
  -        SQLWarning warning = null;
  -        try
  -        {
  -            warning = connection.getWarnings();
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return warning;
  +        assertOpen();
  +        return connection.getWarnings();
       }
   
  +    /**
  +     * Returns true after close() is called, and false prior to that.
  +     *
  +     * @return a <code>boolean</code> value
  +     */
       public boolean isClosed() 
  -        throws SQLException 
       {
           return isClosed;
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public boolean isReadOnly() 
           throws SQLException 
       {
  -        boolean b = false;
  -        try
  -        {
  -            b = connection.isReadOnly();
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return b;
  +        assertOpen();
  +        return connection.isReadOnly();
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public String nativeSQL(String sql) 
           throws SQLException 
       {
  -        String nativeSql = null;
  -        try
  -        {
  -            nativeSql = connection.nativeSQL(sql);
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return nativeSql;
  +        assertOpen();
  +        return connection.nativeSQL(sql);
       }    
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public CallableStatement prepareCall(String sql) 
           throws SQLException 
       {
  -        CallableStatement stmt = null;
  -        try
  -        {
  -            stmt = connection.prepareCall(sql);
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return stmt;
  +        assertOpen();
  +        return connection.prepareCall(sql);
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public CallableStatement prepareCall(String sql, int resultSetType, 
                                            int resultSetConcurrency) 
           throws SQLException 
       {
  -        CallableStatement stmt = null;
  -        try
  -        {
  -            stmt = connection
  -                .prepareCall(sql,resultSetType,resultSetConcurrency);
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return stmt;
  +        assertOpen();
  +        return connection
  +            .prepareCall(sql, resultSetType, resultSetConcurrency);
       }
   
       /**
  -     * Create a prepared Java SQL statement for this connection.
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
        *
  -     * @param sql The SQL statement to prepare.
  -     * @return A new <code>PreparedStatement</code>.
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
        */
       public PreparedStatement prepareStatement(String sql)
           throws SQLException
       {
  -        PreparedStatement stmt = null;
  -        try
  -        {
  -            stmt = connection.prepareStatement(sql);
  -        }
  -        catch (SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return stmt;
  +        assertOpen();
  +        return connection.prepareStatement(sql);
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public PreparedStatement prepareStatement(String sql, int resultSetType, 
                                                 int resultSetConcurrency) 
           throws SQLException 
       {
  -        PreparedStatement stmt = null;
  -        try
  -        {
  -            stmt = connection
  -                .prepareStatement(sql, resultSetType, resultSetConcurrency);
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  -        return stmt;
  +        assertOpen();
  +        return connection
  +            .prepareStatement(sql, resultSetType, resultSetConcurrency);
       }
   
       /**
  -     * Roll back the connection.
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
        */
       public void rollback()
           throws SQLException
       {
  -        try
  -        {
  -            connection.rollback();
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  +        assertOpen();
  +        connection.rollback();
       }
   
       /**
  -     * Set the autocommit flag for the connection.
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
        *
  -     * @param b True if autocommit should be set to true.
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
        */
       public void setAutoCommit(boolean b)
           throws SQLException
       {
  -        try
  -        {
  -            connection.setAutoCommit(b);
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  +        assertOpen();
  +        connection.setAutoCommit(b);
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public void setCatalog(String catalog) 
           throws SQLException 
       {
  -        try
  -        {
  -            connection.setCatalog(catalog);
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  +        assertOpen();
  +        connection.setCatalog(catalog);
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public void setReadOnly(boolean readOnly) 
           throws SQLException 
       {
  -        try
  -        {
  -            connection.setReadOnly(readOnly);
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  +        assertOpen();
  +        connection.setReadOnly(readOnly);
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public void setTransactionIsolation(int level) 
           throws SQLException 
       {
  -        try
  -        {
  -            connection.setTransactionIsolation(level);
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  +        assertOpen();
  +        connection.setTransactionIsolation(level);
       }
   
  +    /**
  +     * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
  +     *
  +     * @exception SQLException if this connection is closed or an error occurs
  +     * in the wrapped connection.
  +     */
       public void setTypeMap(Map map) 
           throws SQLException 
       {
  -        try
  -        {
  -            connection.setTypeMap(map);
  -        }
  -        catch(SQLException e)
  -        {
  -            category.error(e);
  -            throw e;
  -        }
  +        assertOpen();
  +        connection.setTypeMap(map);
       }
   }
  
  
  

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

Reply via email to