cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r430627 - in /incubator/cayenne/main/trunk/core/cayenne-jpa/src: main/java/org/apache/cayenne/jpa/ main/java/org/apache/cayenne/jpa/cspi/ test/java/org/apache/cayenne/jpa/
Date Fri, 11 Aug 2006 02:28:41 GMT
Author: aadamchik
Date: Thu Aug 10 19:28:41 2006
New Revision: 430627

URL: http://svn.apache.org/viewvc?rev=430627&view=rev
Log:
fixing resource local transactions - EntityManager.getTransaction() MUST return a transaction
instance

Modified:
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/JpaEntityManager.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/JpaEntityManagerFactory.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaEntityManager.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaEntityTransaction.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/JpaEntityManagerTest.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/MockJpaEntityManager.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/MockJpaEntityManagerFactory.java

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/JpaEntityManager.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/JpaEntityManager.java?rev=430627&r1=430626&r2=430627&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/JpaEntityManager.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/JpaEntityManager.java
Thu Aug 10 19:28:41 2006
@@ -17,11 +17,9 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.jpa;
 
 import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
 import javax.persistence.EntityNotFoundException;
 import javax.persistence.EntityTransaction;
 import javax.persistence.FlushModeType;
@@ -29,6 +27,7 @@
 import javax.persistence.PersistenceException;
 import javax.persistence.Query;
 import javax.persistence.TransactionRequiredException;
+import javax.persistence.spi.PersistenceUnitTransactionType;
 
 /**
  * Base implementation of a non-JTA EntityManager.
@@ -39,7 +38,7 @@
 
     protected PersistenceContextType contextType;
     protected FlushModeType flushMode;
-    protected EntityManagerFactory factory;
+    protected JpaEntityManagerFactory factory;
     protected EntityTransaction transaction;
     protected boolean open;
     protected Object delegate;
@@ -47,7 +46,12 @@
     /**
      * Creates a new JpaEntityManager, initializing it with a parent factory.
      */
-    public JpaEntityManager(EntityManagerFactory factory) {
+    public JpaEntityManager(JpaEntityManagerFactory factory) {
+
+        if (factory == null) {
+            throw new IllegalArgumentException("Null entity manager factory");
+        }
+
         this.factory = factory;
         this.open = true;
     }
@@ -328,20 +332,27 @@
      * used serially to begin and commit multiple transactions.
      * 
      * @return EntityTransaction instance
-     * @throws IllegalStateException if invoked on a JTA EntityManager or an EntityManager
-     *             that has been closed.
+     * @throws IllegalStateException if invoked on a JTA EntityManager.
      */
     public EntityTransaction getTransaction() {
-        checkClosed();
+        // note - allowed to be called on a closed EM
 
-        return transaction;
-    }
+        if (factory.getUnitInfo().getTransactionType() == PersistenceUnitTransactionType.JTA)
{
+            throw new IllegalStateException(
+                    "'getTransaction' is called on a JTA EntityManager");
+        }
 
-    public void setTransaction(EntityTransaction transaction) {
-        checkClosed();
+        if (transaction == null) {
+            this.transaction = createResourceLocalTransaction();
+        }
 
-        this.transaction = transaction;
+        return transaction;
     }
+
+    /**
+     * A method that creates a new resource-local transaction.
+     */
+    protected abstract EntityTransaction createResourceLocalTransaction();
 
     /**
      * Indicates to the EntityManager that a JTA transaction is active. This method should

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/JpaEntityManagerFactory.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/JpaEntityManagerFactory.java?rev=430627&r1=430626&r2=430627&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/JpaEntityManagerFactory.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/JpaEntityManagerFactory.java
Thu Aug 10 19:28:41 2006
@@ -17,7 +17,6 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.jpa;
 
 import java.util.Collections;
@@ -108,5 +107,12 @@
      */
     public void setDelegate(Object delegate) {
         this.delegate = delegate;
+    }
+
+    /**
+     * Returns PersistenceUnitInfo used by this factory.
+     */
+    PersistenceUnitInfo getUnitInfo() {
+        return unitInfo;
     }
 }

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaEntityManager.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaEntityManager.java?rev=430627&r1=430626&r2=430627&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaEntityManager.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaEntityManager.java
Thu Aug 10 19:28:41 2006
@@ -17,12 +17,11 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.jpa.cspi;
 
 import java.util.Map;
 
-import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
 import javax.persistence.PersistenceException;
 import javax.persistence.Query;
 
@@ -31,16 +30,23 @@
 import org.apache.cayenne.DataObjectUtils;
 import org.apache.cayenne.Persistent;
 import org.apache.cayenne.access.DataContext;
+import org.apache.cayenne.access.Transaction;
 import org.apache.cayenne.jpa.JpaEntityManager;
