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

What Exception if database down

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to write my first Entity EJBs. When I do a findByPrimaryKey I understand that I have to catch a FinderException as this is thrown if the primary key specified does not match a record. But what happens if the database is simply down?

Thanks in advance and sorry for such a noob question.

Adrian
 
Adrian Bates
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I have answered my own question.

A ObjectNotFoundException is thrown if the record does not exist. This is inherits from FinderException which is why catching FinderException hides the ObjectNotFoundException.

FinderException occurs when the database is down.

If anyone knows if this is correct or not can they let me know.

Thanks,
Adrian
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When an EJB container loses its connection to a database server, a system exception is thrown. ObjectNotFoundException indicates that the connection was up but the record was not found.
 
Adrian Bates
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Roger.

When you say a system error occurs. What exception? Is it the FinderException? How do I catch it? Or do I not need to?

Adrian
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adrian,

Usually the container will check if the connection is healthy, in order to get it from the pool. If the db is down it will throw a SQLException. I guess your code (assuming you use kind of DAO) might look similar to this:

Basically you won�t be able to return a connection and this will probably prevent you from starting the transaction.
Regards.
 
Adrian Bates
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.

This is all fine except that I am using entity beans as my database connection and therefore am not getting a connectionFactory etc.

[ July 15, 2005: Message edited by: Adrian Bates ]
[ July 15, 2005: Message edited by: Adrian Bates ]
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're using BMT, then you're ok. Check your connection pool definition and make sure that the container checks if the connection is healthy, before returning it from/back to the pool.
Regards.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you say a system error occurs. What exception? Is it the FinderException? How do I catch it? Or do I not need to?


A system exception is defined in the EJB spec an an exception which the client does not expect, and is either a RuntimeException or a RemoteException. It won't be a FinderException as this is an application exception, ie a checked exception which is expected by the client.

(As a general rule, do not handle system exceptions in your bean code, just duck them to the Container. If you insist on handling these exceptions, make sure you rethrow them.)

Let's say that you had a connection but it's gone down. When the container discovers this and can't reestablish the connection, it may throw a system exception. This exception will be either a RemoteException (for a remote client) or an EJBException (for a local client). EJBException is a subclass of RuntimeException. Note that sometimes the container will throw a subclass of RuntimeException or EJBException. So, you may need to code your client to handle the subclassed exceptions.

Or your bean code will receive an SQLException. In this case, you will want to wrap and rethrow it as an EJBException.
 
Adrian Bates
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clear and complete reply Roger. Now I understand better.

Adrian
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic