You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by ma...@apache.org on 2008/11/13 12:08:19 UTC

svn commit: r713703 - in /openejb/trunk/openejb3/container/openejb-core/src: main/java/org/apache/openejb/core/stateless/ test/java/org/apache/openejb/core/stateless/

Author: manugeorge
Date: Thu Nov 13 03:08:19 2008
New Revision: 713703

URL: http://svn.apache.org/viewvc?rev=713703&view=rev
Log:
Fix for OPENEJB-953 OpenEJB embedded does not release stateless EJB instances in case of RuntimeException.This fix adds a discardInstance method to the StatelessInstanceManager which is called to release the semaphore in case of strict pooling.

Modified:
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessContainer.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java
    openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessInstanceManagerPoolingTest.java

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessContainer.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessContainer.java?rev=713703&r1=713702&r2=713703&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessContainer.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessContainer.java Thu Nov 13 03:08:19 2008
@@ -217,8 +217,9 @@
 
                 // The bean instance is not put into the pool via instanceManager.poolInstance
                 // and therefore the instance will be garbage collected and destroyed.
-                // For this reason the discardInstance method of the StatelessInstanceManager
-                // does nothing.
+                // In case of StrictPooling flag being set to true we also release the semaphore
+            	// in the discardInstance method of the instanceManager.
+            	instanceManager.discardInstance(callContext);
                 handleSystemException(txPolicy, re, callContext);
             } else {
                 /* Application Exception ***********************/

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java?rev=713703&r1=713702&r2=713703&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java Thu Nov 13 03:08:19 2008
@@ -302,6 +302,21 @@
             }
         }
     }
+    
+    /**
+     * This method is called to release the semaphore in case of the business method 
+     * throwing a system exception
+     * 
+     * @param callContext
+     * @param bean
+     */
+    public void discardInstance(ThreadContext callContext) {    	
+    	if (strictPooling) {
+    	    CoreDeploymentInfo deploymentInfo = callContext.getDeploymentInfo();
+            Data data = (Data) deploymentInfo.getContainerData();        
+            data.getSemaphore().release();            
+        }
+    }
 
     private void freeInstance(ThreadContext callContext, Instance instance) {
         try {

Modified: openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessInstanceManagerPoolingTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessInstanceManagerPoolingTest.java?rev=713703&r1=713702&r2=713703&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessInstanceManagerPoolingTest.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessInstanceManagerPoolingTest.java Thu Nov 13 03:08:19 2008
@@ -24,6 +24,7 @@
 import javax.ejb.Remote;
 import javax.ejb.Stateless;
 import javax.naming.InitialContext;
+import javax.naming.NamingException;
 
 import junit.framework.TestCase;
 
@@ -88,6 +89,48 @@
 
     }
     
+    public void testStatelessBeanRelease() throws Exception {
+    	
+    	
+    	final CountDownLatch invocations = new CountDownLatch(30);
+        final InitialContext ctx = new InitialContext(); 
+        final AtomicInteger instances = new AtomicInteger();
+        // Do a business method...
+        Runnable r = new Runnable(){        	
+        	public void run(){
+                Object object = null;
+				try {
+					object = ctx.lookup("CounterBeanLocal");
+				} catch (NamingException e) {					
+					assertTrue(false);
+				}                                
+                Counter counter = (Counter) object;
+                assertNotNull(counter);
+                try{                
+        		    counter.explode(invocations);        	
+                }catch(Exception e){
+                	
+                }        	   
+            }
+        };
+
+        //  -- READY --
+
+        // 30 instances should be created and discarded.
+        for (int i = 0; i < 30; i++) {
+            Thread t = new Thread(r);
+            t.start();            
+        }
+
+        boolean success = invocations.await(10000, TimeUnit.MILLISECONDS);
+
+        assertTrue(success);
+
+        assertEquals(30, CounterBean.discardedInstances.get());
+
+    }
+
+    
     public void testStatelessBeanTimeout() throws Exception {
 
         InitialContext ctx = new InitialContext();
@@ -183,6 +226,7 @@
     public static interface Counter {
         int count();
         void race(CountDownLatch ready, CountDownLatch go);
+        void explode(CountDownLatch invocations);
     }
     
     @Remote
@@ -198,6 +242,7 @@
     public static class CounterBean implements Counter, RemoteCounter {
 
         public static AtomicInteger instances = new AtomicInteger();
+        public static AtomicInteger discardedInstances = new AtomicInteger();
 
         private int count;
 
@@ -208,6 +253,19 @@
         public int count(){
         	return instances.get();
         }
+        
+        public int discardCount(){
+        	return discardedInstances.get();
+        }
+        
+        public void explode(CountDownLatch invocations){
+        	try{
+        	    discardedInstances.incrementAndGet();
+        	    throw new NullPointerException();
+        	}finally{
+        		invocations.countDown();
+        	}
+        }
 
         public void race(CountDownLatch ready, CountDownLatch go){
             comment("ready = " + count);