File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to understand PROPAGATION_REQUIRED ?

 
Edward Chen
Ranch Hand
Posts: 798
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In documentation, it says: "PROPAGATION_REQUIRED: Support a current transaction, create a new one if none exists."; the problem that difination is trying to solve is like this :

Say, I define "update*", "insert*" as PROPAGATION_REQUIRED


So in line 2, it will NOT start a new transaction, it will use same transaction in line 1.

My understanding is correct ?

Thanks.
 
karimkhan pathan
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Edward,

If the propagation is set to required then

1.If the calling method is already in a transaction it will use the same transaction
2.If the calling method is in a non-transaction scope it will create new transaction .


Correct me if I'm wrong .


Regards,
Karim.
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karimkhan pathan wrote:Hi Edward,

If the propagation is set to required then

1.If the calling method is already in a transaction it will use the same transaction
2.If the calling method is in a non-transaction scope it will create new transaction .


Correct me if I'm wrong .


Regards,
Karim.


You are absolutely correct.

For the original poster, setting a propogation is about what happens from the outside coming in. Meaning is the caller already in a Transaction or not. In your example line one is the definition of your method, not a line of code that runs. So you call that method and what is inside that method will always run in a Transaction, either by a transaction created by the calling code, or a new transaction created by calling the method if the calling code is not running in a Transaction already.

Example, two classes A and B. A is calling a method on B. B's method is marked at REQUIRED. If A has already started a Transaction, then B will just join that Transaction and run within it. So if A or B causes the transaction to rollback, everything will rollback. If A has not started a Transaction, then the call to B will create a new Transaction and all the code in B will be within the Transaction, but all the code in A is running outside of a transaction.

-------------------B's method is set to REQUIRED
A------------------B
TX----------------Joins A TX
NO TX -----------Starts New TX


Mark
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic