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. RuntimeException, RemoteException are SystemException. For example, error during EJB lookup. RuntimeException, RemoteException are SystemException.
-> Does this means I need to use checked exceptions for my bussines logic ? Like this ?
private void checkConstraints(Object object) throws ValidationException{
Set<ConstraintViolation<Object>> constraintsAdress = this.getValidator().validate(object);
if(!constraintsAdress.isEmpty()){
String fullErrorConstraint = "";
for (ConstraintViolation<Object> constraint : constraintsAdress) {
fullErrorConstraint = fullErrorConstraint + constraint.getMessage() + "\n";
}
throw new ValidationException(fullErrorConstraint);
}
}
@Override
public long addCafe(Cafe cafe) throws ValidationException, DBException{
this.checkConstraints(cafe.getAddress());
for(FootballMatch footballMatch: cafe.getNextMatchesToWatch()){
this.checkConstraints(footballMatch);
}
this.checkConstraints(cafe);
this.getManager().persist(cafe);
return cafe.getCafeID();
}
but ...
An application exception does not automatically result in marking the transaction for rollback unless the ApplicationException annotation is applied to the exception class and is specified with the rollback element value true…
I don't fully understand it anymore ... Is it a good idea to use:
- @ApplicationException(rollback = true) ?
- And can you use this one then also for making unchecked exception ?
Thx in advance Cheers Tom