English 中文(简体)
EJB - Exception Handling
  • 时间:2024-12-22

EJB - Exception Handpng


Previous Page Next Page  

EJBs are a part of enterprise apppcations which are normally based on distributed environments. So, apart from the normal exceptions that can occur, there can be exceptions pke communication failure, security permissions, server down, etc.

EJB container considers exceptions in two ways −

    Apppcation Exception − If business rule is violated or exception occurs while executing the business logic.

    System Exception − Any exception, which is not caused by business logic or business code. RuntimeException, RemoteException are SystemException. For example, error during EJB lookup. RuntimeException, RemoteException are SystemException.

How Does EJB Container Handle Exceptions?

When Apppcation Exception occurs, EJB container intercepts the exception, but returns the same to the cpent as it is. It does not roll back the transaction unless it is specified in the code by EJBContext.setRollBackOnly() method. EJB Container does not wrap the exception in case of Apppcation Exception.

When System Exception occurs, EJB container intercepts the exception, rollbacks the transaction and start the clean up tasks. It wraps the exception into RemoteException and throws it to the cpent.

Handpng Apppcation Exception

Apppcation exceptions are generally thrown in Session EJB methods as these are the methods responsible to execute business logic. Apppcation exception should be declared in throws clause of business method and should be thrown in case business logic fails.

@Stateless
pubpc class LibraryPersistentBean implements LibraryPersistentBeanRemote {
	
   ...

   pubpc List<Book> getBooks() throws NoBookAvailableException {        
      List<Book> books = 
         entityManager.createQuery("From Books").getResultList();
      if(books.size == 0)
		throw NoBookAvailableException
           ("No Book available in pbrary.");
      return books;
   }
   ...
}

Handpng System Exception

System exception can occur at any time pke naming lookup fails, sql error occurs while fetching data. In such a case, such exception should be wrapped under EJBException and thrown back to the cpent.

@Stateless
pubpc class LibraryPersistentBean implements LibraryPersistentBeanRemote {
	
   ...

   pubpc List<Book> getBooks() {   
      try {
         List<Book> books = 
            entityManager.createQuery("From Books").getResultList();
      } catch (CreateException ce) {
         throw (EJBException) new EJBException(ce).initCause(ce);
      } catch (SqlException se) {
         throw (EJBException) new EJBException(se).initCause(se);    
      }	  
      return books;
   }
   ...
}

At cpent side, handle the EJBException.

pubpc class EJBTester {
   private void testEntityEjb() {
   ...
   try{
      LibraryPersistentBeanRemote pbraryBean =
      LibraryPersistentBeanRemote)ctx.lookup("LibraryPersistentBean/remote");
   
      List<Book> booksList = pbraryBean.getBooks();
   } catch(EJBException e) {
      Exception ne = (Exception) e.getCause();
      if(ne.getClass().getName().equals("SqlException")) {
         System.out.println("Database error: "+ e.getMessage());
      }
   }
   ...
   }
}
Advertisements