openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r963499 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/
Date Mon, 12 Jul 2010 21:39:55 GMT
Author: mikedd
Date: Mon Jul 12 21:39:55 2010
New Revision: 963499

URL: http://svn.apache.org/viewvc?rev=963499&view=rev
Log:
OPENJPA-1702: Allow new entities to be dirtied in BeforeCommit callback even if there are
no other dirty entities. Based on patch submitted by Heath Thomann

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/AnEntity.java
  (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestBeforeCommit.java
  (with props)
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java

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=963499&r1=963498&r2=963499&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 Mon
Jul 12 21:39:55 2010
@@ -3986,8 +3986,10 @@ public class BrokerImpl
      * Return a copy of all transactional state managers.
      */
     protected Collection<StateManagerImpl> getTransactionalStates() {
-        if (!hasTransactionalObjects())
-            return Collections.EMPTY_SET;
+        if (!hasTransactionalObjects()) {
+            // return a new empty set. Entities may be added by TransactionListeners 
+            return new LinkedHashSet<StateManagerImpl>();
+        }
         return _transCache.copy();
     }
 
@@ -4707,8 +4709,10 @@ public class BrokerImpl
          * Return a copy of all transactional state managers.
          */
         public Collection copy() {
-            if (isEmpty())
-                return Collections.EMPTY_SET;
+            if (isEmpty()) {
+                // Transaction Listeners may add entities to the transaction. 
+                return new LinkedHashSet();
+            }
 
             // size may not be entirely accurate due to refs expiring, so
             // manually copy each object; doesn't matter this way if size too

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/AnEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/AnEntity.java?rev=963499&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/AnEntity.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/AnEntity.java
Mon Jul 12 21:39:55 2010
@@ -0,0 +1,62 @@
+/*
+ * 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.event;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Version;
+
+@Entity
+public class AnEntity {
+	@Id
+	private int id;
+	
+	@Version 
+	private int version;
+	
+	public int getVersion() {
+        return version;
+    }
+
+    public void setVersion(int version) {
+        this.version = version;
+    }
+
+    private String name;
+	
+	public void setId(int id) {
+		this.id = id;
+	}
+
+	public int getId() {
+		return id;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public String getName() {
+		return name;
+	}
+	
+	public String toString(){
+		return "id = " + id + ", name = " + name;
+	}
+}

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

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestBeforeCommit.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestBeforeCommit.java?rev=963499&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestBeforeCommit.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/event/TestBeforeCommit.java
Mon Jul 12 21:39:55 2010
@@ -0,0 +1,140 @@
+/*
+ * 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.event;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Query;
+
+import org.apache.openjpa.event.TransactionEvent;
+import org.apache.openjpa.event.TransactionListener;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
+import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase;
+
+public class TestBeforeCommit extends AbstractPersistenceTestCase implements TransactionListener
{
+
+    AnEntity ae = null;
+    public final int PKID = 2;
+
+    private static OpenJPAEntityManagerFactorySPI emf = null;
+
+    public void setUp() {
+        if (emf == null) {
+            emf = createEMF(AnEntity.class);
+        }
+        EntityManager em = emf.createEntityManager();
+        EntityTransaction tran = em.getTransaction();
+
+        tran.begin();
+        em.createQuery("Delete from AnEntity").executeUpdate();
+        tran.commit();
+
+        tran.begin();
+        ae = new AnEntity();
+        ae.setId(PKID);
+        ae.setName("");
+        em.persist(ae);
+        tran.commit();
+        em.close();
+    }
+
+    public void testQuery() {
+        OpenJPAEntityManagerSPI em = (OpenJPAEntityManagerSPI) emf.createEntityManager();
+        em.addTransactionListener(this);
+        EntityTransaction tran = em.getTransaction();
+
+        tran.begin();
+        ae = doQuery(em);
+        assertEquals("", ae.getName());
+        assertEquals(1, ae.getVersion());
+        tran.commit();
+
+        ae = doQuery(em);
+        assertEquals("Ava", ae.getName());
+        assertEquals(2, ae.getVersion());
+
+        em.clear();
+        ae = em.find(AnEntity.class, PKID);
+        assertEquals("Ava", ae.getName());
+        assertEquals(2, ae.getVersion());
+
+        tran.begin();
+        tran.commit();
+        em.clear(); 
+        ae = em.find(AnEntity.class, PKID);
+        assertEquals("AvaAva", ae.getName());
+        assertEquals(3, ae.getVersion());
+
+        em.close();
+    }
+
+    public void testEmptyTransaction() {
+        OpenJPAEntityManagerSPI em = (OpenJPAEntityManagerSPI) emf.createEntityManager();
+        em.addTransactionListener(this);
+        EntityTransaction tran = em.getTransaction();
+        ae = doQuery(em);
+        assertEquals("", ae.getName());
+        assertEquals(1, ae.getVersion());
+        em.clear();
+
+        tran.begin();
+        tran.commit(); 
+        
+        // when BeforeCommit was fired AE was not managed. As a result its state is out of
sync with the database.
+        assertEquals("Ava", ae.getName());
+        ae = doQuery(em);
+        assertEquals("", ae.getName());
+        assertEquals(1, ae.getVersion());
+    }
+
+    public void beforeCommit(TransactionEvent event) {
+        ae.setName(ae.getName() + "Ava");
+    }
+
+    private AnEntity doQuery(EntityManager em) {
+        Query q = em.createQuery("select a from AnEntity a where a.id = :id");
+        return (AnEntity) q.setParameter("id", PKID).getSingleResult();
+    }
+
+    // Unused Interface methods
+    public void afterBegin(TransactionEvent event) {
+    }
+
+    public void afterFlush(TransactionEvent event) {
+    }
+
+    public void beforeFlush(TransactionEvent event) {
+    }
+
+    public void afterCommit(TransactionEvent event) {
+    }
+
+    public void afterCommitComplete(TransactionEvent event) {
+    }
+
+    public void afterRollback(TransactionEvent event) {
+    }
+
+    public void afterRollbackComplete(TransactionEvent event) {
+    }
+
+    public void afterStateTransitions(TransactionEvent event) {
+    }
+}

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



Mime
View raw message