Spring and Hibernate Transaction

UML Sequence Diagram Example

Spring application development framework for enterprise Java™ integrates Hibernate transaction management. Here we provide an example of UML sequence diagram which illustrates transaction management in relation to exception handling.

When some business method is called, it could be intercepted by Spring Transaction Interceptor. This behind the scene interceptor creates Hibernate session and starts Hibernate JDBC transaction, so that business method will run in the context of a new transaction.

Business method execution could complete (successfully or not) without throwing any exception, or by throwing some Java runtime (unchecked) exception or some business (checked) exception.

Spring and Hibernate transaction with exception handling shown as UML sequence diagram.

Spring and Hibernate transaction with exception handling shown as UML sequence diagram.

If business method did not throw any exception or thrown some business (checked) exception, transaction interceptor will try to commit transaction. When Hibernate JDBC transaction is flushed (to store data permanently), this operation might fail, e.g. because of some database constraint violation. In this case transaction will be rolled back (even though business method execution was successful).

If business method thrown some Java runtime (unchecked) exception, it is an indicator that business method failed, and transaction interceptor will rollback the transaction.

At the end Hibernate session will be closed by the Spring Transaction Interceptor.