• Post Reply Bookmark Topic Watch Topic
  • New Topic

What Exception if database down  RSS feed

 
Adrian Bates
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks for the clear and complete reply Roger. Now I understand better.

Adrian
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!