You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by kr...@apache.org on 2008/02/28 21:17:50 UTC

svn commit: r632112 - in /db/derby/code/trunk/java/client/org/apache/derby/client/am: CachingLogicalConnection.java CachingLogicalConnection40.java LogicalStatementEntity.java StatementCacheInteractor.java

Author: kristwaa
Date: Thu Feb 28 12:17:48 2008
New Revision: 632112

URL: http://svn.apache.org/viewvc?rev=632112&view=rev
Log:
DERBY-3457: Closing a logical connection must close all associated logical statements.
Patch file: derby-3457-1c-stmt_closing.diff

Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/CachingLogicalConnection.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/CachingLogicalConnection40.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/StatementCacheInteractor.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/CachingLogicalConnection.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/CachingLogicalConnection.java?rev=632112&r1=632111&r2=632112&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/CachingLogicalConnection.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/CachingLogicalConnection.java Thu Feb 28 12:17:48 2008
@@ -67,6 +67,7 @@
     public synchronized void close()
             throws SQLException {
         if (this.cacheInteractor != null) {
+            this.cacheInteractor.closeOpenLogicalStatements();
             // Nullify reference to cache interactor to allow it to be GC'ed.
             // It should not be used again when, logical connection is closed.
             this.cacheInteractor = null;

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/CachingLogicalConnection40.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/CachingLogicalConnection40.java?rev=632112&r1=632111&r2=632112&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/CachingLogicalConnection40.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/CachingLogicalConnection40.java Thu Feb 28 12:17:48 2008
@@ -62,6 +62,7 @@
     public synchronized void close()
             throws SQLException {
         if (this.cacheInteractor != null) {
+            this.cacheInteractor.closeOpenLogicalStatements();
             // Nullify reference to cache interactor to allow it to be GC'ed.
             // It should not be used again, the logical connection is closed.
             this.cacheInteractor = null;

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java?rev=632112&r1=632111&r2=632112&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java Thu Feb 28 12:17:48 2008
@@ -71,6 +71,8 @@
      */
     //@GuardedBy("this)
     private java.sql.CallableStatement physicalCs;
+    /** The owner of this logical entity. */
+    private StatementCacheInteractor owner;
     /** The key for the associated statement. */
     private final StatementKey stmtKey;
     /** Cache for physical statements. */
@@ -96,6 +98,7 @@
         }
         this.stmtKey = stmtKey;
         this.cache = cacheInteractor.getCache();
+        this.owner = cacheInteractor;
         this.physicalPs = physicalPs;
         if (physicalPs instanceof CallableStatement) {
             this.hasCallableStmt = true;
@@ -156,9 +159,12 @@
             physicalPs = null;
             physicalCs = null;
 
+            this.owner.markClosed(this);
+            // Nullify the reference, since the entity object might stick around
+            // for a while.
+            this.owner = null;
             // Reset the owner of the physical statement.
             temporaryPsRef.setOwner(null);
-
             // If the underlying statement has become closed, don't cache it.
             if (temporaryPsRef.isClosed()) {
                 return;

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/StatementCacheInteractor.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/StatementCacheInteractor.java?rev=632112&r1=632111&r2=632112&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/StatementCacheInteractor.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/StatementCacheInteractor.java Thu Feb 28 12:17:48 2008
@@ -5,6 +5,7 @@
 import java.sql.SQLException;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 
 import org.apache.derby.client.am.stmtcache.JDBCStatementCache;
 import org.apache.derby.client.am.stmtcache.StatementKey;
@@ -41,6 +42,12 @@
     /** List of open logical statements created by this cache interactor. */
     //@GuardedBy("this")
     private final ArrayList openLogicalStatements = new ArrayList();
+    /**
+     * Tells if this interactor is in the process of shutting down.
+     * <p>
+     * If this is true, it means that the logical connection is being closed.
+     */
+    private boolean connCloseInProgress = false;
 
     /**
      * Creates a new JDBC statement cache interactor.
@@ -175,6 +182,46 @@
                     resultSetHoldability);
         }
         return createLogicalCallableStatement(cs, stmtKey);
+    }
+
+    /**
+     * Closes all open logical statements created by this cache interactor.
+     * <p>
+     * A cache interactor is bound to a single (caching) logical connection.
+     * @throws SQLException if closing an open logical connection fails
+     */
+    public synchronized void closeOpenLogicalStatements()
+            throws SQLException {
+        // Transist to closing state, to avoid changing the list of open
+        // statements as we work our way through the list.
+        this.connCloseInProgress = true;
+        // Iterate through the list and close the logical statements.
+        Iterator logicalStatements = this.openLogicalStatements.iterator();
+        while (logicalStatements.hasNext()) {
+            LogicalStatementEntity logicalStatement =
+                    (LogicalStatementEntity)logicalStatements.next();
+            logicalStatement.close();
+        }
+        // Clear the list for good measure.
+        this.openLogicalStatements.clear();
+    }
+
+    /**
+     * Designates the specified logical statement as closed.
+     *
+     * @param logicalStmt the logical statement being closed
+     */
+    public synchronized void markClosed(LogicalStatementEntity logicalStmt) {
+        // If we are not in the process of shutting down the logical connection,
+        // remove the notifying statement from the list of open statements.
+        if (!connCloseInProgress) {
+            boolean removed = this.openLogicalStatements.remove(logicalStmt);
+            if (SanityManager.DEBUG) {
+                SanityManager.ASSERT(removed,
+                    "Tried to remove unregistered logical statement: " +
+                    logicalStmt);
+            }
+        }
     }
 
     /**