openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r1140315 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/
Date Mon, 27 Jun 2011 20:59:02 GMT
Author: mikedd
Date: Mon Jun 27 20:59:01 2011
New Revision: 1140315

URL: http://svn.apache.org/viewvc?rev=1140315&view=rev
Log:
OPENJPA-1768: Check whether the broker is already loading before recursively loading an entity
from datacache

Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PCDataImpl.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestM2MInDataCache.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java?rev=1140315&r1=1140314&r2=1140315&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java Mon
Jun 27 20:59:01 2011
@@ -1167,6 +1167,13 @@ public class BrokerImpl
         }
     }
 
+    public boolean isLoading(Object o) { 
+        if(_loading == null ) {
+            return false; 
+        }
+        return _loading.containsKey(o); 
+    }
+
     private boolean hasFlushed() {
         return (_flags & FLAG_FLUSHED) != 0;
     }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PCDataImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PCDataImpl.java?rev=1140315&r1=1140314&r2=1140315&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PCDataImpl.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PCDataImpl.java Mon
Jun 27 20:59:01 2011
@@ -19,6 +19,7 @@
 package org.apache.openjpa.kernel;
 
 import java.util.BitSet;
+import java.util.Map;
 
 import org.apache.openjpa.datacache.DataCache;
 import org.apache.openjpa.meta.ClassMetaData;
@@ -168,8 +169,13 @@ public class PCDataImpl
                 continue;
 
             fmd = sm.getMetaData().getField(i);
-            if (!isLoaded(i))
+            boolean loading = false; 
+            if(sm.getContext() != null && sm.getContext() instanceof BrokerImpl)
{ 
+                loading = ((BrokerImpl) sm.getContext()).isLoading(sm.getObjectId());
+            }
+            if (!isLoaded(i) || loading) { // prevent reentrant calls. 
                 loadIntermediate(sm, fmd);
+            }
             else {
                 loadField(sm, fmd, fetch, context);
                 loadImplData(sm, fmd);

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestM2MInDataCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestM2MInDataCache.java?rev=1140315&r1=1140314&r2=1140315&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestM2MInDataCache.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestM2MInDataCache.java
Mon Jun 27 20:59:01 2011
@@ -18,29 +18,22 @@
  */
 package org.apache.openjpa.persistence.datacache;
 
-import java.util.Map;
-
 import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAPersistence;
 import org.apache.openjpa.persistence.datacache.common.apps.M2MEntityE;
 import org.apache.openjpa.persistence.datacache.common.apps.M2MEntityF;
 import org.apache.openjpa.persistence.test.SingleEMFTestCase;
 
 public class TestM2MInDataCache extends SingleEMFTestCase {
     public void setUp() {
-        setUp("openjpa.DataCache", "true", 
-            "openjpa.RemoteCommitProvider", "sjvm", 
-            M2MEntityE.class,
-            M2MEntityF.class, CLEAR_TABLES);
+        setUp("openjpa.DataCache", "true", "openjpa.RemoteCommitProvider", "sjvm", M2MEntityE.class,
M2MEntityF.class,
+            CLEAR_TABLES);
     }
 
-    /**
-     * Test if child list is in order after new child list is added in setup().
-     *
-     */
-    public void testM2MDataCache(){
-    	EntityManager em = emf.createEntityManager();
+    public void populate(EntityManager em) {
         em.getTransaction().begin();
-
         M2MEntityE e1 = new M2MEntityE();
         e1.setId(1);
         e1.setName("ABC");
@@ -49,39 +42,83 @@ public class TestM2MInDataCache extends 
         e2.setId(2);
         e2.setName("DEF");
         em.persist(e2);
-        
+
         M2MEntityF f1 = new M2MEntityF();
         f1.setId(10);
         em.persist(f1);
         M2MEntityF f2 = new M2MEntityF();
         f2.setId(20);
         em.persist(f2);
-        
+
         e1.getEntityF().put(f1.getId(), f1);
         e1.getEntityF().put(f2.getId(), f2);
         e2.getEntityF().put(f1.getId(), f1);
         e2.getEntityF().put(f2.getId(), f2);
-        
+
         f1.getEntityE().put(e1.getName(), e1);
         f1.getEntityE().put(e2.getName(), e2);
         f2.getEntityE().put(e1.getName(), e1);
         f2.getEntityE().put(e2.getName(), e2);
         em.getTransaction().commit();
+    }
+
+    public void validateGraph(M2MEntityE e1, M2MEntityE e2, M2MEntityF f1, M2MEntityF f2)
{
+        assertNotNull(e1);
+        assertNotNull(e2);
+        assertNotNull(f1);
+        assertNotNull(f2);
+
+        assertEquals(f1, e1.getEntityF().get(f1.getId()));
+        assertEquals(f2, e1.getEntityF().get(f2.getId()));
+        assertEquals(f1, e2.getEntityF().get(f1.getId()));
+        assertEquals(f2, e2.getEntityF().get(f2.getId()));
+
+        assertEquals(e1, f1.getEntityE().get(e1.getName()));
+        assertEquals(e2, f1.getEntityE().get(e2.getName()));
+        assertEquals(e1, f2.getEntityE().get(e1.getName()));
+        assertEquals(e2, f2.getEntityE().get(e2.getName()));
+    }
+
+    /**
+     * Test if child list is in order after new child list is added in setup().
+     * 
+     */
+    public void testM2MDataCache() {
+        EntityManager em = emf.createEntityManager();
+        populate(em);
         em.close();
 
         em = emf.createEntityManager();
-        try {
-            M2MEntityE e1a = em.find(M2MEntityE.class, 1);
-            Map entityf1 = e1a.getEntityF();
-            M2MEntityE e2a = em.find(M2MEntityE.class, 2);
-            Map entityf2 = e2a.getEntityF();
-            M2MEntityF f1a = em.find(M2MEntityF.class, 10);
-            Map entitye1 = f1a.getEntityE();
-            M2MEntityF f2a = em.find(M2MEntityF.class, 20);
-            Map entitye2 = f2a.getEntityE();
-        } catch (Exception e) {
-            fail("Fail to get a Map field when DataCache is on");
-        }
+        M2MEntityE e1a = em.find(M2MEntityE.class, 1);
+        assertNotNull(e1a.getEntityF());
+        M2MEntityE e2a = em.find(M2MEntityE.class, 2);
+        assertNotNull(e2a.getEntityF());
+        M2MEntityF f1a = em.find(M2MEntityF.class, 10);
+        assertNotNull(f1a.getEntityE());
+        M2MEntityF f2a = em.find(M2MEntityF.class, 20);
+        assertNotNull(f2a.getEntityE());
         em.close();
     }
+
+    public void testEagerFetch() {
+        EntityManager em = emf.createEntityManager();
+        populate(em);
+        em.close();
+
+        em = emf.createEntityManager();
+
+        OpenJPAEntityManager ojEm = OpenJPAPersistence.cast(em);
+        ojEm.getFetchPlan().addField(M2MEntityE.class, "entityf");
+        ojEm.getFetchPlan().addField(M2MEntityF.class, "entitye");
+
+        M2MEntityE e1, e2;
+        M2MEntityF f1, f2;
+
+        e1 = em.find(M2MEntityE.class, 1);
+        e2 = em.find(M2MEntityE.class, 2);
+        f1 = em.find(M2MEntityF.class, 10);
+        f2 = em.find(M2MEntityF.class, 20);
+
+        validateGraph(e1, e2, f1, f2);
+    }
 }



Mime
View raw message