openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r654227 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/conf/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/ openjpa-project/src/doc...
Date Wed, 07 May 2008 19:34:28 GMT
Author: mikedd
Date: Wed May  7 12:34:26 2008
New Revision: 654227

URL: http://svn.apache.org/viewvc?rev=654227&view=rev
Log:
OPENJPA-591

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestFlushBeforeDetach.java
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachManager.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/Item.java
    openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_remote.xml

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java?rev=654227&r1=654226&r2=654227&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
Wed May  7 12:34:26 2008
@@ -55,6 +55,7 @@
     private boolean _nonOptimisticVersionCheck = false;
     private int _jpql = JPQL_STRICT;
     private boolean _storeMapCollectionInEntityAsBlob = false;
+    private boolean _flushBeforeDetach = true; 
 
     /**
      * Whether to require exact identity value types when creating object
@@ -237,4 +238,35 @@
     public void setStoreMapCollectionInEntityAsBlob(boolean storeAsBlob) {
         _storeMapCollectionInEntityAsBlob = storeAsBlob;
     }
+    
+    /**
+     * Whether OpenJPA should flush changes before detaching or serializing an
+     * entity. In JPA this is usually false, but other persistence frameworks
+     * (ie JDO) may expect it to be true.
+     * <P>Prior to version 1.0.3 and 1.2.0 changes were always flushed.
+     * 
+     * @since 1.0.3
+     * @since 1.2.0
+     * @return true if changes should be flushed, otherwise false.
+     */
+    public boolean getFlushBeforeDetach() {
+        return _flushBeforeDetach;
+    }
+
+    /**
+     * Whether OpenJPA should flush changes before detaching or serializing an
+     * entity. In JPA this is usually false, but other persistence frameworks
+     * (ie JDO) may expect it to be true.
+     * <P>Prior to version 1.0.3 and 1.2.0 changes were always flushed.
+     * 
+     * @param beforeDetach if true changes will be flushed before detaching or 
+     * serializing an entity.
+     * 
+     * @since 1.0.3
+     * @since 1.2.0
+     */
+    public void setFlushBeforeDetach(boolean beforeDetach) {
+        _flushBeforeDetach = beforeDetach;
+    }
+
 }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachManager.java?rev=654227&r1=654226&r2=654227&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachManager.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachManager.java
Wed May  7 12:34:26 2008
@@ -67,6 +67,7 @@
     private final OpCallbacks _call;
     private final boolean _failFast;
     private boolean _flushed = false;
+    private boolean _flushBeforeDetach;
 
     // if we're not detaching full, we need to track all detached objects;
     // if we are, then we use a special field manager for more efficient
@@ -82,7 +83,10 @@
         if (!sm.isPersistent())
             return false;
 
-        flushDirty(sm);
+        if (sm.getBroker().getConfiguration().getCompatibilityInstance()
+                .getFlushBeforeDetach()) {
+            flushDirty(sm);
+        }
 
         ClassMetaData meta = sm.getMetaData();
         boolean setState = meta.getDetachedState() != null
@@ -270,6 +274,9 @@
             _detached = new IdentityMap();
             _fullFM = null;
         }
+        _flushBeforeDetach =
+                broker.getConfiguration().getCompatibilityInstance()
+                        .getFlushBeforeDetach();
     }
 
     /**
@@ -415,8 +422,14 @@
         _broker.fireLifecycleEvent(toDetach, null, sm.getMetaData(),
             LifecycleEvent.BEFORE_DETACH);
 
-        // any dirty instances cause a flush to occur
-        _flushed = _flushed || flushDirty(sm);
+        if(! _flushed)  {
+            if(_flushBeforeDetach) {
+                // any dirty instances cause a flush to occur
+                flushDirty(sm);
+            }
+            _flushed = true;
+        }
+        
         BitSet fields = new BitSet();
         preDetach(_broker, sm, fields);
 

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/Item.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/Item.java?rev=654227&r1=654226&r2=654227&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/Item.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/Item.java
Wed May  7 12:34:26 2008
@@ -18,6 +18,8 @@
  */
 package org.apache.openjpa.persistence.simple;
 
+import java.io.Serializable;
+
 import javax.persistence.Basic;
 import javax.persistence.Column;
 import javax.persistence.Entity;
@@ -28,8 +30,10 @@
 
 @Entity
 @Table(name = "ITEM")
-public class Item {
+public class Item implements Serializable {
 
+    private static final long serialVersionUID = 489786296539819572L;
+    
     public int itemId;
     public String itemName;
     public java.math.BigDecimal itemPrice;

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestFlushBeforeDetach.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestFlushBeforeDetach.java?rev=654227&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestFlushBeforeDetach.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestFlushBeforeDetach.java
Wed May  7 12:34:26 2008
@@ -0,0 +1,144 @@
+/*
+ * 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.simple;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.OpenJPAPersistence;
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
+
+public class TestFlushBeforeDetach extends SQLListenerTestCase {
+
+    private int _id;
+    
+    public void setUp() {
+      setUp(Item.class,"openjpa.Compatibility", 
+                "default(flushBeforeDetach=false)");
+        persistSampleEntity();
+    }
+    
+    private void persistSampleEntity() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        Item i = new Item();
+        em.persist(i);
+        em.getTransaction().commit();
+        em.refresh(i);
+        _id = i.getItemId();
+        em.close();
+    }
+
+    public void testClear() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        Item i = em.find(Item.class, _id);
+
+        i.setItemData("ABCD");
+
+        em.clear();
+        em.getTransaction().rollback();
+        assertNotSQL("UPDATE ITEM.*");
+        em.close();
+    }
+
+    public void testDetach() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        Item i = em.find(Item.class, _id);
+
+        i.setItemData("EFGH");
+
+        OpenJPAPersistence.cast(em).detach(i);
+        em.getTransaction().rollback();
+        assertNotSQL("UPDATE ITEM SET.*");
+        em.close();
+    }
+    
+    public void testDetachAll() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        Item i = em.find(Item.class, _id);
+
+        i.setItemData("IJKL");
+
+        OpenJPAPersistence.cast(em).detachAll(i);
+        em.getTransaction().rollback();
+        assertNotSQL("UPDATE ITEM SET.*");
+        em.close();
+    }
+    
+    public void testDetachAllCollection() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        Item i = em.find(Item.class, _id);
+
+        i.setItemData("MNOP");
+
+        Collection<Item> c = new ArrayList<Item>();
+        c.add(i);
+        OpenJPAPersistence.cast(em).detachAll(c);
+        em.getTransaction().rollback();
+        assertNotSQL("UPDATE ITEM SET.*");
+        em.close();
+    }
+
+    public void testSerialize() throws Exception {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        Item i = em.find(Item.class, _id);
+
+        i.setItemData("QRSTU");
+
+        serializeObject(i);
+
+        em.getTransaction().rollback();
+        assertNotSQL("UPDATE ITEM SET.*");
+        em.close();
+    }
+
+    /**
+     * Helper to serialize an object to a byte[]
+     */
+    private Object serializeObject(Object orig) throws Exception {
+        Object deserialized = null;
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(baos);
+        oos.writeObject(orig);
+
+        ByteArrayInputStream bais =
+                new ByteArrayInputStream(baos.toByteArray());
+        ObjectInputStream ois = new ObjectInputStream(bais);
+
+        deserialized = ois.readObject();
+        return deserialized;
+    }
+}

Modified: openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_remote.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_remote.xml?rev=654227&r1=654226&r2=654227&view=diff
==============================================================================
--- openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_remote.xml (original)
+++ openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_remote.xml Wed May  7 12:34:26
2008
@@ -124,21 +124,43 @@
 concurrency exception.
             </para>
             <para>
-You can stop OpenJPA from assuming the transaction will commit by invoking
-<methodname>EntityTransaction.setRollbackOnly</methodname> prior to detaching
-your objects. Setting the <literal>RollbackOnly</literal> flag prevents OpenJPA
-from flushing when detaching dirty objects; instead OpenJPA just runs its
-pre-flush actions (see the <methodname>OpenJPAEntityManager.preFlush
-</methodname>
+You can stop OpenJPA from assuming the transaction will commit in the following
+ways :
+    <itemizedlist>
+        <listitem>
+            <para>
+                Invoke <methodname>EntityTransaction.setRollbackOnly
+                </methodname> prior to detachingyour objects. Setting the 
+                <literal>RollbackOnly</literal> flag prevents OpenJPA from 
+                flushing when detaching dirty objects; instead OpenJPA just 
+                runs its pre-flush actions (see the 
+                <methodname>OpenJPAEntityManager.preFlush</methodname>
 <ulink url="../javadoc/org/apache/openjpa/persistence/OpenJPAEntityManager.html">
 Javadoc</ulink> for details).
             </para>
             <para>
 This allows you to use the same instances in multiple
-attach/modify/detach/rollback cycles. Alternatively, you might also prevent a
-flush by making your modifications outside of a transaction (with <literal>
+attach/modify/detach/rollback cycles. 
+            </para>
+        </listitem>
+        <listitem>
+            <para>
+                Make your modifications outside of a transaction (with <literal>
 NontransactionalWrite</literal> enabled) before detaching.
             </para>
+        </listitem>
+        <listitem>
+            <para>
+            Set <literal>flushBeforeDetach</literal> 
+            to false (see <methodname>Compatibility.setFlushBeforeDetach
+            </methodname>
+            <ulink url="../javadoc/org/apache/openjpa/conf/Compatibility.html">
+Javadoc</ulink> ). This option is similar to the first option, but does not
+            affect the current transaction.
+            </para>
+        </listitem>
+    </itemizedlist>
+    </para>
         </section>
         <section id="ref_guide_attach_behavior">
             <title>



Mime
View raw message