You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by cu...@apache.org on 2012/10/26 21:49:06 UTC

svn commit: r1402635 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/

Author: curtisr7
Date: Fri Oct 26 19:49:06 2012
New Revision: 1402635

URL: http://svn.apache.org/viewvc?rev=1402635&view=rev
Log:
OPENJPA-2285: DataCacheStoreManager improvement.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheStoreLazyFk.java   (with props)
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestStatistics.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java?rev=1402635&r1=1402634&r2=1402635&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java Fri Oct 26 19:49:06 2012
@@ -49,8 +49,7 @@ import org.apache.openjpa.util.Optimisti
  * @author Patrick Linskey
  * @nojavadoc
  */
-public class DataCacheStoreManager
-    extends DelegatingStoreManager {
+public class DataCacheStoreManager extends DelegatingStoreManager {
 
     // all the state managers changed in this transaction
     private Collection<OpenJPAStateManager> _inserts = null;
@@ -438,9 +437,12 @@ public class DataCacheStoreManager
         DataCache cache = _mgr.selectCache(sm);
 
         boolean found = false;
+        int loadedFieldsBefore = sm.getLoaded().cardinality();
         if (cache == null || sm.isEmbedded() || bypass(fetch, StoreManager.FORCE_LOAD_NONE)) {
             found = super.load(sm, fields, fetch, lockLevel, edata);
-            updateDataCache(found, sm, fetch);
+            int loadedFieldsAfter = sm.getLoaded().cardinality();
+            boolean changed = loadedFieldsAfter > loadedFieldsBefore;
+            updateDataCache(found, sm, fetch, changed);
             return found;
         }
 
@@ -458,10 +460,12 @@ public class DataCacheStoreManager
 
         // load from store manager; clone the set of still-unloaded fields
         // so that if the store manager decides to modify it it won't affect us
-        found = super.load(sm, (BitSet) fields.clone(), fetch, lockLevel, edata);
+        found = super.load(sm,(BitSet) fields.clone() , fetch, lockLevel, edata);
 
+        int loadedFieldsAfter = sm.getLoaded().cardinality();
+        boolean changed = loadedFieldsAfter > loadedFieldsBefore;
         // Get new instance of cache after DB load since it may have changed
-        updateDataCache(found, sm, fetch);
+        updateDataCache(found, sm, fetch, changed);
 
         return found;
     }
@@ -473,8 +477,10 @@ public class DataCacheStoreManager
      * @param found whether the entity was found by the store manager
      * @param sm the state manager
      * @param fetch fetch configuration
+     * @param loadedFieldsChanged
      */
-    private void updateDataCache(boolean found, OpenJPAStateManager sm, FetchConfiguration fetch) {
+    private void updateDataCache(boolean found, OpenJPAStateManager sm, FetchConfiguration fetch,
+        boolean loadedFieldsChanged) {
 
         if (!_ctx.getPopulateDataCache() || sm == null || fetch.getCacheStoreMode() == DataCacheStoreMode.BYPASS) {
             return;
@@ -486,10 +492,12 @@ public class DataCacheStoreManager
         }
 
         DataCachePCData data = cache.get(sm.getObjectId());
-        boolean alreadyCached = data != null;
-
-        if ((fetch.getCacheStoreMode() == DataCacheStoreMode.USE && !alreadyCached) ||
-             fetch.getCacheStoreMode() == DataCacheStoreMode.REFRESH) {
+        
+        // If loadedFieldsChanged = true, we don't care that data was already stored as we should update it.
+        boolean alreadyCached = (data != null && !loadedFieldsChanged);
+        DataCacheStoreMode storeMode = fetch.getCacheStoreMode();
+        
+        if ((storeMode == DataCacheStoreMode.USE && !alreadyCached) || storeMode == DataCacheStoreMode.REFRESH) {
             // If not found in the DB and the item is in the cache, and not locking remove the item
             if (!found && data != null && !isLocking(fetch)) {
                 cache.remove(sm.getObjectId());

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheStoreLazyFk.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheStoreLazyFk.java?rev=1402635&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheStoreLazyFk.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheStoreLazyFk.java Fri Oct 26 19:49:06 2012
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.datacache;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.openjpa.meta.ClassMetaData;
+import org.apache.openjpa.persistence.EntityManagerImpl;
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
+
+public class TestDataCacheStoreLazyFk extends SQLListenerTestCase {
+    Object[] p = new Object[] { CLEAR_TABLES, CachedEntityStatistics.class, "openjpa.DataCache", "true" };
+
+    public void setUp() {
+        super.setUp(p);
+    }
+
+    public void testCacheHit() throws Exception {
+        EntityManagerImpl em = (EntityManagerImpl) emf.createEntityManager();
+        // Change the eager field to lazy to make testing easier.
+        ClassMetaData cmd =
+            em.getConfiguration().getMetaDataRepositoryInstance().getMetaData(CachedEntityStatistics.class, null, true);
+        cmd.getField("eagerList").setInDefaultFetchGroup(false);
+        try {
+            em.getTransaction().begin();
+            CachedEntityStatistics e = new CachedEntityStatistics();
+            CachedEntityStatistics lazy = new CachedEntityStatistics();
+            Set<CachedEntityStatistics> lazyList = new HashSet<CachedEntityStatistics>();
+            lazyList.add(lazy);
+            e.setLazyList(lazyList);
+            em.persist(e);
+            em.persist(lazy);
+            em.flush();
+            em.clear();
+
+            // Should prime the cache
+            em.find(CachedEntityStatistics.class, e.getId()).getLazyList();
+            em.clear();
+            sql.clear();
+
+            CachedEntityStatistics c = em.find(CachedEntityStatistics.class, e.getId());
+            c.getLazyList();
+            assertEquals(0, sql.size());
+
+            em.getTransaction().commit();
+        } finally {
+            if (em.getTransaction().isActive())
+                em.getTransaction().rollback();
+            em.close();
+        }
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheStoreLazyFk.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestStatistics.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestStatistics.java?rev=1402635&r1=1402634&r2=1402635&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestStatistics.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestStatistics.java Fri Oct 26 19:49:06 2012
@@ -115,6 +115,9 @@ public class TestStatistics extends Sing
         write++;
         write++;
         write++;
+        
+        write++;
+        write++;
         assertion(cls, hit,  read, write, stats);
 
         em.clear();
@@ -152,6 +155,8 @@ public class TestStatistics extends Sing
         assertEquals(1, p.getLazyList().size());
         read++;
         write++;
+        // +1 write for p which had its intermediate(fk) updated
+        write++;
         assertion(cls, hit,  read, write, stats);
         em.clear();