incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From timothyjw...@apache.org
Subject svn commit: r983645 - in /incubator/aries/trunk/jpa: jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/impl/ jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/ jpa-container/src/main/java/org/apache/aries/jpa...
Date Mon, 09 Aug 2010 13:43:41 GMT
Author: timothyjward
Date: Mon Aug  9 13:43:41 2010
New Revision: 983645

URL: http://svn.apache.org/viewvc?rev=983645&view=rev
Log:
ARIES-374 - JPA Persistence container tidy up

Modified:
    incubator/aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/impl/PersistenceContextManager.java
    incubator/aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPATest.java
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/CountingEntityManagerFactory.java
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/EntityManagerFactoryManager.java

Modified: incubator/aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/impl/PersistenceContextManager.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/impl/PersistenceContextManager.java?rev=983645&r1=983644&r2=983645&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/impl/PersistenceContextManager.java
(original)
+++ incubator/aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/impl/PersistenceContextManager.java
Mon Aug  9 13:43:41 2010
@@ -505,7 +505,7 @@ public class PersistenceContextManager e
       unregisterEM(name);
       boolean winner;
       synchronized(this) {
-        winner = units.remove(name) && units.isEmpty();
+        winner = !!!units.isEmpty() && units.remove(name) && units.isEmpty();
       }
       
       if(winner) {

Modified: incubator/aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPATest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPATest.java?rev=983645&r1=983644&r2=983645&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPATest.java
(original)
+++ incubator/aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPATest.java
Mon Aug  9 13:43:41 2010
@@ -32,6 +32,7 @@ import java.util.HashMap;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.PersistenceContextType;
+import javax.sql.DataSource;
 import javax.transaction.UserTransaction;
 
 import org.apache.aries.jpa.container.PersistenceUnitConstants;
@@ -173,8 +174,10 @@ public class QuiesceJPATest {
 
   @Test
   public void testComplexContextQuiesce() throws Exception {
-
-   // Get a managed context registered
+    //This is load bearing. we have to wait to create the EntityManager until the DataSource
is available
+    getOsgiService(DataSource.class);
+    
+    // Get a managed context registered
     PersistenceContextProvider provider = getOsgiService(PersistenceContextProvider.class);
     
     HashMap<String, Object> props = new HashMap<String, Object>();
@@ -240,11 +243,43 @@ public class QuiesceJPATest {
     emf = getOsgiService(EntityManagerFactory.class, "(&(osgi.unit.name=test-unit)("

         + PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true)" +
       "(" + PersistenceContextProvider.PROXY_FACTORY_EMF_ATTRIBUTE + "=*))", 100);
+    
+    //Test again to make sure we don't hold state over
+    
+    tm.begin();
+    
+    emf.createEntityManager().getProperties();
+    
+    callback = new TestQuiesceCallback();
+    
+    participant.quiesce(callback, Collections.singletonList(getBundle(
+        "org.apache.aries.jpa.org.apache.aries.jpa.container.itest.bundle")));
+    
+    Thread.sleep(1000);
+    
+    assertFalse("Quiesce finished", callback.bundleClearedUp());
+    
+    emf = getOsgiService(EntityManagerFactory.class, "(&(osgi.unit.name=test-unit)("

+        + PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true)" +
+      "(" + PersistenceContextProvider.PROXY_FACTORY_EMF_ATTRIBUTE + "=*))", 100);
+    
+    tm.commit();
+    
+    assertTrue("Quiesce not finished", callback.bundleClearedUp());
+    
+    refs = bundleContext.getAllServiceReferences(EntityManagerFactory.class.getName(), "(&(osgi.unit.name=test-unit)("

+          + PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true)" +
+        "(" + PersistenceContextProvider.PROXY_FACTORY_EMF_ATTRIBUTE + "=*))");
+    
+    assertNull("No context should exist",refs);
+    
   }
   
   @Test
   public void testContextRuntimeQuiesce() throws Exception {
-
+    //This is load bearing. we have to wait to create the EntityManager until the DataSource
is available
+    getOsgiService(DataSource.class);
+    
     PersistenceContextProvider provider = getOsgiService(PersistenceContextProvider.class);
     
     HashMap<String, Object> props = new HashMap<String, Object>();
@@ -322,7 +357,8 @@ public class QuiesceJPATest {
 
   @Test
   public void testComplexUnitQuiesce() throws Exception {
-
+    //This is load bearing. we have to wait to create the EntityManager until the DataSource
is available
+    getOsgiService(DataSource.class);
     
     EntityManagerFactory emf = getOsgiService(EntityManagerFactory.class, "(&(osgi.unit.name=test-unit)("

           + PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true))", DEFAULT_TIMEOUT);
@@ -365,10 +401,37 @@ public class QuiesceJPATest {
     
     emf = getOsgiService(EntityManagerFactory.class, "(&(osgi.unit.name=test-unit)("

         + PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true))", 100);
+    
+    //Test a second time to make sure state isn't held
+    
+    em = emf.createEntityManager();
+    
+    callback = new TestQuiesceCallback();
+    
+    participant.quiesce(callback, Collections.singletonList(getBundle(
+        "org.apache.aries.jpa.org.apache.aries.jpa.container.itest.bundle")));
+    
+    Thread.sleep(1000);
+    
+    assertFalse("Quiesce finished", callback.bundleClearedUp());
+    
+    emf = getOsgiService(EntityManagerFactory.class, "(&(osgi.unit.name=test-unit)("

+        + PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true))", DEFAULT_TIMEOUT);
+    
+    em.close();
+    
+    assertTrue("Quiesce not finished", callback.bundleClearedUp());
+    
+    refs = bundleContext.getAllServiceReferences(EntityManagerFactory.class.getName(), "(&(osgi.unit.name=test-unit)("

+          + PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true))");
+    
+    assertNull("No context should exist",refs);
   }
   
   @Test
   public void testContainerRuntimeQuiesce() throws Exception {
+    //This is load bearing. we have to wait to create the EntityManager until the DataSource
is available
+    getOsgiService(DataSource.class);
     
     EntityManagerFactory emf = getOsgiService(EntityManagerFactory.class, "(&(osgi.unit.name=test-unit)("

           + PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true))", DEFAULT_TIMEOUT);
