my confusion related to this matter is that how we can use a previously created transaction? or in other words how many threads a transaction could be related to?
Asked
Active
Viewed 4.4k times
1 Answers
31
A transaction can be related to only one thread in spring. Well, with some effort you can make it a long-running transaction, but that's an anti-pattern afaik.
REQUIRES_NEW
means that whenever the program flow enters the annotated method, a new transaction will be started regardless of any existing transaction.
REQUIRED
means that an existing transaction will be reused, or if there's no existing transaction a new one will be started.

Bozho
- 588,226
- 146
- 1,060
- 1,140
-
thanks. is it the other way around as well? meaning could a thread have more than a transaction or only one? and is it the same in frameworks other than spring? – meisam Apr 26 '11 at 12:04
-
one thread can have more than one transaction. I don't know for other frameworks, but I supposed it's the same. – Bozho Apr 26 '11 at 12:05
-
4But what happens with existing transaction when `a new transaction will be started regardless of any existing transaction`? It will be commited immidiately before new transaction starts? Or it will be rollbacked? Or will be two transactions simultaneously? If yes, then at which points each of them will be commited/rollbacked? At end of each annotated method corresponding to each transaction? – Ruslan Stelmachenko Jan 12 '16 at 19:36
-
4**Spring documentation** says: "*Create a new transaction, and suspend the current transaction if one exists. Analogous to the EJB transaction attribute of the same name.*" And **Java EE documentation** says: "*If a client calls with a transaction context, the container suspends the association of the transaction context with the current thread before starting the new transaction and invoking the method. The container resumes the suspended transaction association after the method and the new transaction have been completed.*" – franta kocourek Oct 07 '16 at 07:21