- EJB - Packaging Applications
- EJB - Web Services
- EJB - Exception Handling
- EJB - Query Language
- EJB - Access Database
- EJB - Entity Relationships
- EJB - JNDI Bindings
- EJB - Security
- EJB - Transactions
- EJB - Blobs/Clobs
- EJB - Embeddable Objects
- EJB - Interceptors
- EJB - Dependency Injection
- EJB - Timer Service
- EJB - Callbacks
- EJB - Annotations
- EJB - Message Driven Beans
- EJB - Persistence
- EJB - Stateful Bean
- EJB - Stateless Bean
- EJB - Create Application
- EJB - Environment Setup
- EJB - Overview
- EJB - Home
EJB Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
EJB - Exception Handpng
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