2 Replies Latest reply on Mar 26, 2008 3:43 PM by Pete Muir

    EntityManager is closed exception in SeamTest

    Jeff Skjonsby Newbie

      I'm trying to get some basic CRUD functionality working in my application.  I have a simple Entity called Category and a simple controller for it that inherits from EntityController.  In my test I keep getting EntityManager is closed exceptions in the second test in my suite.  I searched around quite a bit in the forums (both of them) and google without any hints.


      Here's the exception:


      java.lang.AssertionError: javax.el.ELException: javax.ejb.EJBTransactionRolledbackException: EntityManager is closed
          at org.jboss.seam.mock.BaseSeamTest$Request.onException(BaseSeamTest.java:488)
          at org.jboss.seam.mock.BaseSeamTest$Request$2.doFilter(BaseSeamTest.java:537)
          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
          at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
          at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
          at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
          at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
          at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
          at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
          at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
          at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
          at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
          at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
          at org.jboss.seam.mock.BaseSeamTest$Request.run(BaseSeamTest.java:524)
          at com.grocery2dinner.actions.CategoryManagerActionTest.testFind(CategoryManagerActionTest.java:41)
      Caused by: javax.el.ELException: javax.ejb.EJBTransactionRolledbackException: EntityManager is closed
          at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:333)
          at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:342)
          at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58)
          at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
          at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
          at org.jboss.seam.mock.BaseSeamTest$Request.invokeMethod(BaseSeamTest.java:496)
          at com.grocery2dinner.actions.CategoryManagerActionTest$1.invokeApplication(CategoryManagerActionTest.java:28)
          at org.jboss.seam.mock.BaseSeamTest$Request.invokeApplicationPhase(BaseSeamTest.java:660)
          at org.jboss.seam.mock.BaseSeamTest$Request.emulateJsfLifecycle(BaseSeamTest.java:609)
          at org.jboss.seam.mock.BaseSeamTest$Request.access$300(BaseSeamTest.java:184)
          at org.jboss.seam.mock.BaseSeamTest$Request$2.doFilter(BaseSeamTest.java:530)
          ... 40 more
      Caused by: javax.ejb.EJBTransactionRolledbackException: EntityManager is closed
          at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:91)
          at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
          at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:197)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:64)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:166)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:249)
          at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:214)
          at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:86)
          at $Proxy83.find(Unknown Source)
          at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
          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.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
          at org.jboss.seam.intercept.ClientSideInterceptor.invoke(ClientSideInterceptor.java:54)
          at org.javassist.tmp.java.lang.Object_$$_javassist_2.find(Object_$$_javassist_2.java)
          at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:329)
          ... 50 more
      Caused by: java.lang.IllegalStateException: EntityManager is closed
          at org.hibernate.ejb.EntityManagerImpl.getSession(EntityManagerImpl.java:42)
          at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:175)
          at org.jboss.seam.persistence.EntityManagerProxy.find(EntityManagerProxy.java:85)
          at org.hibernate.search.jpa.impl.FullTextEntityManagerImpl.find(FullTextEntityManagerImpl.java:94)
          at org.jboss.seam.persistence.EntityManagerProxy.find(EntityManagerProxy.java:85)
          at org.jboss.seam.framework.EntityController.find(EntityController.java:47)
          at com.grocery2dinner.actions.CategoryManagerAction.find(CategoryManagerAction.java:36)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:121)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:110)
          at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
          at org.jboss.seam.intercept.EJBInvocationContext.proceed(EJBInvocationContext.java:44)
          at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
          at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
          at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
          at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:46)
          at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
          at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
          at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
          at org.jboss.seam.persistence.EntityManagerProxyInterceptor.aroundInvoke(EntityManagerProxyInterceptor.java:26)
          at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
          at org.jboss.seam.persistence.HibernateSessionProxyInterceptor.aroundInvoke(HibernateSessionProxyInterceptor.java:27)
          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.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:50)
          at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:118)
          at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
          ... 84 more
      



      Here's the controller: (as you can see there's not much to it at this point)



      package com.grocery2dinner.actions;
      
      import javax.ejb.Stateless;
      
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.framework.EntityController;
      
      import com.grocery2dinner.model.Category;
      
      @Stateless
      @Name("catManager")
      public class CategoryManagerAction extends EntityController implements CategoryManager
      {
          private static final long serialVersionUID = 1L;
      
          @In(required=false) @Out(required=false)
          private Category category;
          
          private long id;
          
          public String delete()
          {
              remove(category);
              return null;
          }
      
          public String find()
          {
              info("Finding Category with ID: " + id);
              category = find(Category.class, id);        
              return null;
          }
      
          public String save()
          {
              persist(category);
              return null;
          }
      
          public String update()
          {
              merge(category);
              flush();
              return null;
          }
      
          public long getId()
          {
              return id;
          }
      
          public void setId(long id)
          {
              this.id = id;
          }
          
          public void destroy() {}
      }



      Here's my Test:


      package com.grocery2dinner.actions;
      
      import javax.persistence.EntityManager;
      
      import org.jboss.seam.mock.SeamTest;
      import org.testng.annotations.Test;
      
      import com.grocery2dinner.model.Category;
      
      
      @SuppressWarnings("unchecked")
      public class CategoryManagerActionTest extends SeamTest
      {
      
          @Test(dependsOnMethods={"testSave"})
          public void testFind() throws Exception
          {
              new FacesRequest()
              {
                  @Override
                  protected void invokeApplication() throws Exception
                  {
                      setValue("#{catManager.id}", 1);
                      invokeMethod("#{catManager.find}");
                  }
      
                  @Override
                  protected void renderResponse() throws Exception
                  {
                      Category cat2 = (Category) getValue("#{category}");
                      assert cat2.getName().equals("Food");
                      
                      System.out.println(cat2.getCategory().getId());
                      assert cat2.getCategory().getId() == 1;
                  }
                  
              }.run();
          }
      
          @Test()
          public void testSave() throws Exception
          {
              new FacesRequest()
              {
                  @Override
                  protected void updateModelValues() throws Exception
                  {
                      setValue("#{category.name}", "Food");
                      Category cat = (Category) getValue("#{category}");
                      System.out.println("Category Name:" + cat.getName());
                      assert cat.getName().equals("Food");
                  }
      
                  @Override
                  protected void invokeApplication() throws Exception
                  {
                      invokeMethod("#{catManager.save}");
                  }
              }.run();
              
              new FacesRequest()
              {
                  @Override
                  protected void invokeApplication() throws Exception
                  {
                      EntityManager em = (EntityManager) getValue("#{entityManager}");
                      Category found = em.find(Category.class, new Long(1));
                      assert found != null;
                      assert found.getId() == 1;
                      assert found.getName().equals("Food");
                  }
              }.run();        
          }  
      
      }
      



      It will run the testSave test just fine, but fails every time for the testFind.  Thanks in advance for any help.