Index: java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
===================================================================
--- java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java	(revision 644444)
+++ java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java	(working copy)
@@ -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);        
     }
 
 
@@ -1000,6 +1000,8 @@
      * @return The locator identifying this lob.
      */
     public int getLocator() {
+        if (locator == 0)
+            locator = localConn.addLOBMapping (this);
         return locator;
     }
 }
Index: java/engine/org/apache/derby/impl/jdbc/EmbedClob.java
===================================================================
--- java/engine/org/apache/derby/impl/jdbc/EmbedClob.java	(revision 644444)
+++ java/engine/org/apache/derby/impl/jdbc/EmbedClob.java	(working copy)
@@ -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,8 @@
      * @return locator value for this Clob.
      */
     public int getLocator() {
+        if (locator == 0)
+            locator = localConn.addLOBMapping(this);
         return locator;
     }
 }
Index: java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java
===================================================================
--- java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java	(revision 644444)
+++ java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java	(working copy)
@@ -21,6 +21,8 @@
 
 package org.apache.derby.impl.jdbc;
 
+import java.util.Map;
+import java.util.WeakHashMap;
 import org.apache.derby.iapi.error.ExceptionSeverity;
 import org.apache.derby.jdbc.InternalDriver;
 
@@ -130,6 +132,10 @@
 
 	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 more valid or when connection is closed
+    private WeakHashMap lobRefrences = null;
 
 	//////////////////////////////////////////////////////////
 	// STATE (copied to new nested connections, but nesting
@@ -2901,14 +2907,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.lobRefrences;
 		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();
+        }
 	}
 
 	/**
@@ -2939,6 +2948,18 @@
                 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.lobRefrences == null) {
+            rootConnection.lobRefrences = new WeakHashMap ();
+        }
+        rootConnection.lobRefrences.put(lobReference, null);
+    }
+    
 	/**
 	* Return the Hash Map in the root connection
 	* @return the HashMap that contains the locator to LOB object mapping
