113

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

@Transactional(propagation=Propagation.REQUIRED)

Thanks

Adil
  • 4,503
  • 10
  • 46
  • 63
  • 3
    Have you read this? http://static.springsource.org/spring/docs/2.5.x/reference/transaction.html#tx-propagation – Brad May 24 '12 at 15:00
  • 3
    Propagation.REQUIRED is the default propagation mode of Transaction, so you don't need to explicitly set it. – tibtof May 24 '12 at 15:02

4 Answers4

178

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

shellbye
  • 4,620
  • 4
  • 32
  • 44
Brad
  • 15,186
  • 11
  • 60
  • 74
  • 9
    If anyone is interested, I posted [a similar layman's answer comparing PROPAGATION_REQUIRES_NEW, PROPAGATION_NESTED, PROPAGATION_REQUIRED](http://stackoverflow.com/questions/25076718/spring-propagation-examples-in-laymans-terms/25083505#25083505) – Brad Sep 08 '14 at 15:02
  • 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. – TomasMolina 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 06 '18 at 05:45
  • Yes, so long as the same Thread is being used to execute a nested method (i.e. you don't create a new Thread explicitly). This is because Spring transactions are bound to the current Thread using ThreadLocal variables – Brad Feb 06 '18 at 13:46
71

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

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html

Guido
  • 46,642
  • 28
  • 120
  • 174
16

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

K. Siva Prasad Reddy
  • 11,786
  • 12
  • 68
  • 95
5

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.

Bitmap
  • 12,402
  • 16
  • 64
  • 91