Wednesday, June 30, 2010

JPA @Transaction default properties

Lets examine JPA default Transaction attribute(@Transaction) and propagation

When using the @Transactional annotation by itself without any parameters, the propagation mode is set to REQUIRED, the read-only flag is set to false, the transaction isolation level is set to the database default (usually READ_COMMITTED), and the transaction will not roll back on a checked exception.


@Transactional(propagation=Propagation.REQUIRES_NEW)
The REQUIRES_NEW transaction attribute should be used only if the database action in the method being invoked needs to be saved to the database regardless of the outcome of the overlaying transaction. For example, suppose that every stock trade that was attempted had to be recorded in an audit database. This information needs to be persisted whether or not the trade failed because of validation errors, insufficient funds, or some other reason. If you did not use the REQUIRES_NEW attribute on the audit method, the audit record would be rolled back along with the attempted trade. Using the REQUIRES_NEW attribute guarantees that the audit data is saved regardless of the initial transaction's outcome. The main point here is always to use either the MANDATORY or REQUIRED attribute instead of REQUIRES_NEW unless you have a reason to use it for reasons similar those to the audit example.

2 comments:

  1. Your post is nicely written.
    However, in my humble opinion, you should have also mentioned the usage of SUPPORTS.
    As someone who is using transactions mainly with JEE applications deployed on jboss, I discovered that usage of REQUIRES for methods that perform only data retrieval hurts. For these methods SUPPORTS improves performance.

    ReplyDelete
  2. I used to bе аblе tο finԁ good іnfo fгοm your blog ρosts.
    My page :: Payday Loans Online Uk

    ReplyDelete