Exception handling by R4R Team

The Hibernate entity manager generally raises exceptions which encapsulate the Hibernate core exception. Common exceptions raised by the EntityManager API are

1. IllegalArgumentException: something wrong happen

2. EntityNotFoundException: an entity was expected but none match the requirement

3. NonUniqueResultException: more than one entity is found when calling getSingleResult()

4. NoResultException: when getSingleResult() does not find any matching entity

5. EntityExistsException: an existing entity is passed to persist()

6. TransactionRequiredException: this operation has to be in a transaction

7. IllegalStateException: the entity manager is used in a wrong way

8. RollbackException: a failure happens during commit()

9. QueryTimeoutException: the query takes longer than the specified timeout (see 
javax.persistence.query.timeout - this property is a hint and might not be followed)

10. PessimisticLockException: when a lock cannot be acquired

11. OptimisticLockException: an optimistic lock is failing

12. LtExceptionockTimeou: when a lock takes longer than the expected time to be acquired (javax.persistence.lock.timeout in milliseconds)

13. TransactionRequiredException: an operation requiring a transaction is executed outside of a transaction

The HibernateException, which wraps most of the errors that can occur in a Hibernate persistence layer, is an unchecked exception. Note that Hibernate might also throw other unchecked exceptions which are not a HibernateException. These are, again, not recoverable and appropriate action should be taken.

Hibernate wraps SQLExceptions thrown while interacting with the database in a JDBCException. In fact, Hibernate will attempt to convert the exception into a more meaningful subclass of JDBCException. The underlying SQLException is always available via JDBCException.getCause(). Hibernate converts the SQLException into an appropriate JDBCException subclass using the SQLExceptionConverter attached to the SessionFactory. By default, the SQLExceptionConverter is defined by the configured dialect; however, it is also possible to plug in a custom implementation (see the javadocs for the SQLExceptionConverterFactory class for details). 

The standard JDBCException subtypes are:

1. JDBCConnectionException - indicates an error with the underlying JDBC communication.

2. SQLGrammarException - indicates a grammar or syntax problem with the issued SQL.

3. ConstraintViolationException - indicates some form of integrity constraint violation.

4. LockAcquisitionException - indicates an error acquiring a lock level necessary to perform the requested operation.

5. GenericJDBCException - a generic exception which did not fall into any of the other categories.

If the EntityManager throws an exception (including any SQLException), then we need to immediately rollback the database transaction, call EntityManager.close() (if createEntityManager() has been called) and discard the EntityManager instance. 

Certain methods of EntityManager will not leave the persistence context in a consistent state. No exception thrown by an entity manager can be treated as recoverable. Ensure that the EntityManager will be closed by calling close() in a finally block. Note that a container managed entity manager will do that for you. You just have to let the RuntimeException propagate up to the container.
Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!