EJB Exception Handling - EJB(Enterprise JavaBeans)

What is EJB Exception Handling?

EJBs are a component of enterprise applications which are usually based on distributed environment. So, separately from the standard exceptions that can occur, there can be exceptions like statement failure, security permissions, server down, etc.

EJB container considers exceptions in two ways −

  • Application 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. Runtime Exception, Remote Exception are System Exception. For example, error during EJB lookup. Runtime Exception, Remote Exception is System Exception.

How Does EJB Container Handle Exceptions?

When Application Exception occurs, EJB container intercepts the exception, but precedes the same to the client as it is. It does not roll back the operation unless it is particular in the code by EJBContext.setRollBackOnly () method. EJB Container does not wrap the exception in case of Application Exception.

When System Exception occurs, EJB container intercepts the exception, rollbacks the contract and start the clean up tasks. It wraps the exception into Remote Exception and throws it to the client.

Handling Application Exception

Application exceptions are commonly thrown in Session EJB methods as these are the methods responsible to execute business logic. Application exception should be stated in throws clause of business scheme and should be thrown in case business logic fails.

Handling System Exception

System exception can happen at any time like naming lookup fails, sol error occurs while attractive data. In such a case, such exception should be wrapped under EJBException and thrown back to the client.

At client side, handle the EJBException.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

EJB(Enterprise JavaBeans) Topics