Class ejbexception in java

Chapter 10 - Exceptions

    Class ejbexception in java

  • Exceptions

    Class EJBException:

                   •  java.lang.Object

                   •  java.lang.Throwable

                   •  java.lang.Exception

                   •  java.lang.RuntimeException

                   •  javax.ejb.EJBException

    There are two types EJB Exception:

                   •  Application Exception - If business rule is voilated 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.

                   •  When Application Exception occurs, ejb container intercepts the exception but returns the same to the client as it is. It does not roll back the transaction unless it is specified in code by EJBContext.

                   •  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 client

                   •  Handling Application Exception

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

     @Stateless
    public class LibraryPersistentBean implements LibraryPersistentBeanRemote {

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

    Handling System Exception

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

    @Stateless
    public class LibraryPersistentBean implements LibraryPersistentBeanRemote {

    ...

    public 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;
    }
    ...
    }

    Unchecked Exceptions

              When a java.lang.RuntimeException is thrown from the myClientEjbMethod() method, the container will

              1.  roll back the transaction

              2.  discard the EJB Bean instance

              3.  Throw a javax.ejb.EJBException to the client.

    Throwable unrollException(Throwable exception,
      Class expected){ while(exception != null && exception != exception.getCause()){ if(expected.isInstance(exception)){ return exception; } exception = exception.getCause(); } return null; } 
    Checked Exceptions

              When a java.lang.Exception is thrown from the EJB method,it declare method's signature with the throws keyword:

              1. the transaction gets commited

              2. the exception is rethrown to the client

                   •  BusinessValidationException extends java.lang.Exception

                   •  BusinessLogicException extends java.lang.Exception

              if the transaction has to be rolled back, or the flow can be continued, and maybe throw the exception at the end of the method to indicate some problem happened during processing but it actually executed successfully.

              To force the rollback manually for an ApplicationException, before throwing it,then it reference to the EjbContext, and call EjbContext.setRollbackOnly().

    Example:

    Date getDateFromStringEjbMethod(String yearString){
      SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY");
      try{
        return dateFormat.parse(yearString);
      } catch(ParseException e){
        throw new EJBException(e);
      }
    }
    
    Business logic exceptions

              1.  Inherit your BusinessValidationException from java.lang.RuntimeException: Transaction is rolled back automatically, but unroll the exception on client side, because it will be wrapped into an EJBException

              2.  Inherit BusinessValidationException from java.lang.RuntimeException and this class with @ApplicationException(rollback=true)

              3.  Inherit from java.lang.Exception and annotate it with @ApplicationException(rollback=true)

              4.  with rollback=false attribute for problems where the transaction can be continued/committed, but identify the user that there a problem throwing exceptions from EJB interceptors

    public class AuthorizationInterceptor {
    
      Logger log = Logger.getLogger(getClass().getName());
    
      @AroundInvoke
      private Object authorize(InvocationContext ic) throws Exception{
        // ... some other logic for authorization
    
        if (!allowedMethods.contains(ic.getMethod().getName())){
          log.info("Authorization failed. Preparing to throw exception");
          throw new AuthException("Authorization failed for method " +
                    ic.getMethod().getName());
        }
    
        return ic.proceed();
      }
    }
    

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy