• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

EJB 3, Transaction question

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a very concise and short question just to clarify one concept.

What happend if in the middle of a EJB transaction-REQUIRED method, a RequiresNew-method in another EJB is called, and this RequiresNew-method happens to throw a System Exception ? ... and what happend if it throws an application exception ? .. I mean, what happend with the transaction of the first calling method ?, if it rolls back, as i think it should ,,, whats the difference, from the developers view, between a REQUIRED-method and a RequiresNew-Method ? ... I know the theory ... it must start a new transaction ... but ... whats the difference if it do not start a new one, and just is included in the transaction scope of the calling method ?

Hope my english is not too bad,,,

Thanks for your help,

Camilo Morales,
SCJP 5
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What happend if in the middle of a EJB transaction-REQUIRED method, a RequiresNew-method in another EJB is called, and this RequiresNew-method happens to throw a System Exception ?



B1 - with Tx1 calls B2 with Tx2 (new). And then B2 throws System Exception.
Since the Container manages the System Exception it will :

1. Log the exception
2. Rollback Tx2
3. Discard the instance of B2
and throw EJBException to B1 with Tx1.

Now it is upto B1 with Tx1 how to handle the EJBException. If it doesn't catch and ignore it then an EJBException being raised from B1 would result in the same sequence of steps taken by container as above. i.e Tx1 would also rollback.


.. and what happend if it throws an application exception ? ..



For Application Exception - if the exception is marked with the flag rollback=true or if B2 marked the Tx2 for rollback using setRollbackOnly then the container rollback the Tx2 and forward the application exception.

It is upto the Bean B1 to either eat up the application exception and continue to work in Tx1 or forward it back to the calling client i.e rethrow Application Exception - In this case - the behavior of the container would be same as worked for B2/Tx2.


See - if the Tx is same for bean B1 and B2 - then the container starts to take action in rollback/commit in the bean which actually started the Tx. Otherwise container propagates exception (and/or) logs exptn,discards the instance only.

But if Tx is different for both beans then actions are taken per bean where Tx started and then Exception propagated to the caller.

The one who starts the Tx has the control - that is the difference...

Regards,
Shivani
 
Camilo Morales
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
That new kid is a freak. Show him this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic