Suggested Pages

Tuesday, June 5, 2012

J2EE Transaction Propagation vs Spring Transaction Propagation

J2EE Transaction Propagation

In a J2EE environment you can give information to the container to manage transactions. There are six transaction attribute to know:
  • Required
  • RequiresNew
  • Mandatory
  • Supports
  • NotSupported
  • Never

Nested or multiple transactions are not allowed within a method. The J2EE platform supports only flat transactions.

Required

In a CMT environment, Required is the implicit transaction attribute.
  • If the client is running within a transaction and invokes the enterprise bean’s method, the method executes within the client’s transaction;
  • If the client is not associated with a transaction, the container starts a new transaction before running the method.


RequiresNew

  • If the client is running within a JTA transaction and invokes the enterprise bean’s method, the container takes the following steps:
    • Suspends the client’s transaction;
    • Starts a new transaction;
    • Delegates the call to the method;
    • When the method and the transaction complete, the container resumes the suspended transaction
  • If the client is not associated with a transaction, the container starts a new JTA transaction before running the method.

You should use the RequiresNew attribute when you want to ensure that the method always runs within a new transaction. Suppose the original Required transaction rolls back, the RequiresNew transaction will still be persited.

Mandatory

  • If the client is running within a transaction and invokes the enterprise bean’s method, the method executes within the client’s transaction;
  • If the client is not associated with a transaction, the container throws the TransactionRequiredException.
Use the Mandatory attribute if the enterprise bean’s method must use the transaction of the client.

Supports

  • If the client is running within a transaction and invokes the enterprise bean’s method, the method executes within the client’s transaction;
  • If the client is not associated with a transaction, the container does not start a new transaction before running the method.


NotSupported

  • If the client is running within a transaction and invokes the enterprise bean’s method, the container suspends the client’s transaction before invoking the method. After the method has completed, the container resumes the client’s transaction;
  • If the client is not associated with a transaction, the container does not start a new transaction before running the method.
Use the NotSupported attribute for methods that don’t need transactions. Because transactions involve overhead, this attribute may improve performance.

Never

  • If the client is running within a transaction and invokes the enterprise bean’s method, the container throws a RemoteException.
  • If the client is not associated with a transaction, the container does not start a new transaction before running the method.


Spring Transaction Propagation

  • Required
  • Requires_New
  • Mandatory
  • Supports
  • NotSupported
  • Never
  • Nested

Required

If a transaction exists it's used otherwise a new one i started

Requires_New

Active transactions are suspended and a new one started. If none transaction exists a new transaction is started. This new transaction is independent from the suspended transaction, it will be committed or rolled back independent from the outer transaction. The new transaction has its own isolation scope,and locks. The outer transaction is suspended at the beginning of the inner one, and will be resumed once the inner one has completed.

Mandatory

If there's no active transaction a RemoteException is thrown.

Supports

If a transaction exists it's used otherwise execution is done not transactional.

NotSupports

If a transaction exists, it will suspend any active transaction.. Does not support execution within an active transaction.

Never

If a transaction exists a RemoteException is thrown. Otherwise execution is non transactional.

Nested

It starts a "nested" transaction, which is a true subtransaction of the existing one. What will happen is that a savepoint will be taken at the start of the nested transaction. Íf the nested transaction fails, we will roll back to that savepoint. The nested transaction is part of the outer transaction, so it will only be committed at the end of of the outer transaction.
Nested transactions essentially allow to try some execution subpaths as subtransactions: rolling back to the state at the beginning of the failed subpath, continuing with another subpath or with the main execution path there - all within one isolated transaction, and not losing any previous work done within the outer transaction.
(Juergen Hoeller)

It can only be used with a DataSourceTransactionManager and a JDBC3 driver. It uses save points in order to be able to rollback some part of a transaction.

No comments :

Post a Comment

Suggested Pages