Allow EntityManagerFactory objects to be serialized and deserialized successfully.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@618844 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
David Ezzio 2008-02-06 00:51:34 +00:00
parent 3f439edc57
commit 754eb8d60c
2 changed files with 49 additions and 46 deletions

View File

@ -144,8 +144,7 @@ public abstract class AbstractBrokerFactory
*/
protected AbstractBrokerFactory(OpenJPAConfiguration config) {
_conf = config;
_pcClassLoaders = new ConcurrentReferenceHashSet(
ConcurrentReferenceHashSet.WEAK);
getPcClassLoaders();
}
/**
@ -284,13 +283,13 @@ public abstract class AbstractBrokerFactory
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 =
@ -815,4 +814,15 @@ public abstract class AbstractBrokerFactory
_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;
}
}

View File

@ -30,30 +30,29 @@ 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 class TestSerializedFactory extends SingleEMFTestCase {
public void setUp() {
setUp(AllFieldTypes.class);
}
/**
* This test case assumes that OpenJPA creates EMF objects that are
* instances of the Serializable interface. If this changes, the test
* logic has to change.
* This test case assumes that OpenJPA creates EMF objects that are
* instances of the Serializable interface. If this changes, the test logic
* has to change.
* <p>
* Currently, although the EMF objects implement Serializable, they
* do not successfully pass through serialization. Once they do
* (assuming they should), the catch block in the test and the
* fail method invocation can be removed.
* Currently, although the EMF objects implement Serializable, they do not
* successfully pass through serialization. Once they do (assuming they
* should), the catch block in the test and the fail method invocation can
* be removed.
*/
public void testSerializedEntityManagerFactory() throws Exception {
// correct the logic if and when EMFs do not implement
// correct the logic if and when EMFs do not implement
// the serializable interface
assertTrue("EntityManagerFactory object is not serializable",
assertTrue("EntityManagerFactory object is not serializable",
emf instanceof Serializable);
// serialize and deserialize the entity manager factory
@ -61,40 +60,34 @@ public class TestSerializedFactory
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(emf);
EntityManagerFactory emf2 =
(EntityManagerFactory) new ObjectInputStream(
new ByteArrayInputStream(baos.toByteArray())).readObject();
(EntityManagerFactory) new ObjectInputStream(
new ByteArrayInputStream(baos.toByteArray())).readObject();
try {
// 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());
// 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());
// clean up any committed records, etc.
clear(emf2);
if (emf2.isOpen())
emf2.close();
assertFalse("The entity manager factory is not closed",
emf2.isOpen());
// Correct the logic when EMF's are supposed to serialize
fail("This test is expected to fail until the issue of " +
"serializing an EMF is settled");
}
catch (Exception e) {
// failure is currently expected
}
assertFalse("The entity manager factory is not closed",
emf2.isOpen());
}
public static void main(String[] args) {
TestRunner.run(TestSerializedFactory.class);
}
}