openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kwsut...@apache.org
Subject svn commit: r502751 - in /incubator/openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/
Date Fri, 02 Feb 2007 21:12:36 GMT
Author: kwsutter
Date: Fri Feb  2 13:12:35 2007
New Revision: 502751

URL: http://svn.apache.org/viewvc?view=rev&rev=502751
Log:
OPENJPA-119.  Needed to allow the EM.clear operation to perform without doing an implicit
flush.  Introduced a new boolean
parameter to indicate whether a flush is desired or not.  This allows both the new (correct)
behaviour for JPA as well as the 
old behaviour for other persistence personalities (JDO, etc).  I also introduced a new testcase
for this scenario, and updated 
a couple of other tests.

Added:
    incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerClear.java
  (with props)
Modified:
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Broker.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBroker.java
    incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerMethodsThrowAfterClose.java
    incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestPersistence.java
    incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java?view=diff&rev=502751&r1=502750&r2=502751
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
Fri Feb  2 13:12:35 2007
@@ -287,6 +287,8 @@
             Broker broker;
             for (Iterator itr = _brokers.iterator(); itr.hasNext();) {
                 broker = (Broker) itr.next();
+                /* Check for null here because _brokers is a weak reference
+                collection */
                 if ((broker != null) && (!broker.isClosed()))
                     broker.close();
             }

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Broker.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Broker.java?view=diff&rev=502751&r1=502750&r2=502751
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Broker.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Broker.java
Fri Feb  2 13:12:35 2007
@@ -473,9 +473,20 @@
     public void evictAll(Extent extent, OpCallbacks call);
 
     /**
-     * Detach all objects in place.
+     * Detach all objects in place.  A flush will be performed before
+     * detaching the entities.
      */
     public void detachAll(OpCallbacks call);
+
+    /**
+     * Detach all objects in place, with the option of performing a
+     * flush before doing the detachment.
+     * @param call Persistence operation callbacks
+     * @param flush boolean value to indicate whether to perform a
+     * flush before detaching the entities (true, do the flush;
+     * false, don't do the flush)
+     */
+    public void detachAll(OpCallbacks call, boolean flush);
 
     /**
      * Detach the specified object from the broker.

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java?view=diff&rev=502751&r1=502750&r2=502751
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
Fri Feb  2 13:12:35 2007
@@ -3088,9 +3088,16 @@
     }
 
     public void detachAll(OpCallbacks call) {
+        detachAll(call, true);
+    }
+
+    public void detachAll(OpCallbacks call, boolean flush) {
         beginOperation(true);
         try {
-            if ((_flags & FLAG_FLUSH_REQUIRED) != 0)
+            /* If a flush is desired (based on input parm), then check if the
+             * "dirty" flag is set before calling flush().
+             */
+            if ((flush) && ((_flags & FLAG_FLUSH_REQUIRED) != 0))
                 flush();
             detachAllInternal(call);
         } catch (OpenJPAException ke) {

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBroker.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBroker.java?view=diff&rev=502751&r1=502750&r2=502751
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBroker.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBroker.java
Fri Feb  2 13:12:35 2007
@@ -1115,6 +1115,14 @@
         }
     }
 
