if some one can explain what this annotation do and when exactly we use it :
@Transactional(propagation=Propagation.REQUIRED)
Thanks
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.
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.
Enum Constant Summary. Support a current transaction, throw an exception if none exists.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With