Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Transactional(propagation=Propagation.REQUIRED)

if some one can explain what this annotation do and when exactly we use it :

@Transactional(propagation=Propagation.REQUIRED) 

Thanks

like image 703
Adil Avatar asked May 24 '12 14:05

Adil


People also ask

Is @transactional required?

if it's using proxy based configuration to declare and access to DAO layer, the method into DAO class must be annotated with @Transactional too. If you have added @Transactional to your service layer, there is no further requirement to also add @Transactional to the DAO methods being called within that transaction.

What is the transaction behavior of the propagation requires new mode?

REQUIRES_NEW behavior means that a new physical transaction will always be created by the container. The NESTED behavior makes nested Spring transactions to use the same physical transaction but sets savepoints between nested invocations so inner transactions may also rollback independently of outer transactions.

Which transaction propagation strategy supports a current transaction and executes non transactionally if none exists?

Enum Constant Summary. Support a current transaction, throw an exception if none exists.

What happens when a method annotated with @transaction propagation Requires_new is invoked?

The main difference between them is if a method in Spring Business Activity/DAO class is annotated with Propagation. REQUIRES_NEW, then when the execution comes to this method, it will create a new transaction irrespective of the existing transaction whereas if the method is annotated with Propagation.


1 Answers

If you need a laymans explanation of the use beyond that provided in the Spring Docs

Consider this code...

class Service {     @Transactional(propagation=Propagation.REQUIRED)     public void doSomething() {         // access a database using a DAO     } } 

When doSomething() is called it knows it has to start a Transaction on the database before executing. If the caller of this method has already started a Transaction then this method will use that same physical Transaction on the current database connection.

This @Transactional annotation provides a means of telling your code when it executes that it must have a Transaction. It will not run without one, so you can make this assumption in your code that you wont be left with incomplete data in your database, or have to clean something up if an exception occurs.

Transaction management is a fairly complicated subject so hopefully this simplified answer is helpful

like image 123
Brad Avatar answered Nov 07 '22 21:11

Brad