5 Replies Latest reply on Dec 18, 2004 12:07 PM by chatbuilder

    Transactions

    chatbuilder

      Hi,

      I have difficulties to understand how to set up transactions.

      I'm using servlets and CMP entitity beans.

      On my servlet I want to do the following.

      //Start transaction
      client.pay(500);
      book.soldAmount(10);
      //commit transaction

      I'm using entity beans, so I'm not sure if I can use javax.transaction.UserTransaction.

      Thx in advance,
      Pieter

        • 1. Re: Transactions
          chatbuilder

          It seems to work like this:

          InitialenRemote initiaal = null;
          Context context = new InitialContext();
          UserTransaction userTran = (UserTransaction)context.lookup("UserTransaction");
          try{
           userTran.begin();
          
           String strInitiaal = initialenForm.getInitiaal();
           Object einRef = context.lookup("Initiaal");
           InitialenHomeRemote home = (InitialenHomeRemote)PortableRemoteObject.narrow(einRef, InitialenHomeRemote.class);
           initiaal = home.create(strInitiaal);
          
           userTran.commit();
          }catch(Exception e){
           userTran.rollback();
           e.printStackTrace();
          }
          


          But I'm not sure if this is the right way...

          • 2. Re: Transactions
            hariv

            You cannot use UserTransaction in entity Bean. Entity Beans can have only CMT(Container Managed Transaction). If you set the the transaction isolation level as required in your entity bean methods. The method will join the already created transaction from the caller of the method. In the servelet you can user UserTransaction and set the methods in entity bean with Transaction Required isolation level

            • 3. Re: Transactions
              chatbuilder

              Hi hariv,

              Can you give a bit more details how to implement this?

              Some code would be nice.

              Thx,
              Pieter

              • 4. Re: Transactions
                kero

                You need to add these in your ejb-jar
                ---------------------------------------------

                <container-transaction>

                <ejb-name>YourEJBname</ejb-name>
                <method-intf>Remote</method-intf>
                <method-name>YourMethodName</method-name>

                <trans-attribute>RequiresNew</trans-attribute>
                </container-transaction>

                -----------------------------------------------
                for the trans-attribute tag, there are a few option and you can read from sun.

                in >YourMethodName, you may set up a condition and do the rollback as :
                -----------------------------------------------

                context.setRollbackOnly();
                } catch (Exception ee) {
                ee.printStackTrace();
                }
                -----------------------------------------------

                • 5. Re: Transactions
                  chatbuilder

                  I use the usertransaction for CMP entity beans, and I must say it really works... I have also readed on the internet that userTransactions are only for session beans. So I don't have a clue why it works with me.

                  below the code of my action, of my struts application

                  
                  public ActionForward update(ActionMapping mapping,
                   ActionForm form,
                   HttpServletRequest request,
                   HttpServletResponse response)
                  throws Exception{
                  
                   Context context = new InitialContext();
                   UserTransaction userTran = (UserTransaction)context.lookup("UserTransaction");
                   try{
                  
                   InitialenForm initialenForm = (InitialenForm)form;
                   userTran.begin();
                  
                   Object ref = context.lookup("Initiaal");
                   InitialenHomeRemote home = (InitialenHomeRemote)PortableRemoteObject.narrow(ref, InitialenHomeRemote.class);
                  
                   home.create("NOT","NOT","NOT","NOT","NOT");
                  
                   InitialenRemote initiaal = home.findByPrimaryKey(initialenForm.getId());
                   initiaal.update(initialenForm.getInitiaal(),
                   initialenForm.getNaam(),
                   initialenForm.getDienst(),
                   initialenForm.getInterneEmail(),
                   initialenForm.getTelefoon(),
                   initialenForm.getLockIndicator());
                   initialenForm.setLockIndicator(initiaal.getLockindicator());
                  
                  
                   userTran.commit();
                  
                   ActionMessages messages = new ActionMessages();
                   messages.add("initiaal",new ActionMessage("initiaal.successvol.opgeslaan", initialenForm.getInitiaal()));
                   saveMessages(request,messages);
                   }catch(ActionException e){
                   userTran.rollback();
                   saveExceptions(request,e);
                   }catch(Exception ex){
                   userTran.rollback();
                   throw ex;
                   }
                   return mapping.findForward(IConstants.INPUT_KEY);
                  }
                  


                  My update method of my bean throws an ActionException (selfmade -exception) when the name is empty.

                  So when initialenForm.getNaam() is empty, I get a ROLLBACK. And when I check my MySQL database, I see that the home.create("NOT","NOT","NOT","NOT","NOT"); is also rollback.

                  Strange!!!