openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From awh...@apache.org
Subject svn commit: r492790 - in /incubator/openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachedStateManager.java openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestAttachWithNoChanges.java
Date Thu, 04 Jan 2007 22:44:20 GMT
Author: awhite
Date: Thu Jan  4 14:44:19 2007
New Revision: 492790

URL: http://svn.apache.org/viewvc?view=rev&rev=492790
Log:
Force a version check when merging an unchanged detached entity to ensure that
we don't blindly use stale state.


Added:
    incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestAttachWithNoChanges.java
  (with props)
Modified:
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachedStateManager.java

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachedStateManager.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachedStateManager.java?view=diff&rev=492790&r1=492789&r2=492790
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachedStateManager.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachedStateManager.java
Thu Jan  4 14:44:19 2007
@@ -110,9 +110,9 @@
         FieldMetaData[] fields = meta.getFields();
         int restore = broker.getRestoreState();
         if (_dirty.length() > 0) {
-            BitSet load = (BitSet) _dirty.clone();
+            BitSet load = new BitSet(fields.length);
             for (int i = 0; i < fields.length; i++) {
-                if (!load.get(i))
+                if (!_dirty.get(i))
                     continue;
 
                 switch (fields[i].getDeclaredTypeCode()) {
@@ -140,7 +140,8 @@
             }
             FetchConfiguration fc = broker.getFetchConfiguration();
             sm.loadFields(load, fc, fc.getWriteLockLevel(), null, true);
-        }
+        }        
+        Object origVersion = sm.getVersion();
         sm.setVersion(_version);
 
         BitSet loaded = sm.getLoaded();
@@ -269,6 +270,17 @@
             }
         }
         pc.pcReplaceStateManager(sm);
+
+        // if we were clean at least make sure a version check is done to
+        // prevent using old state
+        if (!sm.isVersionCheckRequired() && broker.isActive()
+            && _version != origVersion && (origVersion == null 
+            || broker.getStoreManager().compareVersion(sm, _version, 
+            origVersion) != StoreManager.VERSION_SAME)) {
+            broker.transactional(sm.getManagedInstance(), false, 
+                manager.getBehavior());
+        }
+
         return sm.getManagedInstance();
     }
 

Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestAttachWithNoChanges.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestAttachWithNoChanges.java?view=auto&rev=492790
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestAttachWithNoChanges.java
(added)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestAttachWithNoChanges.java
Thu Jan  4 14:44:19 2007
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.detachment;
+
+import java.util.HashMap;
+import java.util.Map;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.OptimisticLockException;
+import javax.persistence.Persistence;
+
+import junit.framework.TestCase;
+import junit.textui.TestRunner;
+
+/**
+ * Test that attaching an instance without having changed it still overwrites
+ * any changes to the managed copy.
+ *
+ * @author Abe White
+ */
+public class TestAttachWithNoChanges
+    extends TestCase {
+
+    private EntityManagerFactory emf;
+
+    public void setUp() {
+        String types = DetachmentOneManyParent.class.getName() + ";"
+            + DetachmentOneManyChild.class.getName(); 
+        Map props = new HashMap();
+        props.put("openjpa.MetaDataFactory", "jpa(Types=" + types + ")");
+        emf = Persistence.createEntityManagerFactory("test", props);
+    }
+
+    public void tearDown() {
+        if (emf == null)
+            return;
+        try {
+            EntityManager em = emf.createEntityManager();
+            em.getTransaction().begin();
+            em.createQuery("delete from DetachmentOneManyChild").
+                executeUpdate();
+            em.createQuery("delete from DetachmentOneManyParent").
+                executeUpdate();
+            em.getTransaction().commit();
+            em.close();
+            emf.close();
+        } catch (Exception e) {
+        }
+    }
+    
+    public void testAttachWithNoChangesChecksVersion() {
+try {
+        DetachmentOneManyChild e = new DetachmentOneManyChild();
+        DetachmentOneManyParent p = new DetachmentOneManyParent();
+        e.setName("orig");
+        p.addChild(e);
+
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        em.persist(p);
+        em.persist(e);
+        em.flush();
+        em.clear();
+        
+        DetachmentOneManyChild changed = em.find(DetachmentOneManyChild.class,
+            e.getId()); 
+        changed.setName("newname");
+        em.flush();
+
+        em.merge(e);
+        try {
+            em.flush();
+            fail("Should not be able to flush old version over new.");
+        } catch (OptimisticLockException ole) {
+            // expected
+        } finally {
+            if (em.getTransaction().isActive())
+                em.getTransaction().rollback();
+            em.close();
+        }
+} catch (RuntimeException re) {
+re.printStackTrace();
+throw re;
+}
+    }
+
+    public static void main(String[] args) {
+        TestRunner.run(TestAttachWithNoChanges.class);
+    }
+}
+

Propchange: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestAttachWithNoChanges.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestAttachWithNoChanges.java
------------------------------------------------------------------------------
    svn:executable = *



Mime
View raw message