db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r1033485 - in /db/derby/code/trunk/java/engine/org/apache/derby: iapi/sql/conn/LanguageConnectionContext.java iapi/sql/dictionary/TableDescriptor.java impl/sql/conn/GenericLanguageConnectionContext.java
Date Wed, 10 Nov 2010 14:22:57 GMT
Author: dag
Date: Wed Nov 10 14:22:56 2010
New Revision: 1033485

URL: http://svn.apache.org/viewvc?rev=1033485&view=rev
Log:
DERBY-4895 Thread local TableDescriptor#referencedColumnMap is not removed from app threads
when Derby is shut down

Patch derby-4895-a plus a comment as Knut suggested. The patch moves
the storage of a table's referencedColumnMap to the lcc to avoid a
lingering thread local in application threads after Derby has shut
down (seen as potential memory leak error in TomCat deployments).


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/dictionary/TableDescriptor.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java?rev=1033485&r1=1033484&r2=1033485&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java
Wed Nov 10 14:22:56 2010
@@ -22,6 +22,7 @@
 package org.apache.derby.iapi.sql.conn;
 
 import org.apache.derby.iapi.services.context.Context;
+import org.apache.derby.iapi.services.io.FormatableBitSet;
 import org.apache.derby.iapi.db.Database;
 import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.iapi.sql.compile.CompilerContext;
@@ -1290,4 +1291,20 @@ public interface LanguageConnectionConte
      * @return saved exception
      */
     public StandardException getInterruptedException();
+
+    /**
+     * Get the referenced column map for a table
+     *
+     * @return the map
+     */
+    public FormatableBitSet getReferencedColumnMap(TableDescriptor td);
+
+    /**
+     * Set the referenced column map for a table
+     *
+     * @param td the table descriptor
+     * @param map the map
+     */
+    public void setReferencedColumnMap(TableDescriptor td,
+                                       FormatableBitSet map);
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/dictionary/TableDescriptor.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/dictionary/TableDescriptor.java?rev=1033485&r1=1033484&r2=1033485&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/dictionary/TableDescriptor.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/dictionary/TableDescriptor.java
Wed Nov 10 14:22:56 2010
@@ -24,7 +24,7 @@ package org.apache.derby.iapi.sql.dictio
 import java.util.Enumeration;
 import java.util.Iterator;
 import java.util.List;
-import java.util.WeakHashMap;
+import java.util.Map;
 
 import org.apache.derby.catalog.Dependable;
 import org.apache.derby.catalog.DependableFinder;
@@ -34,6 +34,7 @@ import org.apache.derby.iapi.reference.S
 import org.apache.derby.iapi.services.io.FormatableBitSet;
 import org.apache.derby.iapi.services.io.StoredFormatIds;
 import org.apache.derby.iapi.services.sanity.SanityManager;
+import org.apache.derby.iapi.services.context.ContextService;
 import org.apache.derby.iapi.sql.StatementType;
 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
 import org.apache.derby.iapi.sql.depend.DependencyManager;
@@ -113,43 +114,44 @@ public class TableDescriptor extends Tup
 	UUID							oid;
 	int								tableType;
 	long							heapConglomNumber = -1;
-	ColumnDescriptorList		columnDescriptorList;
+    ColumnDescriptorList            columnDescriptorList;
 	ConglomerateDescriptorList		conglomerateDescriptorList;
 	ConstraintDescriptorList		constraintDescriptorList;
 	private	GenericDescriptorList	triggerDescriptorList;
 	ViewDescriptor					viewDescriptor;
 
-	/**
-	 * referencedColumnMap is thread local (since DERBY-2861)
-	 *
-	 * It contains a weak hash map keyed by the the TableDescriptor
-	 * and the value is the actual referencedColumnMap bitmap.  So,
-	 * each thread has a weak hash map it uses to find the appropriate
-	 * referencedColumnMap for 'this' TableDescriptor.
-	 *
-	 * Since the hash map is weak, when the TableDescriptor is no
-	 * longer referenced the hash entry can be garbage collected (it
-	 * is the *key* of a weak hash map that is weak, not the value).
-	 */
-	private static ThreadLocal referencedColumnMap = new ThreadLocal() {
-			protected Object initialValue() {
-				// Key: TableDescriptor
-				// Value: FormatableBitSet
-				return new WeakHashMap();
-			}
-		};
-
 	private FormatableBitSet referencedColumnMapGet() {
-		WeakHashMap map = (WeakHashMap)(referencedColumnMap.get());
 
-		return (FormatableBitSet) (map.get(this));
+        LanguageConnectionContext lcc =
+            (LanguageConnectionContext)ContextService.getContextOrNull(
+                LanguageConnectionContext.CONTEXT_ID);
+
+        if (SanityManager.DEBUG) {
+            SanityManager.ASSERT(lcc != null);
+        }
+
+        return lcc.getReferencedColumnMap(this);
+
 	}
 
 	private void referencedColumnMapPut
 		(FormatableBitSet newReferencedColumnMap) {
 
-		WeakHashMap map = (WeakHashMap)(referencedColumnMap.get());
-		map.put(this, newReferencedColumnMap);
+        LanguageConnectionContext lcc =
+            (LanguageConnectionContext)ContextService.getContextOrNull(
+                LanguageConnectionContext.CONTEXT_ID);
+
+        if (SanityManager.DEBUG) {
+            SanityManager.ASSERT(lcc != null || newReferencedColumnMap == null);
+        }
+
+        // This method is called with a null argument at database
+        // creation time when there is no lcc, cf stack trace in the
+        // JIRA for DERBY-4895, we can safely ignore that, as there
+        // exists no referencedColumnMap yet.
+        if (lcc != null) {
+            lcc.setReferencedColumnMap(this, newReferencedColumnMap);
+        }
 	}
 
 	/** A list of statistics pertaining to this table-- 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java?rev=1033485&r1=1033484&r2=1033485&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java
Wed Nov 10 14:22:56 2010
@@ -37,6 +37,7 @@ import org.apache.derby.iapi.services.sa
 import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
 import org.apache.derby.iapi.services.loader.GeneratedClass;
 import org.apache.derby.iapi.services.cache.Cacheable;
+import org.apache.derby.iapi.services.io.FormatableBitSet;
 import org.apache.derby.iapi.db.Database;
 import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.iapi.sql.compile.CompilerContext;
@@ -85,6 +86,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.AbstractMap;
 import java.util.IdentityHashMap;
+import java.util.WeakHashMap;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -302,7 +304,14 @@ public class GenericLanguageConnectionCo
      * null if no interrupt has been seen.
      */
     private StandardException interruptedException;
-    
+
+    /**
+     * Connection local state for cached {@code TableDescriptor}s used
+     * for keeping track of referenced columns for a table during DDL
+     * operations.
+     */
+    private WeakHashMap referencedColumnMap;
+
     /*
        constructor
     */
@@ -390,6 +399,7 @@ public class GenericLanguageConnectionCo
 
         setDefaultSchema(initDefaultSchemaDescriptor());
         interruptedException = null;
+        referencedColumnMap = new WeakHashMap();
     }
 
     /**
@@ -811,6 +821,8 @@ public class GenericLanguageConnectionCo
 
         // Reset the current user
         getCurrentSQLSessionContext().setUser(getSessionUserId());
+
+        referencedColumnMap = new WeakHashMap();
     }
 
     // debug methods
@@ -3875,4 +3887,13 @@ public class GenericLanguageConnectionCo
     public StandardException getInterruptedException() {
         return interruptedException;
     }
+
+    public FormatableBitSet getReferencedColumnMap(TableDescriptor td) {
+        return (FormatableBitSet)referencedColumnMap.get(td);
+    }
+
+    public void setReferencedColumnMap(TableDescriptor td,
+                                       FormatableBitSet map) {
+        referencedColumnMap.put(td, map);
+    }
 }



Mime
View raw message