3 Replies Latest reply on Sep 1, 2010 1:04 PM by njrich28

    Understand Seam Transaction vs EJB transaction

    paradigmza.sean.healthbridge.co.za

      Hi,


      My understanding of a Seam Transaction  (when you add
      <transaction:ejb-transaction />) to components.xml is that they work like EJB transactions. So if you throw an exception the transaction does not roll back unless it is a Runtime or @ApplicationException(rollback=true).



      My code works as expected when I have



      @Stateless
      @Name("loginRequest")
      public class LoginRequestEJB implements LoginRequest {
      
          public void login() throws LoginException() {
            ...
            updateAttemptsLeft();
            throw new LoginException();
            ...
          }
      }



      The attempts left gets updated in the DB... but now when I try use a plain Seam component org.jboss.seam.util.Work rolls back all Exceptions.


      @Name("loginRequest")
      public class LoginRequest {
      
          @Transactional
          public void login() throws LoginException() {
            ...
            updateAttemptsLeft();
            throw new LoginException();
            ...
          }
      }



      is this a bug or as expected?... and if it is as expected, how do I get the same behaviour of a EJB transaction with a plain Seam component?


      I am using Seam 2.0.2CR2 with JBoss 4.2.2 and I am using the login component from a Servlet.


      Thanks,
      Sean.

        • 1. Re: Understand Seam Transaction vs EJB transaction
          pmuir

          Hmm, I think you are right. File an issue in JIRA, I need to think about this a bit.

          • 2. Re: Understand Seam Transaction vs EJB transaction
            nuuh

            Hi,


            We have a project and we have customers and users..


            one user opend a customer account (page) for change and write new staff about customer if one of the other users open the same customer and change, first users changing ll be lost..


            so i need a system for not lose the changing.. like if one user select a customer, this customer object ll be lock.. i thing this transaction for what i need right??


            i didnt understand how to use transactions in my project..


            if you now something (links) can you write.. :)


            thanks all..

            • 3. Re: Understand Seam Transaction vs EJB transaction
              njrich28

              This sounds more like locking than transactions. If you're using Hibernate or JPA then have a look at 'optimistic locking' using the @Version annotation.


              You basically specify one field of Customer class as a version field


                private @Version Long version;



              Hibernate/JPA will then increment the version field every time it updates the row in the database. When it updates the field it checks that the version in the database is the same as the one it loaded - if it's different then it knows that the row has been updated by another user/process and throws an exception.


              It's very efficient and doesn't require any actual locks in the database but prevents users from overwriting changes made by other users.