db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oyste...@apache.org
Subject svn commit: r644764 - in /db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc: EmbedBlob.java EmbedClob.java EmbedConnection.java
Date Fri, 04 Apr 2008 16:40:30 GMT
Author: oysteing
Date: Fri Apr  4 09:40:25 2008
New Revision: 644764

URL: http://svn.apache.org/viewvc?rev=644764&view=rev
Log:
DERBY-3354:  Select from large lob table with embedded gives OutOfMemoryError
Contributed by Anurag.

This patch introduces a new WeakHashMap in EmbedConnection. EmbedBlob and EmbedClob objects
references are stored in this
map (objects as key and null as value). Adding entry to locater map is
differed till the first call of getLocater.
This ensures that there is entry of LOB objects in locater map if they are invoked in embedded
mode.
As the keys of WeakHashMap doesn't prevents the objects from being
garbage collected, once the lob objects are unreferenced lob objects will
be garbage collected releasing the memory.

During commit/rollback or Connection.close, free is invoked on all the lob
objects from WeakHashMap and the map is cleared.

Modified files
java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java
Added a new attribute lobRefrences of type WeakHashMap.
Added a new method addLOBReference to make an entry in new
 hash map.
Modified clearLOBMapping to use lobRefrences to fetch and invoke free on lob objects instead
of lobHashMap.

java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
java/engine/org/apache/derby/impl/jdbc/EmbedClob.java

Modified constructs to call connection.lobRefrences instead of conn.addLOBMapping.
Modified getLocater method to check if the locater value is non zero
before returning and if its zero calling conn.addLOBMapping to make
entry of lob objects and getting locater value.
Calling removeLOBMapping in free method.


Cleanup of temporary file is already being taken care by the finalizer of
LOBStreamControl so I haven't added any new cleanup code for
finalizer.


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java?rev=644764&r1=644763&r2=644764&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java Fri Apr  4 09:40:25
2008
@@ -75,7 +75,7 @@
     private InputStream     myStream;
     
     // locator key for lob. used by Network Server.
-    private final int             locator;
+    private int             locator;
     
     /*
      * Length of the BLOB if known. Set to -1 if
@@ -117,7 +117,7 @@
              materialized = true;
              //add entry in connection so it can be cleared 
              //when transaction is not valid
-             locator = con.addLOBMapping (this);
+             con.addLOBReference (this);
          }
          catch (IOException e) {
              throw Util.setStreamFailure (e);
@@ -193,7 +193,7 @@
         pos = 0;
         //add entry in connection so it can be cleared 
         //when transaction is not valid
-        this.locator = con.addLOBMapping (this);
+        con.addLOBReference (this);
     }
 
 
@@ -906,6 +906,8 @@
         //valid
         isValid = false;
         
+        //remove entry from connection
+        localConn.removeLOBMapping(locator);
         //initialialize length to default value -1
         myLength = -1;
         
@@ -1000,6 +1002,9 @@
      * @return The locator identifying this lob.
      */
     public int getLocator() {
+        if (locator == 0) {
+            locator = localConn.addLOBMapping(this);
+        }
         return locator;
     }
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java?rev=644764&r1=644763&r2=644764&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java Fri Apr  4 09:40:25
2008
@@ -79,7 +79,7 @@
     /** Tells whether the Clob has been freed or not. */
     private boolean isValid = true;
 
-    private final int locator;
+    private int locator;
     
     /**
      * Creates an empty Clob object.
@@ -91,7 +91,7 @@
     EmbedClob(EmbedConnection con) throws SQLException {
         super(con);
         this.clob = new TemporaryClob (con.getDBName(), this);
-        this.locator = con.addLOBMapping (this);
+        con.addLOBReference (this);
     }
 
     /**
@@ -156,7 +156,7 @@
                 throw se;
             }
         }
-        this.locator = con.addLOBMapping (this);
+        con.addLOBReference (this);
     }
 
     /**
@@ -663,6 +663,7 @@
             } catch (IOException e) {
                 throw Util.setStreamFailure(e);
             } finally {
+                localConn.removeLOBMapping(locator);
                 this.clob = null;
             }
         }
@@ -791,6 +792,9 @@
      * @return locator value for this Clob.
      */
     public int getLocator() {
+        if (locator == 0) {
+            locator = localConn.addLOBMapping(this);
+        }
         return locator;
     }
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java?rev=644764&r1=644763&r2=644764&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java Fri Apr
 4 09:40:25 2008
@@ -65,6 +65,8 @@
 import java.sql.SQLWarning;
 import java.sql.Statement;
 
+import java.util.Map;
+import java.util.WeakHashMap;
 import java.util.HashMap;
 import java.util.Properties;
 import java.util.Iterator;
@@ -131,6 +133,13 @@
 	private HashMap lobHashMap = null;
 	private int lobHMKey = 0;
 
+    /**
+     * Map to keep track of all the lobs associated with this
+     * connection. These lobs will be cleared after the transaction
+     * is no longer valid or when connection is closed
+     */
+    private WeakHashMap lobReferences = null;
+
 	//////////////////////////////////////////////////////////
 	// STATE (copied to new nested connections, but nesting
 	// specific)
@@ -2901,14 +2910,17 @@
 		//free all the lob resources in the HashMap
 		//initialize the locator value to 0 and
 		//the hash table object to null.
-		HashMap map = rootConnection.lobHashMap;
+		Map map = rootConnection.lobReferences;
 		if (map != null) {
-            Iterator it = map.values().iterator();
+            Iterator it = map.keySet ().iterator ();
             while (it.hasNext()) {
                 ((EngineLOB)it.next()).free();
 			}
 			map.clear();
 		}
+        if (rootConnection.lobHashMap != null) {
+            rootConnection.lobHashMap.clear ();
+        }
 	}
 
 	/**
@@ -2938,6 +2950,18 @@
                     newKey = rootConnection.lobHMKey = 1;
                 return newKey;
 	}
+
+    /**
+     * Adds an entry of the lob in WeakHashMap. These entries are used
+     * for cleanup during commit/rollback or close.
+     * @param lobReference LOB Object
+     */
+    void addLOBReference (Object lobReference) {
+        if (rootConnection.lobReferences == null) {
+            rootConnection.lobReferences = new WeakHashMap ();
+        }
+        rootConnection.lobReferences.put (lobReference, null);
+    }
 
 	/**
 	* Return the Hash Map in the root connection



Mime
View raw message