• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

question about EJB transaction

 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An EJB business method performs an operation that throws a checked exception. The bean cannot
recover from this checked exception and should rollback. The bean will be deployed using
container-managed transaction demarcation. Which implementation is the BEST?

A. public void businessMethod() { try { // operation throwing SomeCheckedException goes here }
catch (SomeCheckedException ae) { throw new EJBException(ae); } }

B. public void businessMethod() { try { // operation throwing SomeCheckedException goes here }
catch (SomeCheckedException ae) { context.setRollbackOnly(); throw new EJBException(ae); } }

C. public void businessMethod() throws EJBException { try { // operation throwing
SomeCheckedException goes here } catch (SomeCheckedException ae) { throw new EJBException(ae); }
}

D. public void businessMethod() throws EJBException { try { // operation throwing
SomeCheckedException goes here } catch (SomeCheckedException ae) { context.setRollbackOnly();
throw new EJBException(ae); } } }}
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is A
Debanjana
 
Timber Lee
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think D is correct.
because:
1.transaction needs to be rollbacked, so context.setRollbackOnly() should be called.
2.businessMethod() should return a exception to caller, so throw new EJBException(ae) should be executed.
but, caller gets a EJBException, which type it handles the EJBException as, system Exception or application Exception?
I get confused.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Timber,
The reason why it's A is because javax.ejb.EJBException is a RuntimeException, which are handled differently by the EJB container.
When an EJB throws a RuntimeException, unlike checked exceptions, the container is forced to automatically rollback the transaction (whether you want it or not...). You can also call setRollbackOnly(), but it is unnecesary (thus B and D are false)
The rest is between A and C. As RuntimeException are not required to be defined as part of the throws of the method signature (Java stuff), C is not the best answer.
Eduard
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic