4 Replies Latest reply on Feb 26, 2012 3:12 AM by hantsy bai

    How can I use seam transaction in servlet

    Jeffrey Jia Newbie

      Environments:

       

      seam 3.1:  (solder , seam-persistence, seam-transaction, weld-servlet 1.1.5)

       

      I inject EntityManager in  my servlet ,now I can usr entityManager query from database but when pesist a enity I got an exception:

       

       

      @Inject

      EntityManager em;

       

      em.persist(user);

      em.flush();

        • 1. Re: How can I use seam transaction in servlet
          hantsy bai Master

          How do you produces the EntityManager..?

           

          I created another none Conversation scoped EntityManager producor, it worked well.

          • 2. Re: How can I use seam transaction in servlet
            Jeffrey Jia Newbie

            public class EntityManagerProducer

            {

              

               @ExtensionManaged

               @Produces

               @PersistenceUnit(unitName="jpa")

               @ConversationScoped

               EntityManagerFactory emf; 

            }

             

             

             

             

            @Inject

            EntityManager em;

             

            em.persist(user);

            em.flush();

             

            java.lang.reflect.UndeclaredThrowableException

                at $Proxy87.flush(Unknown Source)

                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

                at java.lang.reflect.Method.invoke(Method.java:601)

                at org.jboss.weld.bean.proxy.AbstractBeanInstance.invoke(AbstractBeanInstance.java:44)

                at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:105)

                at org.jboss.weld.proxies.EntityManager$ManagedPersistenceContext$-1598289256$Proxy$_$$_WeldClientProxy.flush(EntityManager$ManagedPersistenceContext$-1598289256$Proxy$_$$_WeldClientProxy.java)

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

                at org.jboss.solder.servlet.exception.CatchExceptionFilter.doFilter(CatchExceptionFilter.java:65)

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

                at org.jboss.solder.servlet.event.ServletEventBridgeFilter.doFilter(ServletEventBridgeFilter.java:74)

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

                at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:126)

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)

                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)

                at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)

                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)

                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)

                at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)

                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)

                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)

                at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)

                at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)

                at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304)

                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)

                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)

                at java.lang.Thread.run(Thread.java:722)

            Caused by: java.lang.reflect.InvocationTargetException

                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

                at java.lang.reflect.Method.invoke(Method.java:601)

                at org.jboss.seam.persistence.PersistenceContextProxyHandler.invoke(PersistenceContextProxyHandler.java:56)

                at org.jboss.seam.persistence.ManagedPersistenceContextProxyHandler.invoke(ManagedPersistenceContextProxyHandler.java:108)

                ... 36 more

            Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress

                at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:971)

                ... 42 more

            • 3. Re: How can I use seam transaction in servlet
              Jeffrey Jia Newbie

              because entityManger is @ConversationScoped  so in servlet  active the conversation context

               

              protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException{

               

              HttpConversationContext conversationContext = Container.instance()

                              .deploymentManager().instance()

                              .select(HttpConversationContext.class).get();

                      conversationContext.associate(request);

                      conversationContext.activate();

                      conversationContext.getCurrentConversation().begin();

               

               

               

              ...

               

              em.persist(uf);

                            em.flush();

               

              ...

               

              conversationContext.getCurrentConversation().end();

                              conversationContext.deactivate();

              }

               

              when I persist a bean  I got

              : javax.persistence.TransactionRequiredException: no transaction is in progress

               


              • 4. Re: How can I use seam transaction in servlet
                hantsy bai Master

                I added another producer in my project, it worked well.

                 

                @Produces

                @PersistenceContext

                @Utility

                EntityManager em;

                 

                use the following

                 

                @Inject

                @Utility

                EntityManager em

                 

                to enject EntityManager in servlet codes.

                 

                Utility is a custom @Qualifiar ...