From 024ff20a8c5c5ec8494cb65a175a69687d83259c Mon Sep 17 00:00:00 2001
From: Pinaki Poddar <ppoddar@apache.org>
Date: Wed, 18 Jan 2012 19:58:59 +0000
Subject: [PATCH] OPENJPA-2099: Guard cached select

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1233026 13f79535-47bb-0310-9956-ffa450edef68
---
 .../strats/StoreCollectionFieldStrategy.java  | 103 +++++++++---------
 .../apache/openjpa/jdbc/sql/SelectImpl.java   |  22 ++--
 2 files changed, 63 insertions(+), 62 deletions(-)

diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java
index 56f471ec3..9158900cd 100644
--- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java
+++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java
@@ -525,57 +525,58 @@ public abstract class StoreCollectionFieldStrategy
             return;
         }
         // select data for this state manager
-        final ClassMapping[] elems = getIndependentElementMappings(true);
-        final Joins[] resJoins = new Joins[Math.max(1, elems.length)];
-        Union union;
-        if (_executor == null) {
-        	union = store.getSQLFactory().newUnion(Math.max(1, elems.length));
-        	if (store.getConfiguration().getSelectCacheEnabled()) {
-        		_executor = union;
-        	}
-        } else {
-        	union = (Union)_executor;
-        }
-        union.select(new Union.Selector() {
-            public void select(Select sel, int idx) {
-                ClassMapping elem = (elems.length == 0) ? null : elems[idx];
-                resJoins[idx] = selectAll(sel, elem, sm, store, fetch, JDBCFetchConfiguration.EAGER_PARALLEL);
-            }
-        });
-
-        // create proxy
-        Object coll;
-        ChangeTracker ct = null;
-        if (field.getTypeCode() == JavaTypes.ARRAY)
-            coll = new ArrayList();
-        else {
-            coll = sm.newProxy(field.getIndex());
-            if (coll instanceof Proxy)
-                ct = ((Proxy) coll).getChangeTracker();
-        }
-
-        // load values
-        Result res = union.execute(store, fetch);
-        try {
-            int seq = -1;
-            while (res.next()) {
-                if (ct != null && field.getOrderColumn() != null)
-                    seq = res.getInt(field.getOrderColumn());
-                setMappedBy(sm.getObjectId(), sm, coll, res);
-               	add(store, coll, loadElement(sm, store, fetch, res, resJoins[res.indexOf()]));
-            }
-            if (ct != null && field.getOrderColumn() != null)
-                ct.setNextSequence(seq + 1);
-        } finally {
-            res.close();
-        }
-
-        // set into sm
-        if (field.getTypeCode() == JavaTypes.ARRAY) {
-            sm.storeObject(field.getIndex(), JavaTypes.toArray
-                ((Collection) coll, field.getElement().getType()));
-        } else {
-            sm.storeObject(field.getIndex(), coll);
+        synchronized (this) {
+	        final ClassMapping[] elems = getIndependentElementMappings(true);
+	        final Joins[] resJoins = new Joins[Math.max(1, elems.length)];
+	        Union union;
+	        if (_executor == null) {
+	        	union = store.getSQLFactory().newUnion(Math.max(1, elems.length));
+	        	if (store.getConfiguration().getSelectCacheEnabled()) {
+	        		_executor = union;
+	        	}
+	        } else {
+	        	union = (Union)_executor;
+	        }
+	        union.select(new Union.Selector() {
+	            public void select(Select sel, int idx) {
+	                ClassMapping elem = (elems.length == 0) ? null : elems[idx];
+	                resJoins[idx] = selectAll(sel, elem, sm, store, fetch, JDBCFetchConfiguration.EAGER_PARALLEL);
+	            }
+	        });
+	
+	        // create proxy
+	        Object coll;
+	        ChangeTracker ct = null;
+	        if (field.getTypeCode() == JavaTypes.ARRAY)
+	            coll = new ArrayList();
+	        else {
+	            coll = sm.newProxy(field.getIndex());
+	            if (coll instanceof Proxy)
+	                ct = ((Proxy) coll).getChangeTracker();
+	        }
+	
+	        // load values
+	        Result res = union.execute(store, fetch);
+	        try {
+	            int seq = -1;
+	            boolean ordered = ct != null && field.getOrderColumn() != null;
+	            while (res.next()) {
+	                if (ordered) seq = res.getInt(field.getOrderColumn());
+	                setMappedBy(sm.getObjectId(), sm, coll, res);
+	               	add(store, coll, loadElement(sm, store, fetch, res, resJoins[res.indexOf()]));
+	            }
+	            if (ordered) ct.setNextSequence(seq + 1);
+	        } finally {
+	            res.close();
+	        }
+	
+	        // set into sm
+	        if (field.getTypeCode() == JavaTypes.ARRAY) {
+	            sm.storeObject(field.getIndex(), 
+	            		JavaTypes.toArray((Collection<?>) coll, field.getElement().getType()));
+	        } else {
+	            sm.storeObject(field.getIndex(), coll);
+	        }
         }
     }
 
diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
index 605fe9eae..7c073b3a6 100644
--- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
+++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
@@ -1404,15 +1404,15 @@ public class SelectImpl
             return;
         }
 
+        Object[] params = getBindParameter(oid, mapping, toCols, fromCols, pj, store);
 
         SQLBuffer buf = isReadOnly() ? _where : new SQLBuffer(_dict);
-        Object[] params = getBindParameter(oid, mapping, toCols, fromCols, pj, store);
         bindParameter(buf, params, fromCols, pj);
         
         if (constCols != null && constCols.length > 0) {
             bindParameter(buf, vals, constCols, pj);
         }
-        if (buf != _where) {
+        if (!isReadOnly()) {
         	where(buf, pj);
         }
     }
@@ -1498,22 +1498,22 @@ public class SelectImpl
     }
 
     public void where(Joins joins) {
+    	assertMutable();
         if (joins != null)
-            where((SQLBuffer) null, joins);
-    }
-
-    public void where(SQLBuffer sql) {
-        where(sql, (Joins) null);
+            getJoins(joins, true);
     }
 
     public void where(SQLBuffer sql, Joins joins) {
-        where(sql, getJoins(joins, true));
+    	assertMutable();
+    	where(joins);
+        where(sql);
     }
 
     /**
      * Add the given condition to the WHERE clause.
      */
-    private void where(SQLBuffer sql, PathJoins pj) {
+    public void where(SQLBuffer sql) {
+    	assertMutable();
         // no need to use joins...
         if (sql == null || sql.isEmpty())
             return;
@@ -3132,9 +3132,9 @@ public class SelectImpl
             }
 
             int idx;
-            if (_aliases.put(id, alias) != null)
+            if (_aliases.put(id, alias) != null) {
                 idx = _ids.indexOf(id);
-            else {
+            } else {
                 _ids.add(id);
                 idx = _ids.size() - 1;