Exception handling

Exception handling is crucial for enterprise applications. Exceptions can arise in the middle of execution, such as a PersonCreationException in our example. Once an exception arises somewhere within the execution, the data will either be persisted in the database or the complete transaction will be rolled back, depending on the exception.

For example, let's say our createPerson() function throws PersonCreationException:

public class PersonCreationException extends Exception {
public PersonCreationException(String message) {
super(message)
}
}

Since PersonCreationException is a checked exception, the transaction will not be rolled back if the exception is thrown during the execution of the createPerson() function. These kinds of exceptions are commonly called application exceptions.

Application exceptions are checked exceptions that either have to be handled in a function, or declared to be thrown in the function declaration. That means that although the createPerson() function throws an exception, the person object would still be persisted in the database. This is shown in the following code:

@Stateless
class App {
@Inject
private lateinit var identityCreator: IdentityCreator

@PersistenceContext(unitName="local")
private lateinit var entityManager: EntityManager

@TransactionAttribute(TransactionAttributeType.REQUIRED)
fun createIdentity(inputData: InputData): Person throws PersonCreationException {
val person = identityCreator.createPerson(inputData)
entityManager.persist(person)
throw new PersonCreationException("exception occurred while
creating the person")
return person
}

fun findAllPerson(): List<Identity> {
return
entityManager.createNamedQuery(Queries.FIND_ALL_PERSON,
Person::class.java).resultList
}
}
}

If PersonCreationException were a runtime exception, the entire transaction would be rolled back. These exception types extend from the RuntimeException class and are commonly called system exceptions:

public class PersonCreationException extends RuntimeException {
public PersonCreationException (String message) {
super(message);
}
}
The application exception extends from the Java Exception class. If such an exception is thrown during the execution of EJB- or CDI-managed bean functions, the transaction will not be rolled back, as these kinds of exceptions are expected and the function either declares the exception to be thrown or it handles the exceptions.

The system exceptions are child classes of the Java RuntimeExeption class. If such an exception is thrown during the execution of a function, the transaction will be rolled back as these kinds of exceptions are not expected at runtime.

However, if we want to roll back the transaction once an exception arises during the execution of the function, we can use the @ApplicationException annotation with rollback = true. This makes the exceptions that occur to be treated as application exceptions, which causes the transaction to roll back:

@ApplicationException(rollback = true)
public class PersonCreationException extends RuntimeException {
public PersonCreationException (String message) {
super(message)
}
}

Now, if this PersonCreationException is thrown during the execution, the transaction will be rolled back and the person data would not be persisted into the database:

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
fun createIdentity(inputData: InputData): Person throws PersonCreationException {
val person = identityCreator.createPerson(inputData)
entityManager.persist(person)
throw new PersonCreationException("exception occurred while creating the person")
return person
}

Similarly, for the CDI-managed beans, the CDI doesn't take the @ApplicationException annotation into consideration in order to roll back the transaction. These beans can, however, be configured to use the @Transactional annotation to roll back on a specified exception type:

class IdentityCreator {

@Transactional(rollbackOn = PersonCreationException.class)
            fun createPerson(inputData: InputData): Person {
//..
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset