Hi Olivier, Whenever a system exception is thrown by any of the session bean's business methods the bean instance is discarded. In the case of stateless session beans each method call goes to an instance in the pool. It need not be the same instance. So even if the instance is discarded this is transparent to the bean developer. In the case of a stateful session bean, all the calls go to the same bean and so if the previous call threw a system exception then the corressponding stateful session instance will be discarded and you will get the javax.ejb.NoSuchEJBException.
Use the @javax.ejb.ApplicationException annotation to specify that an exception class is an application exception. Regards Manu On Wed, Nov 19, 2008 at 8:38 PM, Olivier THIERRY <[EMAIL PROTECTED]> wrote: > Hi, > > I have a project with EJB3 Seam components. I write unit tests that will run > on OpenEJB, but I encounter a problem I can't find it is a Seam or OpenEJB > one. > > I wrote a Seam component with CONVERSATION scope, i.e. a stateful session > bean. I wrote the following unit test for this component : > > public void testSaveAndDeleteOrganizationType() throws > java.lang.Exception > { > new ComponentTest() { > public void testComponents() throws java.lang.Exception { > > OrganizationInternalServiceLocal service = > (OrganizationInternalServiceLocal) > getInstance("organizationInternalService"); > > // Create a new organization type > > OrganizationType newOrganizationType = new > OrganizationType(); > newOrganizationType.setCode("ORG_TYPE_TEST"); > newOrganizationType.setName("Test organization type"); > newOrganizationType = > service.saveOrganizationType(newOrganizationType); > assert newOrganizationType.getId() != null; > > // Create a duplicated organization type > > try { > OrganizationType duplicatedOrganizationType = new > OrganizationType(); > duplicatedOrganizationType.setCode("ORG_TYPE_TEST"); > duplicatedOrganizationType.setName("Test organization > type"); > duplicatedOrganizationType = > service.saveOrganizationType(duplicatedOrganizationType); > assert false; > } > catch (Exception exception){ > assert true; > } > > // Delete an organization type > > service.deleteOrganizationType(newOrganizationType); > assert service.loadOrganizationType("ORG_TYPE_TEST") == > null; > > } > }.run(); > } > > I have a unique constraint on code property of organization type. So when I > try to save an organization type with a code that is already in database, > the saveOrganizationType is expected to throw an exception. Actually it > does, no problem about that. But further calls to the component, for example > deleteOrganizationType method, fail with the following stack trace : > > testSaveAndDeleteOrganizationType(t4.core.commons.organization.internal.test.OrganizationInternalServiceTest) > Time elapsed: 0.156 sec <<< FAILURE! > javax.ejb.NoSuchEJBException: reference is invalid > at > org.apache.openejb.core.ivm.BaseEjbProxyHandler.isValidReference(BaseEjbProxyHandler.java:319) > at > org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:232) > at $Proxy135.deleteOrganizationType(Unknown Source) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) > at > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) > at java.lang.reflect.Method.invoke(Method.java:585) > at org.jboss.seam.util.Reflections.invoke(Reflections.java:22) > at > org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31) > at > org.jboss.seam.intercept.ClientSideInterceptor$1.proceed(ClientSideInterceptor.java:76) > at > org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56) > at > org.jboss.seam.ejb.RemoveInterceptor.aroundInvoke(RemoveInterceptor.java:43) > at > org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) > at > org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107) > at > org.jboss.seam.intercept.ClientSideInterceptor.invoke(ClientSideInterceptor.java:54) > at > org.javassist.tmp.java.lang.Object_$$_javassist_27.deleteOrganizationType(Object_$$_javassist_27.java) > at > t4.core.commons.organization.internal.test.OrganizationInternalServiceTest$26.testComponents(OrganizationInternalServiceTest.java:1276) > at > org.jboss.seam.mock.AbstractSeamTest$ComponentTest.run(AbstractSeamTest.java:163) > at > t4.core.commons.organization.internal.test.OrganizationInternalServiceTest.testSaveAndDeleteOrganizationType(OrganizationInternalServiceTest.java:1246) > > If I set the component to STATELESS scope, i.e. staless session bean, my > unit test works OK ! So what I note is that throwing an exception from a > stateful session bean turns it to invalid. Do you think this is a problem > with OpenEJB or with Seam ? > > Thanks in advance, > > Olivier >