3 Replies Latest reply on Mar 5, 2009 9:53 AM by Mark Candelora

    how to turn on ejb transaction demarcation?

    Andrew Batman Newbie

      hello!

      i have jboss-4.2.3GA and example from jboss-seam-2.1.1.GA sources (seam-booking)

      i want to save user in a transaction and then change his name in another transaction, so some changes were made in RegisterAction.java:

      public void register()
       {
       if ( user.getPassword().equals(verify) )
       {
       List existing = em.createQuery("select u.username from User u where u.username=#{user.username}")
       .getResultList();
       if (existing.size()==0)
       {
       persistUser();
      
       user.setName(user.getName() + "1");
       persistUserWithException();
      
       facesMessages.add("Successfully registered as #{user.username}");
       registered = true;
       }
       else
       {
       facesMessages.addToControl("username", "Username #{user.username} already exists");
       }
       }
       else
       {
       facesMessages.addToControl("verify", "Re-enter your password");
       verify=null;
       }
       }
      
       @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
       private void persistUser() {
       em.persist(user);
       }
      
       @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
       private void persistUserWithException() {
       em.persist(user);
       throw new RuntimeException();
       }


      this expects that user with original name will be persisted, but change of his name won't apply.

      but this doesn't work. both operations will be rollbacked because they are in the same transaction.

      what's wrong? please help me!

        • 1. Re: how to turn on ejb transaction demarcation?
          Mark Candelora Newbie

          I know little (nothing) about seam, but with stateless beans I've had very little success annotating private methods with @TransactionAttribute. I believe those annotations only work when the method is called from outside the EJB. In this case, it's probably just best for you to inject the UserTransaction as a @Resource and work with that. You should be able to use begin(), commit(), & rollback() for all your transaction needs.

          • 2. Re: how to turn on ejb transaction demarcation?
            Andrew Batman Newbie

            thank you!
            i've tried @TransactionAttribute outside the bean and it's helped.

            • 3. Re: how to turn on ejb transaction demarcation?
              Mark Candelora Newbie

              One other thing I forgot was the @TransactionManagement and @TransactionAttribute, when injecting the UserTransaction for the thread, I set these items on the bean like so:

              @TransactionManagement(TransactionManagementType.BEAN)
              @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
              public class MyBean
              {
              ...
              


              But this is for bean managed transactions, if you want to continue to use the container managed transactions which is the default, you don't need these, but you can specifically request CMT by doing this:


              @TransactionManagement(TransactionManagementType.CONTAINER)
              public class MyBean
              {
              ...