5 Replies Latest reply on Oct 11, 2011 9:15 AM by Marcus Schmidke

    javax.persistence.TransactionRequiredException

    Marcus Schmidke Newbie

      Hello all,

       

      after years of developing with Seam 2.x, I now try to jump start into a new world (AS7, builtin CDI etc.), but until now, I am not very succesful.

       

      I've written a very simple sample application, a few entities and one servlet. After lots of work, everything deploys fine, Hibernate generates tables, well.

       

      Then I call the servlet from Webbrowser. The servlet does nothing else than injecting and calling a SLSB, which itself tries to create and persist an entity.

       

      With Seam 2.x, I never worried about transactions - everything was fully automatic. In fact, I have absolutely no knowledge about EJB3 CMT, because Seam did everything for me.

       

      But now the only thing I get this this weird TransactionRequiredException. Google does not know anything about it (sureley because it is an absolute beginner's error).

       

      My servlet:

      @WebServlet(urlPatterns = "/*")

      public class VaadinAppServlet extends HttpServlet {

       

          @Inject

          Beispieldaten bd;

       

          @Override

          protected void service(HttpServletRequest arg0, HttpServletResponse arg1)

                  throws ServletException, IOException {

              bd.run();

              super.service(arg0, arg1);

          }

       

      }

       

      The SLSB:

      @Stateless

      @TransactionManagement(TransactionManagementType.CONTAINER)

      @TransactionAttribute(TransactionAttributeType.REQUIRED)

      public class BeispieldatenImpl implements Beispieldaten {

       

          @PersistenceContext

          private EntityManager session;

       

          public void run() {

             TestEntity te=new TestEntity();

             session.persist(te);

          }

       

      }

       

      (as you can see, I added lots of useless annotations just to avoid any possible mistake)

       

      And the interface:

      @Local

      public interface Beispieldaten {

       

          void run();

       

      }

       

      persistence.xml:

      <?xml version="1.0" encoding="UTF-8"?>

      <persistence version="2.0"

          xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

          xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

          <persistence-unit name="KochbuchServer"

              transaction-type="JTA">

              <jta-data-source>java:jboss/datasources/KochbuchDS</jta-data-source>

              <exclude-unlisted-classes>false</exclude-unlisted-classes>

              <properties>

                  <property name="hibernate.show_sql" value="true" />

                  <property name="hibernate.default_batch_fetch_size" value="100" />

                  <property name="hibernate.hbm2ddl.auto" value="create" />

                  <property name="jboss.entity.manager.factory.jndi.name"

                      value="java:/EntityManagerFactories/KochbuchServer" />

              </properties>

          </persistence-unit>

      </persistence>

       

       

      All packaged in an EAR - servlet in WAR, other classes in EJB-module.

       

      I'm sure my mistake is quite obvious, but I need help ...

       

      Thank you!

       

      Marcus.

        • 1. Re: javax.persistence.TransactionRequiredException
          jaikiran pai Master

          Can you please post your entire exception stacktrace? Also, instead of this:

           

          @Inject

              Beispieldaten bd;

           

          can you try:

           

          @EJB

              Beispieldaten bd;

          • 2. Re: javax.persistence.TransactionRequiredException
            Marcus Schmidke Newbie

            Changing from @Inject to @EJB gives:

                No component found for type 'de.schmidke.kochbuch.server.Beispieldaten'

            during deployment. Never used @EJB annotation before. Tried to add a beanName parameter, same error.

             

            With @Inject, the complete stack trace is as follows:

            11:33:52,879 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/KochbuchWeb].[de.schmidke.kochbuch.frontend.vaadin.VaadinAppServlet]] (http--127.0.0.1-8080-1) Servlet.service() for servlet de.schmidke.kochbuch.frontend.vaadin.VaadinAppServlet threw exception: javax.persistence.TransactionRequiredException: Transaction is required to perform this operation (either use a transaction or extended persistence context)

                at org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:686) [jboss-as-jpa-7.0.2.Final.jar:7.0.2.Final]

                at org.jboss.as.jpa.container.AbstractEntityManager.persist(AbstractEntityManager.java:574) [jboss-as-jpa-7.0.2.Final.jar:7.0.2.Final]

                at de.schmidke.kochbuch.server.BeispieldatenImpl.run(BeispieldatenImpl.java:29) [KochbuchServer.jar:]

                at de.schmidke.kochbuch.frontend.vaadin.VaadinAppServlet.service(VaadinAppServlet.java:27) [classes:]

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:139) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]

                at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]

                at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:49) [jboss-as-jpa-7.0.2.Final.jar:7.0.2.Final]

                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:952) [jbossweb-7.0.1.Final.jar:7.0.2.Final]

                at java.lang.Thread.run(Thread.java:619) [:1.6.0_07]

            • 3. Re: javax.persistence.TransactionRequiredException
              jaikiran pai Master

              What does your packaging look like? I'm not sure those EJBs are being picked up and deployed. Can you attach the server.log?

              • 4. Re: javax.persistence.TransactionRequiredException
                jaikiran pai Master

                That stacktrace too is a bit weird - as if the bean instance is directly provided (like a new BeispieldatenImpl()) to the user and invoked upon it.

                1 of 1 people found this helpful
                • 5. Re: javax.persistence.TransactionRequiredException
                  Marcus Schmidke Newbie

                  Now that you mention it ... you are right, there are missing lots of stack layers with interceptors and such. I did not notice it before.

                   

                  You gave me the right clue, I inspected my ear packaging. After years of packaging ears by hand, I tried for the first time to let Eclipse do it automatically, and obviously, I must have done something wrong - the ejb jar was packaged as an utility jar into the lib folder, not as a module in the ear.

                   

                  I corrected this and - tadah! - the error messages I get are completely different :-).

                   

                  You are right, the SLSB had not been recognized as such, and this was the error.

                   

                  Interesting though that the entity beans were recognized, but who cares - I have found my mistake.

                   

                  Thank you for your help!!

                   

                  Marcus.