• 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
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

How to Catch Exception

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using EJB3. As usual, i'll use the EntityManager's API to do CRUD operation, such as, em.persist(), em.merge() etc.

So now I want to catch exception while performing em.persist() and do my own thing.

If I used @TransactionManagement(TransactionManagementType.BEAN) I can handle exception below show my code.




Problem is if I use @TransactionManagement(TransactionManagementType.CONTAINER) I can't catch exception, I used same code( display above), how can I overcome this problem.

If you can provide more details about exception handling in EJB3( reference link) it's more helpful to me.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can handle/catch exceptions in BMT or CMT Enterprise Beans.
See the links below more about transactions and handling of exceptions:

http://java.sun.com/javaee/5/docs/tutorial/doc/bncih.html
http://java.sun.com/javaee/5/docs/tutorial/doc/bnbpj.html

And I recommend you check the specs:

http://jcp.org/aboutJava/communityprocess/final/jsr244/index.html


Best Regards!
 
Rodrigo Lazoti
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
duplicate message. sorry...
 
Greenhorn
Posts: 15
Eclipse IDE Firefox Browser Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.


Thanks Rodrigo for information.

I add the following items:

- In EJB 3 in Action section 6.2.5 Transaction and exception handling.
- In Entreprise JavaBeans 3.0 (O┬┤Relly) Section 16.6 Exceptions and Transactions.

By.
 
dinesh thalis
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read this tutorials but still I'm fail



I'm using container manager transaction and persist account details. if some body try to persist incorrect account information I want to give customer exception(message) instead of system generated message. Above code show that but this not work. please help me how to it?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With CMT, the actual queries are fired on transaction completion, i.e. after the method exits. So the catch block will never be able to catch such exceptions which happen within a transaction synchronization callback. You'll see the difference if you add a em.flush after the em.persist. That way, the queries will be fired immediately instead of waiting for tx synchronization:

 
dinesh thalis
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below show how I try to handle this issue. Is it write or wrong. Using this way I can succeed some extend, Is there any suggestion regarding this.

 
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic