openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From curti...@apache.org
Subject svn commit: r1134726 - 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/detachment/ openjpa-persistenc...
Date Sat, 11 Jun 2011 19:00:01 GMT
Author: curtisr7
Date: Sat Jun 11 19:00:01 2011
New Revision: 1134726

URL: http://svn.apache.org/viewvc?rev=1134726&view=rev
Log:
OPENJPA-2014: Allow proxies to stay in place when using LiteAutoDetach.

Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/DetachOptions.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachManagerLite.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestDetachLite.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/model/DMCustomer.java
    openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_remote.xml

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/DetachOptions.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/DetachOptions.java?rev=1134726&r1=1134725&r2=1134726&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/DetachOptions.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/DetachOptions.java
Sat Jun 11 19:00:01 2011
@@ -35,6 +35,7 @@ public abstract class DetachOptions
     private boolean _access = true;
     
     private boolean _liteAutoDetach = false;
+    private boolean _detachProxyFields = true;
 
     /**
      * The {@link DetachState} constant.
@@ -142,6 +143,24 @@ public abstract class DetachOptions
     public boolean getLiteAutoDetach() {
         return (getDetachState() & DETACH_LOADED) == 1 && _liteAutoDetach;
     }
+    
+    /**
+     * Whether to detach proxy fields.
+     */
+    public void setDetachProxyFields(boolean b) {
+        _detachProxyFields = b;
+    }
+    
+    /**
+     * Whether to detach proxy fields.
+     */
+    public boolean getDetachProxyFields() {
+        // This property can only be set to false when using lite auto detach.
+        if(!_liteAutoDetach){
+            return true;
+        }
+        return _detachProxyFields;
+    }
 
     /**
      * Detach loaded state.

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=1134726&r1=1134725&r2=1134726&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 Sat
Jun 11 19:00:01 2011
@@ -258,7 +258,7 @@ public class BrokerImpl
     private LifecycleEventManager _lifeEventManager = null;
     private int _lifeCallbackMode = 0;
 
-    private transient DetachManagerLite _dmLite = new DetachManagerLite();
+    private transient DetachManagerLite _dmLite;
     
     private transient boolean _initializeWasInvoked = false;
     private transient boolean _fromWriteBehindCallback = false;
@@ -382,6 +382,7 @@ public class BrokerImpl
             _instm.start(InstrumentationLevel.BROKER, this);
         }
 
+        _dmLite = new DetachManagerLite(_conf);
         _printParameters =
             Boolean.parseBoolean(Configurations.parseProperties(_conf.getConnectionFactoryProperties()).getProperty(
                 PRINT_PARAMETERS_CONFIG_STR, "false"));
@@ -3442,7 +3443,7 @@ public class BrokerImpl
             _transAdditions.clear();
         }
         if (_dmLite == null) {
-            _dmLite = new DetachManagerLite();
+            _dmLite = new DetachManagerLite(_conf);
         }
         _dmLite.detachAll(states);
     }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachManagerLite.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachManagerLite.java?rev=1134726&r1=1134725&r2=1134726&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachManagerLite.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DetachManagerLite.java
Sat Jun 11 19:00:01 2011
@@ -21,17 +21,20 @@ package org.apache.openjpa.kernel;
 import java.util.BitSet;
 import java.util.Collection;
 
+import org.apache.openjpa.conf.OpenJPAConfiguration;
 import org.apache.openjpa.enhance.PersistenceCapable;
 import org.apache.openjpa.meta.ClassMetaData;
 import org.apache.openjpa.meta.FieldMetaData;
 import org.apache.openjpa.util.Proxy;
 
 /**
- * 
- *
+ * Handles detaching instances.
  */
 public class DetachManagerLite {
-    public DetachManagerLite() {
+    private final boolean _detachProxies;
+
+    public DetachManagerLite(OpenJPAConfiguration conf) {
+        _detachProxies = conf.getDetachStateInstance().getDetachProxyFields();
     }
 
     /**
@@ -46,7 +49,7 @@ public class DetachManagerLite {
             ClassMetaData cmd = sm.getMetaData();
             if (sm.isPersistent() && cmd.isDetachable()) {
                 PersistenceCapable pc = sm.getPersistenceCapable();
-                if(pc.pcIsDetached() == false){
+                if (pc.pcIsDetached() == false) {
                     // Detach proxy fields.
                     BitSet loaded = sm.getLoaded();
                     for (FieldMetaData fmd : cmd.getProxyFields()) {
@@ -70,13 +73,17 @@ public class DetachManagerLite {
      * @param sm
      *            The StateManagerImpl that the PersistenceCapable belongs to.
      */
-    private void detachProxyField(FieldMetaData fmd, PersistenceCapable pc,
-            StateManagerImpl sm, TransferFieldManager fm) {
+    private void detachProxyField(FieldMetaData fmd, PersistenceCapable pc, 
+        StateManagerImpl sm, TransferFieldManager fm) {
+        
         int fieldIndex = fmd.getIndex();
         if (fmd.isLRS() == true) {
             // need to null out LRS fields.
             nullField(fieldIndex, pc, sm, fm);
         } else {
+            if (!_detachProxies) {
+                return;
+            }
             Object o = sm.fetchObject(fieldIndex);
             if (o instanceof Proxy) {
                 // Get unproxied object and replace

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestDetachLite.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestDetachLite.java?rev=1134726&r1=1134725&r2=1134726&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestDetachLite.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/TestDetachLite.java
Sat Jun 11 19:00:01 2011
@@ -18,19 +18,25 @@
  */
 package org.apache.openjpa.persistence.detachment;
 
+import java.util.Calendar;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
 import org.apache.openjpa.conf.Compatibility;
 import org.apache.openjpa.enhance.PersistenceCapable;
 import org.apache.openjpa.persistence.detachment.model.DMCustomer;
 import org.apache.openjpa.persistence.detachment.model.DMCustomerInventory;
 import org.apache.openjpa.persistence.detachment.model.DMItem;
+import org.apache.openjpa.util.Proxy;
 
 public class TestDetachLite extends TestDetach {
+    Object[] props =
+        new Object[] { "openjpa.DetachState", "loaded(LiteAutoDetach=true)", DMCustomer.class,
+            DMCustomerInventory.class, DMItem.class, CLEAR_TABLES };
+
     public void setUp() {
-        super.setUp(
-            "openjpa.DetachState", "loaded(LiteAutoDetach=true)", 
-            DMCustomer.class, DMCustomerInventory.class, DMItem.class, 
-            CLEAR_TABLES
-            );
+        super.setUp(props);
 
         Compatibility compat = emf.getConfiguration().getCompatibilityInstance();
         compat.setCopyOnDetach(false);
@@ -39,6 +45,64 @@ public class TestDetachLite extends Test
         root = createData();
     }
 
+    public void testLeaveProxy() {
+        Object[] p = props;
+        p[1] = "loaded(LiteAutoDetach=true,DetachProxyFields=false)";
+        EntityManagerFactory iemf = createEMF(p);
+        try {
+            DMCustomer dc = new DMCustomer();
+            Calendar cal = Calendar.getInstance();
+            cal.setTimeInMillis(System.currentTimeMillis());
+            dc.setCal(cal);
+
+            EntityManager iem = iemf.createEntityManager();
+            try {
+                iem.getTransaction().begin();
+                iem.persist(dc);
+                iem.getTransaction().commit();
+                Calendar beforeDetachCal = dc.getCal();
+                iem.clear();
+                Calendar afterDetachCal = dc.getCal();
+
+                assertTrue(beforeDetachCal instanceof Proxy);
+                assertTrue(afterDetachCal instanceof Proxy);
+            } finally {
+                if (iem.getTransaction().isActive()) {
+                    iem.getTransaction().rollback();
+                }
+                iem.close();
+            }
+
+        } finally {
+            iemf.close();
+        }
+    }
+
+    public void testProxyClear() {
+        DMCustomer dc = new DMCustomer();
+        Calendar cal = Calendar.getInstance();
+        cal.setTimeInMillis(System.currentTimeMillis());
+        dc.setCal(cal);
+
+        EntityManager iem = emf.createEntityManager();
+        try {
+            iem.getTransaction().begin();
+            iem.persist(dc);
+            iem.getTransaction().commit();
+            Calendar beforeDetachCal = dc.getCal();
+            iem.clear();
+            Calendar afterDetachCal = dc.getCal();
+
+            assertTrue(beforeDetachCal instanceof Proxy);
+            assertFalse(afterDetachCal instanceof Proxy);
+        } finally {
+            if (iem.getTransaction().isActive()) {
+                iem.getTransaction().rollback();
+            }
+            iem.close();
+        }
+    }
+
     public void testCloseDetach() {
         root = em.merge(root);
         PersistenceCapable pc = (PersistenceCapable) root;

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/model/DMCustomer.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/model/DMCustomer.java?rev=1134726&r1=1134725&r2=1134726&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/model/DMCustomer.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment/model/DMCustomer.java
Sat Jun 11 19:00:01 2011
@@ -19,6 +19,7 @@
 package org.apache.openjpa.persistence.detachment.model;
 
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.List;
 
 import javax.persistence.CascadeType;
@@ -26,6 +27,8 @@ import javax.persistence.Entity;
 import javax.persistence.FetchType;
 import javax.persistence.Id;
 import javax.persistence.OneToMany;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
 import javax.persistence.Version;
 
 @Entity 
@@ -44,6 +47,9 @@ public class DMCustomer {
             private List<DMCustomerInventory> customerInventories =
                 new ArrayList<DMCustomerInventory>();
 
+    @Temporal(TemporalType.TIMESTAMP)
+    Calendar cal;
+    
     public DMCustomer() {
     }
 
@@ -79,4 +85,10 @@ public class DMCustomer {
             List<DMCustomerInventory> customerInventories) {
         this.customerInventories = customerInventories;
     }
+    public void setCal(Calendar c){
+        cal = c;
+    }
+    public Calendar getCal(){
+        return cal;
+    }
 }

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=1134726&r1=1134725&r2=1134726&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 Sat Jun 11 19:00:01
2011
@@ -373,6 +373,21 @@ When merging a detached Entity back into
                 </itemizedlist>
                         </para>
                     </listitem>
+                    <listitem>                    
+                        <para>
+<literal>DetachProxyFields</literal>: <emphasis role="bold">This option
is ONLY valid when used in conjunction with the <literal>LiteAutoDetach</literal>

+DetachState setting.</emphasis> When detaching the persistence context, all proxies
will be left in place. Note, that 
+all <literal>Large Result Sets</literal> will be removed.
+                        </para>
+                        <itemizedlist>
+                            <listitem>
+                            <literal>true(default)</literal>: All proxies will
be removed and LRS fields will be removed. 
+                        	</listitem>
+                            <listitem>
+                            <literal>false</literal>: All proxies will be left
in place and LRS fields will be removed. 
+                        	</listitem>                        	
+                        </itemizedlist>                                           
        
+                    </listitem>                
                 </itemizedlist>
                 <example id="ref_guide_detach_graph_confex">
                     <title>



Mime
View raw message