13

I have a question about Spring transaction propagation.

Suppose I use @Transactional(propagation = Propagation.REQUIRED) to annotate a method m1(). When execution logic enters m1(), if there is already a transaction, m1() will use that one. When after m1(), what about the transaction? It will end or still remain open? (if I call m1() in another method, and after the invocation there is still other things to do).

In summary, I want to know when exiting an annotated method, the transaction ends or still remains open?

Great thanks.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Just a learner
  • 26,690
  • 50
  • 155
  • 234

1 Answers1

24

Propagation.REQUIRED (documented here) will create a new transaction (if none exists for the current thread), or will join an existing transaction (if one exists).

When the method exits, then the transaction will be completed (if entering the method caused a transaction to be created), or will leave the transaction open (if a transaction already existed when the method was entered). In other, words, it's symmetrical, and will leave the thread's transactional state in the same state it was before the method was entered.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Hi staffman, what about other propagation level with regard to my question? – Just a learner May 02 '10 at 16:56
  • @Yousui: The various propagation levels, and their effects, are documented in the link I posted. – skaffman May 02 '10 at 16:58
  • Thank you staffman, after reading your answer carefully and do some testing, I have understood the spring transaction propagation policy. Great thanks for your help. – Just a learner May 02 '10 at 17:14
  • @Yousui Could you post the scenarios you tested out? I'm facing difficulties understanding the finer intricacies between REQUIRED vs REQUIRES_NEW. My transaction boundaries are defined separately, with each operation called from an external flow, so I should essentially be seeing the operations sequentially, but I'm still running into problems. – anuragw Dec 27 '12 at 13:31