• Post Reply Bookmark Topic Watch Topic
  • New Topic

Are UserTransactions propogated into other methods?  RSS feed

 
James Adams
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have several methods in a class which contain codes that should be run in the context of a transaction. These methods are being called by other methods of the class which create and begin a UserTransaction before making the method calls.

For example:

methodA() contains code which should be run in a transaction, but nowhere in the method is a transaction created or started.

methodB() creates and starts a UserTransaction, then calls methodA(). If methodA() completes successfully then the transaction is committed, otherwise the transaction is rolled back.


My question is whether or not the code in methodA() is in fact running within the transaction which was started in methodB(), as I assume. My worry is that perhaps the transaction is somehow suspended when methodA() starts running, and then resumes once methodA() returns. Can someone please enlighten me as to how this actually works, or perhaps point me to a good reference?

Thanks in advance for any help.


--James
 
James Adams
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW the methods I'm referring to in the original post are not part of an EJB. I know that I could control this behavior via the ejb-jar.xml if the methods in question were part of an EJB, but I'm wondering about how this works outside of an EJB.


--James
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,


methodA() contains code which should be run in a transaction, but nowhere in the method is a transaction created or started.

methodB() creates and starts a UserTransaction, then calls methodA(). If methodA() completes successfully then the transaction is committed, otherwise the transaction is rolled back.

It works as you expected: methodA executes within the transaction started by methodB. However if methodA would have started a new transaction, then because J2EE doesn�t support nested transactions, your first scenario will become valid: the transaction will get suspended and methodA will run within a new transaction. The initial transaction will resume after the second one will end.
Regards.
 
James Adams
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin thanks for the confirmation -- now I can look elsewhere for my bug without worrying about a transaction related problem.


--James
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're very welcome James
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!