[jboss-user] [EJB 3.0] - Are transactions used by default or do i HAVE to write trans

Markus.Wahl do-not-reply at jboss.com
Fri Jul 14 03:25:05 EDT 2006


>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):anonymous wrote : 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: anonymous wrote : 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:
@PersistenceContext
  | private EntityManager em;
  | ...
  | EntityTransaction tx = em.getTransaction();
  | tx.begin();
  | 
  | 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 ) ;
  |     
  | tx.commit();
  | em.close();
  | 
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?

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3957985#3957985

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3957985



More information about the jboss-user mailing list