Friday, September 14, 2007

Transactions (EJB3)

Transactions in EJB3 and Seam

  • ACID
  • Put all transactions in your EJBs (not in servlets)
  • Container or Bean Managed (default in Seam)
    • Container-managed-transactions (CMTs) preferred in Java EE. The Java EE container will handle comitting, rolling back, starting the transaction, etc. (no coding for the transactions).
      • 6 types of transactions to choose from (default is REQUIRED)
        • MANDATORY
        • REQUIRED
        • REQUIRES_NEW
        • SUPPORTS
        • NOT_SUPPORTED
        • NEVER
      • Example: Set this annotation before a method to use a transaction independent of an already-active transaction. Will attempt to commit in the end of the method.
        @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
        public void addUser(User user) {
        em.persist(user);
        }
    • Bean-managed transactions (BMTs) allows a much more fine-grained control of transactions. (It is recommended to not use BMTs unless you cannot achieve the goals you are looking for using the CMT option.

No comments: