4 Replies Latest reply on May 4, 2011 8:51 PM by Stuart Douglas

    javax.persistence.TransactionRequiredException (Glassfish 3, JSF 2.0, Weld, JPA, Seam 3 Persistence Module)

    Kamalpreet Singh Newbie

      Hello Everybody,


      I am getting javax.persistence.TransactionRequiredException in our application. We are using below technology stack:


      Glassfish Server 3
      JAVAEE6
      JSF 2.0
      CDI (Weld)
      JPA
      Seam 3 Persistence Module



      The layered architecture of our application is as follows:



      projectname-domain.jar with beans.xml and persistence.xml under META-INF 
      projectname-persistence.jar with beans.xml under META-INF 
      projectname-business.jar with beans.xml under META-INF 
      projectname-adminapplication.war with beans.xml under WEB-INF 
      




      Exception:
      javax.persistence.TransactionRequiredException
           at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
           at javax.faces.component.UICommand.broadcast(UICommand.java:315)
           at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
           at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
           at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
           at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
           at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
           at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
           at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
           at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
           at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
           at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
           at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
           at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
           at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
           at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
           at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
           at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
           at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
           at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
           at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
           at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
           at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
           at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
           at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
           at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
           at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
           at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
           at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
           at java.lang.Thread.run(Thread.java:619)
      Caused by: javax.faces.el.EvaluationException: javax.persistence.TransactionRequiredException
           at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
           at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
           ... 32 more
      Caused by: javax.persistence.TransactionRequiredException
           at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTxRequiredCheck(EntityManagerWrapper.java:163)
           at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTransactionScopedTxCheck(EntityManagerWrapper.java:145)
           at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:263)
           at com.convivial.trueshop.dao.impl.GenericDAO.update(GenericDAO.java:82)
           at com.convivial.trueshop.dao.category.impl.org$jboss$weld$bean-WEB-INF$lib$trueshop-persistence-1$0-SNAPSHOT-ManagedBean-classcom$convivial$trueshop$dao$category$impl$CategoryDAO$$WeldClientProxy.update(org$jboss$weld$bean-WEB-INF$lib$trueshop-persistence-1$0-SNAPSHOT-ManagedBean-classcom$convivial$trueshop$dao$category$impl$CategoryDAO$$WeldClientProxy.java)
           at com.convivial.trueshop.biz.catalog.impl.ProductCatalogManager.updateCategory(ProductCatalogManager.java:46)
           at com.convivial.trueshop.biz.catalog.impl.org$jboss$weld$bean-WEB-INF$lib$trueshop-business-1$0-SNAPSHOT-ManagedBean-classcom$convivial$trueshop$biz$catalog$impl$ProductCatalogManager$$WeldClientProxy.updateCategory(org$jboss$weld$bean-WEB-INF$lib$trueshop-business-1$0-SNAPSHOT-ManagedBean-classcom$convivial$trueshop$biz$catalog$impl$ProductCatalogManager$$WeldClientProxy.java)
           at com.convivial.trueshop.app.admin.catalog.category.CategoryAction.updateCategory(CategoryAction.java:67)
           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:597)
           at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
           at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
           at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43)
           at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56)
           at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
           at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
           ... 33 more


      Code:


      @ManagedBean
      @SessionScoped
      public class CategoryAction {
      
           private Category category;
           
           @Inject
           private IProductCatalogManager productCatalogManager;
           
           public Category getCategory() {
                return category;
           }
      
           public void setCategory(Category category) {
                this.category = category;
           }
           
           public String updateCategory() {
                category = productCatalogManager.updateCategory(category);
                return "/catalog/category/Category";
           }
      }




      @Named
      @ApplicationScoped
      @Transactional
      public class ProductCatalogManager implements IProductCatalogManager {
      
           @Inject
           private ICategoryDAO categoryDAO;
           
           @Override
           public Category updateCategory(Category category) {
                return categoryDAO.update(category);
           }
      }




      @Named
      @ApplicationScoped
      public class CategoryDAO extends GenericDAO<Category, Long> implements ICategoryDAO {
      
           @Override
           public Category updateCategory(Category category) {
                return super.update(category);
           }
      }



      @Named
      @ApplicationScoped
      public abstract class GenericDAO<T, ID extends Serializable> implements IGenericDAO<T, ID> {
           
           private final Class<T> persistentClass;
           
           @PersistenceContext
           private EntityManager entityManager;
      
           @SuppressWarnings("unchecked")
           public GenericDAO() {
                // Some code here.
                this.persistentClass = localPersistentClass;
           }
      
           public Class<T> getPersistentClass() {
                return persistentClass;
           }
      
           public EntityManager getEntityManager() {
                return entityManager;
           }
      
           @Override
           public T update(T entity) {
                entityManager.persist(entity);
                return entity;
           }
      }



      beans.xml content of projectname-business.jar




      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://java.sun.com/xml/ns/javaee" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd">
      
           <interceptors>
                <class>org.jboss.seam.transaction.TransactionInterceptor</class>
           </interceptors>
      
      </beans>



      Please help me what I am missing to resolve this exception.


      Thanks in advance
      Kamal