@@ -403,7 +466,10 @@ public class QuiesceJPATest {
   @Test
   public void testComplexQuiesceInteraction() throws Exception {
 
-   // Get a managed context registered
+    //This is load bearing. we have to wait to create the EntityManager until the DataSource
is available
+    getOsgiService(DataSource.class);
+    
+    // Get a managed context registered
     PersistenceContextProvider provider = getOsgiService(PersistenceContextProvider.class);
     
     HashMap<String, Object> props = new HashMap<String, Object>();
@@ -491,8 +557,10 @@ public class QuiesceJPATest {
   
   @Test
   public void testComplexQuiesceInteraction2() throws Exception {
-
-   // Get a managed context registered
+    //This is load bearing. we have to wait to create the EntityManager until the DataSource
is available
+    getOsgiService(DataSource.class);
+    
+    // Get a managed context registered
     PersistenceContextProvider provider = getOsgiService(PersistenceContextProvider.class);
     
     HashMap<String, Object> props = new HashMap<String, Object>();

Modified: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/CountingEntityManagerFactory.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/CountingEntityManagerFactory.java?rev=983645&r1=983644&r2=983645&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/CountingEntityManagerFactory.java
(original)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/CountingEntityManagerFactory.java
Mon Aug  9 13:43:41 2010
@@ -101,16 +101,31 @@ public class CountingEntityManagerFactor
     this.callback.compareAndSet(null, callback);
     if(count.get() == 0) {
       PersistenceBundleManager.unregister(this.reg.getAndSet(null));
+      this.callback.set(null);
       callback.callback(name);
     }
   }
 
   public void callback() {
-    if(count.decrementAndGet() == 0 && callback.get() != null) {
-      PersistenceBundleManager.unregister(reg.getAndSet(null));
-      callback.get().callback(name);
+    
+    if(count.decrementAndGet() == 0) {
+      NamedCallback c = callback.getAndSet(null);
+      if(c != null) {
+        PersistenceBundleManager.unregister(reg.getAndSet(null));
+        c.callback(name);
+      }
     }
       
   }
 
+  public void clearQuiesce() {
+    //We will already be unregistered
+    reg.set(null);
+    NamedCallback c = callback.getAndSet(null);
+    //If there was a callback then call it in case time hasn't run out.
+    if(c != null) {
+      c.callback(name);
+    }
+  }
+
 }

Modified: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/EntityManagerFactoryManager.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/EntityManagerFactoryManager.java?rev=983645&r1=983644&r2=983645&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/EntityManagerFactoryManager.java
(original)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/EntityManagerFactoryManager.java
Mon Aug  9 13:43:41 2010
@@ -176,13 +176,14 @@ public class EntityManagerFactoryManager
   private void unregisterEntityManagerFactories() {
     //If we have registrations then unregister them
     if(registrations != null) {
-      for(ServiceRegistration reg : registrations.values()) {
+      for(Entry<String, ServiceRegistration> entry : registrations.entrySet()) {
         try {
-          reg.unregister();
+          entry.getValue().unregister();
         } catch (Exception e) {
           _logger.error("There was an error unregistering the EntityManagerFactory services
for bundle " 
               + bundle.getSymbolicName() + "_" + bundle.getVersion() , e);
         }
+        emfs.get(entry.getKey()).clearQuiesce();
       }
       // remember to set registrations to be null
       registrations = null;
@@ -349,7 +350,8 @@ public class EntityManagerFactoryManager
     Map<CountingEntityManagerFactory, ServiceRegistration> entries = new HashMap<CountingEntityManagerFactory,
ServiceRegistration>();
     Collection<String> names = new ArrayList<String>();
     synchronized(this) {
-      quiesce = true;
+      if((bundle.getState() & (Bundle.ACTIVE | Bundle.STARTING)) != 0)
+        quiesce = true;
       if(emfs != null) {
         for(String key : emfs.keySet()) {
           entries.put(emfs.get(key), registrations != null ? registrations.get(key) : null);



Mime
View raw message