commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwaldh...@apache.org
Subject cvs commit: jakarta-commons/dbcp/src/test/org/apache/commons/dbcp TestManual.java
Date Fri, 01 Nov 2002 15:42:33 GMT
rwaldhoff    2002/11/01 07:42:33

  Modified:    dbcp/src/java/org/apache/commons/dbcp
                        DelegatingPreparedStatement.java
                        PoolablePreparedStatement.java
               dbcp/src/test/org/apache/commons/dbcp TestManual.java
  Log:
  ensure PreparedStatment can only be closed once
  
  Revision  Changes    Path
  1.7       +9 -5      jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/DelegatingPreparedStatement.java
  
  Index: DelegatingPreparedStatement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/DelegatingPreparedStatement.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- DelegatingPreparedStatement.java	31 Oct 2002 21:14:32 -0000	1.6
  +++ DelegatingPreparedStatement.java	1 Nov 2002 15:42:32 -0000	1.7
  @@ -153,8 +153,8 @@
        * any ResultSets that were not explicitly closed.
        */
       public void close() throws SQLException {
  -        passivate();
           _stmt.close();
  +        passivate();
       }
   
       public Connection getConnection() throws SQLException {
  @@ -272,6 +272,10 @@
           if(_stmt instanceof DelegatingPreparedStatement) {
               ((DelegatingPreparedStatement)_stmt).passivate();
           }
  +    }
  +
  +    protected boolean isClosed() {
  +        return _closed;
       }
   
       protected boolean _closed = false;
  
  
  
  1.4       +16 -13    jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/PoolablePreparedStatement.java
  
  Index: PoolablePreparedStatement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/PoolablePreparedStatement.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PoolablePreparedStatement.java	16 May 2002 21:25:38 -0000	1.3
  +++ PoolablePreparedStatement.java	1 Nov 2002 15:42:32 -0000	1.4
  @@ -112,15 +112,18 @@
        * Return me to my pool.
        */
       public void close() throws SQLException {
  -        // pool will mark me as closed
  -        try {
  -            _pool.returnObject(_key,this);
  -        } catch(SQLException e) {
  -            throw e;
  -        } catch(RuntimeException e) {
  -            throw e;
  -        } catch(Exception e) {
  -            throw new SQLException(e.toString());
  +        if(isClosed()) {
  +            throw new SQLException("Already closed");
  +        } else {
  +            try {
  +                _pool.returnObject(_key,this);
  +            } catch(SQLException e) {
  +                throw e;
  +            } catch(RuntimeException e) {
  +                throw e;
  +            } catch(Exception e) {
  +                throw new SQLException(e.toString());
  +            }
           }
       }
   
  
  
  
  1.10      +23 -5     jakarta-commons/dbcp/src/test/org/apache/commons/dbcp/TestManual.java
  
  Index: TestManual.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbcp/src/test/org/apache/commons/dbcp/TestManual.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- TestManual.java	1 Nov 2002 15:27:21 -0000	1.9
  +++ TestManual.java	1 Nov 2002 15:42:33 -0000	1.10
  @@ -114,7 +114,7 @@
           }
       }
   
  -    public void testCantCloseTwice() throws Exception {
  +    public void testCantCloseConnectionTwice() throws Exception {
           for(int i=0;i<2;i++) { // loop to show we *can* close again once we've borrowed
it from the pool again
               Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
               assertTrue(null != conn);
  @@ -129,6 +129,24 @@
               }
               assertTrue(conn.isClosed());
           }
  +    }
  +
  +    public void testCantCloseStatementTwice() throws Exception {
  +        Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
  +        assertTrue(null != conn);
  +        assertTrue(!conn.isClosed());
  +        for(int i=0;i<2;i++) { // loop to show we *can* close again once we've borrowed
it from the pool again
  +            PreparedStatement stmt = conn.prepareStatement("select * from dual");
  +            assertTrue(null != stmt);
  +            stmt.close();
  +            try {
  +                stmt.close();
  +                fail("Expected SQLException on second attempt to close");
  +            } catch(SQLException e) {
  +                // expected
  +            }
  +        }
  +        conn.close();
       }
   
       public void testSimple() throws Exception {
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message