EJB programming & troubleshooting: how to define a transaction boundaries

  1. how to define a transaction boundaries (2 messages)

    in SQL,I can use begin/commit/rollback to control where the transaction start/stop,in EJB,if I use the container managed transaction,I don't write any code to control transactions,instaed,container will control it,let's say,I have a web based application,jsps call session beans, I set the transaction to "required" for beans,how does container knows where the transaction start/stop?
  2. In most cases transaction boundaries are definded around signle method call. The code that defines transaction is placed between EJB interface call and bean object call. The idea is very similar to Aspect Oriented Programming, and , as far as I know, JBoss goes this way to define transactions.
    Transaction begins when client enters EJB Container boundary calling business method. When code exits EJB Container boundary transaction commitment depends on exit conditions. If code marks transaction as rollback-only (setRollbackOnly() on EntityContext or SessionContext) or throws unchecked (system) exception (for example EJBException) transaction performs rollback. If code returns ormally or with application exception, but without setting transaction as rollback only transaction is commited.
  3. For container transaction Required , you should also be able to control the transaction across multiple beans or bean methods by obtaining the UserTransaction :

    UserTransaction utx =
      (UserTransaction) context.lookup("java:comp/UserTransaction");




    As usual , to rollback , just call UserTransaction.rollback();