1 Reply Latest reply on Jul 19, 2006 7:52 AM by scraatz

    Are transactions used by default or do i HAVE to write trans


      From reading the transaction section of the trailblazer on ejb 3 (http://trailblazer.demo.jboss.com/EJB3Trail/services/transaction/index.html) I get the impression that transactions are always enabled, and that I don't have to write any code or use any transaction annotations at all, and still know that transactions are used. Am I right, or am I wrong?

      It is the following words that make me assume this (from the trailblazer):

      If a method does not have a transaction annotation, it is assigned the default transaction property REQUIRED by the EJB 3.0 container.

      But it also says:
      In EJB 3.0, the EntityManager must run in a transactional context to ensure the database integrity.

      So, does that mean, in my session bean, that I have to obtain the transaction manager from the entity manager and use it to ensure that transactions are used? Like in the following example:
      private EntityManager em;
      EntityTransaction tx = em.getTransaction();
      CD cd = new CD( "The Beatles", "Rubber Soul" );
      cd.addTrack( new Track( 220, "Norwegian Wood" ) );
      cd.addTrack( new Track( 180, "Drive My Car" ) );
      em.persist( cd );
      cd = new CD( "Deep Purple", "Machine Head" );
      cd.addTrack( new Track( 200, "Smoke On The Water" ) );
      cd.addTrack( new Track( 480, "Lazy" ) );
      em.persist( cd ) ;

      Or could I simply remove the 3 lines using tx as well as remove all transation annotations from the entity bean, and still be sure that transactions are used?

      Another wondering about the code above; if I only created one CD (and not two as above), would it be even relevant to talk about transactions?

        • 1. Re: Are transactions used by default or do i HAVE to write t


          from what I know, you do not need the Transaction code and should not use em.getTransaction() in EJB3 session beans.
          A session bean method is by default starting a transaction (when none is active) which is commited when the method succeeds and rolled back when the method throws an exception. The injected entity manager lives only as long as the method runs and is auto-closed by the container.
          The em.getTransaction() returns a resource (JDBC?) transaction which is not the same as the UserTransaction you can lookup from JNDI.