Author: dezzio
Date: Wed Jan 30 16:59:02 2008
New Revision: 616972

URL: http://svn.apache.org/viewvc?rev=616972&view=rev
Log:
Allow EntityManagerFactory objects to be serialized and deserialized 
successfully.

Added:
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestSerializedFactory.java
   (with props)
Modified:
    
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java

Modified: 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java?rev=616972&r1=616971&r2=616972&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
 (original)
+++ 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
 Wed Jan 30 16:59:02 2008
@@ -147,8 +147,7 @@
      */
     protected AbstractBrokerFactory(OpenJPAConfiguration config) {
         _conf = config;
-        _pcClassLoaders = new ConcurrentReferenceHashSet(
-            ConcurrentReferenceHashSet.WEAK);
+        getPcClassLoaders();
     }
 
     /**
@@ -287,13 +286,13 @@
                     if (needsSub(cls))
                         toRedefine.add(cls);
                 }
-                _pcClassLoaders.add(loader);
+                getPcClassLoaders().add(loader);
                 _pcClassNames = c;
             }
             _persistentTypesLoaded = true;
         } else {
             // reload with this loader
-            if (_pcClassLoaders.add(loader)) {
+            if (getPcClassLoaders().add(loader)) {
                 for (Iterator itr = _pcClassNames.iterator(); itr.hasNext();) {
                     try {
                         Class cls =
@@ -818,4 +817,15 @@
             _transactional.remove (_trans);
                }
        }
+   
+   /**
+    * Method insures that deserialized EMF has this reference re-instantiated
+    */
+   private Collection getPcClassLoaders() {
+      if (_pcClassLoaders == null)
+        _pcClassLoaders = new ConcurrentReferenceHashSet(
+            ConcurrentReferenceHashSet.WEAK);
+         
+      return _pcClassLoaders;
+   }
 }

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestSerializedFactory.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestSerializedFactory.java?rev=616972&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestSerializedFactory.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestSerializedFactory.java
 Wed Jan 30 16:59:02 2008
@@ -0,0 +1,74 @@
+/*
+ * 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.*;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+
+import junit.textui.TestRunner;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Tests that a EntityManagerFactory can be used after serialization.
+ *
+ * @author David Ezzio
+ */
+public class TestSerializedFactory
+    extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(AllFieldTypes.class);
+    }
+
+    public void testSerializedEntityManagerFactory() throws Exception {
+        // serialize and deserialize the entity manager factory
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(baos);
+        oos.writeObject(emf);
+        EntityManagerFactory emf2 = 
+            (EntityManagerFactory) new ObjectInputStream(
+            new ByteArrayInputStream(baos.toByteArray())).readObject();
+            
+        // use the deserialized entity manager factory
+        assertTrue("The deserialized entity manager factory is not open",
+            emf2.isOpen());
+        EntityManager em = emf2.createEntityManager();
+        assertTrue("The newly created entity manager is not open", 
em.isOpen());
+
+        // exercise the entity manager produced from the deserialized EMF
+        em.getTransaction().begin();
+        em.persist(new AllFieldTypes());
+        em.getTransaction().commit();
+        
+        // close the extra resources
+        em.close();
+        assertFalse("The entity manager is not closed", em.isOpen());
+        emf2.close();
+        assertFalse("The entity manager factory is not closed", emf2.isOpen());
+    }
+    
+    public static void main(String[] args) {
+        TestRunner.run(TestSerializedFactory.class);
+    }
+}
+

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


Reply via email to