4 Replies Latest reply on Apr 20, 2004 3:06 PM by Jim

    UserTransaction and Servlets

    Jim Newbie

      I have a servlet running in a standalone copy of Tomcat. This servlet needs to call several methods accross several sessionbeans as part of a unit of work. How can I create a UserTransaction which will allow me to rollback everything if any sessionbean call fails?

      Thanks,
      Jim

        • 1. Re: UserTransaction and Servlets
          sesques Novice

          You must use a user transaction created in your servlet, and configure all your beans with transaction type set to "required".

          Code for your servlet:

          try{
           ctx = new InitialContext();
           tx = (UserTransaction)ctx.lookup("UserTransaction");
           tx.begin();
           ...
           //Do what you want with your beans
           ...
           tx.commit();
          } catch (Exception e) {
           tx.rollback();
          } finally {
           ctx.close();
          }
          


          Since the Arjuna integration onto JBoss, I think that JBoss supports Natively JTS (which is not part of EJB spec) without specific configuration.

          Hope this helps you


          • 2. Re: UserTransaction and Servlets
            Jim Newbie

             

            "sesques" wrote:

            Code for your servlet:

            try{
             ctx = new InitialContext();
             tx = (UserTransaction)ctx.lookup("UserTransaction");
            


            I tried this and received the following Exception:
            javax.naming.NameNotFoundException: Name UserTransaction is not bound in this Context

            • 3. Re: UserTransaction and Servlets
              sesques Novice

              Of course, I'm silly.
              You cannot initiate a JBoss UserTransaction because your servlet is running in a standalone tomcat instance, which is the web container. The user transaction is part of EJB container only.

              It depends on how your Tomcat is configured. Does it have a JTS library ?
              Also, the UserTransaction JNDI name should be name "java:comp/
              env/UserTransaction" or something like that.

              I confess that I don't know this config, so I cannot help you anymore.
              Good luck

              • 4. Re: UserTransaction and Servlets
                Jim Newbie

                 

                Does it have a JTS library


                It's a stock Tomcat install. What jar file should I search for to see if JTS is installed?

                Thanks,
                Jim