reference is invalid error after exception
chawax Nov 19, 2008 11:28 AMHi,
I wrote unit tests for Seam components, using Seam 2.1.0.SP1 and HSQLDB as database. One of these tests saves an entity in database, then tries to save a duplicate one (unique constraint on code property of my entity) and then deletes the entity. Here is the unit test I wrote.
public void testSaveAndDeleteOrganization() throws java.lang.Exception { new ComponentTest() { public void testComponents() throws java.lang.Exception { OrganizationInternalServiceLocal service = (OrganizationInternalServiceLocal) getInstance("organizationInternalService"); // Create an organization Organization organization = new Organization(); organization.setCode("ORG_TEST"); organization.setDescription("ORG_TEST description"); organization.setName("ORG_TEST name"); organization = service.saveOrganization(organization); assert organization.getId() != null; // Create a duplicate organization // We should get an exception Organization duplicateOrganization = new Organization(); duplicateOrganization.setCode("ORG_TEST"); duplicateOrganization.setDescription("ORG_TEST description"); duplicateOrganization.setName("ORG_TEST name"); try { duplicateOrganization = service.saveOrganization(duplicateOrganization); assert false; } catch (Exception exception) { assert true; } // Delete an organization service.deleteOrganization(organization); assert service.loadOrganization("ORG_TEST") == null; } }.run(); }
But when running this test, I get the following exception on deleteOrganization method call. (Note I overrode Seam test classes to use OpenEJB instead of JBoss embedded because of bugs with inheritance that made my tests crash, that's why you can see OpenEJB logs in the stack trace).
testSaveAndDeleteOrganization(t4.core.commons.organization.internal.test.OrganizationInternalServiceTest) Time elapsed: 0.062 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 $Proxy120.deleteOrganization(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_11.deleteOrganization(Object_$$_javassist_11.java) at t4.core.commons.organization.internal.test.OrganizationInternalServiceTest$4.testComponents(OrganizationInternalServiceTest.java:204) at org.jboss.seam.mock.AbstractSeamTest$ComponentTest.run(AbstractSeamTest.java:163) at t4.core.commons.organization.internal.test.OrganizationInternalServiceTest.testSaveAndDeleteOrganization(OrganizationInternalServiceTest.java:144)
If I remove the lines for duplicate organization test, I don't have the problem ! So it looks like my Seam component is down after an exception is thrown. I have the same problem for any test with a unique constraint violation.
The organizationInternalService Seam component is CONVERSATION scoped and calls an organizationDao component (CONVERSATION scoped too). I use extended persistence context. All my components used to be STATELESS with no extended persistence context and the test used to work at that time. But I had to use extended persistence context because of lazy collections loading problems. Maybe persistence context is corrupted after an exception is thrown ? Is there something known about that ?
Any help welcome ;)