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




When the propagation setting is PROPAGATION_REQUIRED, a logical transaction scope is created for each method upon which the setting is applied. Each such logical transaction scope can determine rollback-only status individually, with an outer transaction scope being logically independent from the inner transaction scope. Of course, in case of standard PROPAGATION_REQUIRED behavior, all these scopes will be mapped to the same physical transaction. So a rollback-only marker set in the inner transaction scope does affect the outer transaction's chance to actually commit (as you would expect it to).

enter image description here



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

Consider this code...

class Service {
    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

  • 1
    Too Awesome man, thanks for the great guidance.. – Java Curious ღ Feb 26 '14 at 4:40
  • 5
  • 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. – Kurapika Nov 17 '17 at 13:32
  • 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. – Brad Nov 20 '17 at 10:49
  • if doSomething() invokes a another nested method without any @transactional will this same transaction will apply to that invoking method as well? – thisarattr Feb 6 '18 at 5:45

In Spring applications, if you enable annotation based transaction support using <tx:annotation-driven/> and annotate any class/method with @Transactional(propagation=Propagation.REQUIRED) then Spring framework will start a transaction and executes the method and commits the transaction. If any RuntimeException occurred then the transaction will be rolled back.

Actually propagation=Propagation.REQUIRED is default propagation level, you don't need to explicitly mentioned it.

For further info : http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations


To understand the various transactional settings and behaviours adopted for Transaction management, such as REQUIRED, ISOLATION etc. you'll have to understand the basics of transaction management itself.

Read Trasaction management for more on explanation.

Your Answer

