3 Replies Latest reply on Sep 15, 2011 2:26 AM by Wolf-Dieter Fink

    How to detect lost database connection using injected EntityManager?

    Josh Broadway Newbie

      Hi all,

       

      I'm trying to build some reliability and robustness into an application we're running on JBoss AS 6.0.0 and part of this involves being able to handle the loss of a connection to a database on a remote server due to a server failure or network failure. We're using injected EntityManagers to manage the connection to the database:

       

          /***

           * Allows access to persistent data.

           */

          @PersistenceContext

          private EntityManager em;

       

      I've tried using the EntityManager.isOpen method but when I try that I get an error stating that isOpen can't be used with injected instances of EntityManager.

       

      My current solution is to catch RuntimeExceptions (or PersistenceExceptions) when they get thrown by the EntityManager but I'm finding that it can take quite some time before the EntityManager actually throws the exception, leading to long delays in the user interface.

       

      Does anyone know if there's a better way to quickly detect that there's an error with the database connection and handle it appropriately to enhance robustness and provide a better user experience?

       

      Any replies would be greatly appreciated.

       

      Thanks,

      Josh.