+    public void detachAll(OpCallbacks call, boolean flush) {
+        try {
+            _broker.detachAll(call, flush);
+        } catch (RuntimeException re) {
+            throw translate(re);
+        }
+    }
+
     public Object attach(Object obj, boolean copyNew, OpCallbacks call) {
         try {
             return _broker.attach(obj, copyNew, call);

Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerClear.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerClear.java?view=auto&rev=502751
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerClear.java
(added)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerClear.java
Fri Feb  2 13:12:35 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.simple;
+
+import java.util.HashMap;
+import java.util.Map;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+
+import junit.framework.TestCase;
+import junit.textui.TestRunner;
+
+/**
+ * Test case to ensure that the proper JPA clear semantics are processed.
+ *
+ * @author Kevin Sutter
+ */
+public class TestEntityManagerClear
+    extends TestCase {
+
+    private EntityManagerFactory emf;
+    private EntityManager em;
+
+    public void setUp() {
+        Map props = new HashMap(System.getProperties());
+        props.put("openjpa.MetaDataFactory",
+            "jpa(Types=" + AllFieldTypes.class.getName() + ")");
+        emf = Persistence.createEntityManagerFactory("test", props);
+    }
+
+    public void tearDown() {
+        if (emf == null)
+            return;
+        try {
+            EntityManager em = emf.createEntityManager();
+            em.getTransaction().begin();
+            em.createQuery("delete from AllFieldTypes").executeUpdate();
+            em.getTransaction().commit();
+            em.close();
+            emf.close();
+        } catch (Exception e) {
+        }
+    }
+    public void testClear() {
+        try {
+            // Create EntityManager and Start a transaction (1)
+            em = emf.createEntityManager();
+            em.getTransaction().begin();
+
+            // Insert a new object and flush
+            AllFieldTypes testObject1 = new AllFieldTypes();
+            testObject1.setStringField("my test object1");
+            em.persist(testObject1);
+            em.flush();
+
+            // Clear the PC for new object 2
+            AllFieldTypes testObject2 = new AllFieldTypes();
+            testObject1.setStringField("my test object2");
+            em.persist(testObject2);
+            em.clear();
+
+            // Commit the transaction (only object 1 should be in database)
+            em.getTransaction().commit();
+
+            // Start a new transaction
+            em.getTransaction().begin();
+
+            // Attempt retrieve of Object1 from previous PC (should exist)
+            assertEquals(1, em.createQuery
+                    ("select x from AllFieldTypes x where x.stringField = 'my test object1'").
+                    getResultList().size());
+
+            // Attempt retrieve of Object2 from previous PC (should not exist)
+            assertEquals(0, em.createQuery
+                    ("select x from AllFieldTypes x where x.stringField = 'my test object2'").
+                    getResultList().size());
+
+            // Rollback the transaction and close everything
+            em.getTransaction().rollback();
+            em.close();
+        } catch (Exception ex) {
+            fail("Unexpected Exception ex = " + ex);
+        }
+    }
+
+    public static void main(String[] args) {
+        TestRunner.run(TestEntityManagerClear.class);
+    }
+}
+

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

Modified: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerMethodsThrowAfterClose.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerMethodsThrowAfterClose.java?view=diff&rev=502751&r1=502750&r2=502751
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerMethodsThrowAfterClose.java
(original)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerMethodsThrowAfterClose.java
Fri Feb  2 13:12:35 2007
@@ -271,7 +271,7 @@
     }
 
     public static void main(String[] args) {
-        TestRunner.run(TestPersistence.class);
+        TestRunner.run(TestEntityManagerMethodsThrowAfterClose.class);
     }
 }
 

Modified: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestPersistence.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestPersistence.java?view=diff&rev=502751&r1=502750&r2=502751
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestPersistence.java
(original)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestPersistence.java
Fri Feb  2 13:12:35 2007
@@ -107,26 +107,6 @@
         em.close();
     }
 
-    /**
-     * Ensures that an IllegalStateException is thrown if getDelegate is called
-     * after closing the EntityManager.
-     */
-    public void testGetDelegateAfterClose() {
-        EntityManager em = emf.createEntityManager();
-
-        em.close();
-
-        try {
-            Object o = em.getDelegate();
-            fail();
-        }
-        catch(IllegalStateException ise) {
-            /*
-             * An IllegalStateException is expected. Nothing to do here.
-             */
-        }
-    }
-
     public static void main(String[] args) {
         TestRunner.run(TestPersistence.class);
     }

Modified: incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java?view=diff&rev=502751&r1=502750&r2=502751
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
(original)
+++ incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
Fri Feb  2 13:12:35 2007
@@ -853,7 +853,7 @@
 
     public void clear() {
         assertNotCloseInvoked();
-        _broker.detachAll(this);
+        _broker.detachAll(this, false);
     }
 
     public Object getDelegate() {



Mime
View raw message