+import org.apache.cayenne.jpa.JpaEntityManagerFactory;
 
 public class CjpaEntityManager extends JpaEntityManager {
 
     private DataContext context;
 
-    public CjpaEntityManager(DataContext context, EntityManagerFactory factory,
+    public CjpaEntityManager(DataContext context, JpaEntityManagerFactory factory,
             Map parameters) {
         super(factory);
         this.context = context;
+    }
+
+    @Override
+    protected EntityTransaction createResourceLocalTransaction() {
+        return new CjpaEntityTransaction(Transaction.internalTransaction(null));
     }
 
     @Override

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaEntityTransaction.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaEntityTransaction.java?rev=430627&r1=430626&r2=430627&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaEntityTransaction.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaEntityTransaction.java
Thu Aug 10 19:28:41 2006
@@ -16,8 +16,6 @@
  *  specific language governing permissions and limitations
  *  under the License.
  ****************************************************************/
-
-
 package org.apache.cayenne.jpa.cspi;
 
 import java.sql.SQLException;
@@ -29,9 +27,9 @@
 import org.apache.cayenne.access.Transaction;
 
 /**
- * A JPA wrapper around a cayenne Transaction
+ * A JPA wrapper around a Cayenne Transaction.
  * 
- * @see http://objectstyle.org/confluence/display/CAYDOC/Understanding+Transactions
+ * @see http://cwiki.apache.org/CAYDOC/understanding-transactions.html
  */
 public class CjpaEntityTransaction implements EntityTransaction {
 

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/JpaEntityManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/JpaEntityManagerTest.java?rev=430627&r1=430626&r2=430627&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/JpaEntityManagerTest.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/JpaEntityManagerTest.java
Thu Aug 10 19:28:41 2006
@@ -17,23 +17,38 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.jpa;
 
+import javax.persistence.EntityTransaction;
 import javax.persistence.FlushModeType;
 
 import junit.framework.TestCase;
 
+import org.apache.cayenne.jpa.spi.MockPersistenceUnitInfo;
+
 public class JpaEntityManagerTest extends TestCase {
 
     public void testOpenClose() throws Exception {
-        JpaEntityManager m = new MockJpaEntityManager(new MockEntityManagerFactory() {
+        JpaEntityManagerFactory factory = new MockJpaEntityManagerFactory() {
 
             @Override
             public boolean isOpen() {
                 return true;
             }
-        });
+        };
+        JpaEntityManager m = new MockJpaEntityManager(factory) {
+
+            @Override
+            protected EntityTransaction createResourceLocalTransaction() {
+                return new MockEntityTransaction() {
+
+                    @Override
+                    public boolean isActive() {
+                        return true;
+                    }
+                };
+            }
+        };
 
         assertTrue(m.isOpen());
 
@@ -84,23 +99,32 @@
     }
 
     public void testCloseActiveTransactionInProgress() {
-        JpaEntityManager m = new MockJpaEntityManager(new MockEntityManagerFactory() {
+        JpaEntityManagerFactory factory = new MockJpaEntityManagerFactory() {
 
             @Override
             public boolean isOpen() {
                 return true;
             }
-        });
-
-        assertTrue(m.isOpen());
+        };
 
-        m.setTransaction(new MockEntityTransaction() {
+        JpaEntityManager m = new MockJpaEntityManager(factory) {
 
             @Override
-            public boolean isActive() {
-                return true;
+            protected EntityTransaction createResourceLocalTransaction() {
+                return new MockEntityTransaction() {
+
+                    @Override
+                    public boolean isActive() {
+                        return true;
+                    }
+                };
             }
-        });
+        };
+
+        assertTrue(m.isOpen());
+        
+        // make sure we trigger transaction creation
+        assertNotNull(m.getTransaction());
 
         try {
             m.close();
@@ -114,18 +138,19 @@
     public void testCloseFactoryClosed() {
         final boolean[] factoryCloseState = new boolean[1];
 
-        JpaEntityManager m = new MockJpaEntityManager(new MockEntityManagerFactory() {
+        JpaEntityManagerFactory factory = new MockJpaEntityManagerFactory(
+                new MockPersistenceUnitInfo()) {
 
             @Override
             public boolean isOpen() {
                 return !factoryCloseState[0];
             }
-        });
+        };
 
-        assertTrue(m.isOpen());
+        JpaEntityManager m = new MockJpaEntityManager(factory);
 
+        assertTrue(m.isOpen());
         factoryCloseState[0] = true;
-
         assertFalse(m.isOpen());
     }
 }

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/MockJpaEntityManager.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/MockJpaEntityManager.java?rev=430627&r1=430626&r2=430627&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/MockJpaEntityManager.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/MockJpaEntityManager.java
Thu Aug 10 19:28:41 2006
@@ -20,13 +20,18 @@
 
 package org.apache.cayenne.jpa;
 
-import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
 import javax.persistence.Query;
 
 public class MockJpaEntityManager extends JpaEntityManager {
 
-    public MockJpaEntityManager(EntityManagerFactory factory) {
+    public MockJpaEntityManager(JpaEntityManagerFactory factory) {
         super(factory);
+    }
+    
+    @Override
+    protected EntityTransaction createResourceLocalTransaction() {
+        return null;
     }
 
     @Override

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/MockJpaEntityManagerFactory.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/MockJpaEntityManagerFactory.java?rev=430627&r1=430626&r2=430627&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/MockJpaEntityManagerFactory.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/MockJpaEntityManagerFactory.java
Thu Aug 10 19:28:41 2006
@@ -17,7 +17,6 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.jpa;
 
 import java.util.Map;
@@ -25,8 +24,14 @@
 import javax.persistence.EntityManager;
 import javax.persistence.spi.PersistenceUnitInfo;
 
+import org.apache.cayenne.jpa.spi.MockPersistenceUnitInfo;
+
 public class MockJpaEntityManagerFactory extends JpaEntityManagerFactory {
 
+    public MockJpaEntityManagerFactory() {
+        this(new MockPersistenceUnitInfo());
+    }
+
     public MockJpaEntityManagerFactory(PersistenceUnitInfo unitInfo) {
         super(unitInfo);
     }
@@ -35,5 +40,4 @@
     protected EntityManager createEntityManagerInternal(Map parameters) {
         return null;
     }
-
 }



Mime
View raw message