Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing database transactions manually in a Spring/Hibernate environment

We've got a Spring based web application that makes use of Hibernate to load/store its entities to the underlying database.

Since it's a backend application we not only want to allow our UI but also 3rd party tools to manually initiate DB transactions. That's why the callers need to

  1. Call a StartTransaction method and in return get an ID that they can refer to
  2. Do all DB relevant calls (e. g. creating, modifying, deleting) by referring to this ID to make clear which operations belong to the started transaction
  3. Call the CommitTransaction method to signal to our backend that the transaction can be committed now (or in the negative case RollbackTransaction will be called)

So keeping in mind, that all database handling will be done internally by the Java persistence annotations, how can we open the transaction management to our UI that behaves like a 3rd party application that has no direct access to the backend entities but deals with data transfer objects only?

like image 797
Lars Blumberg Avatar asked Feb 22 '11 09:02

Lars Blumberg


People also ask

How do you handle transactions in hibernate?

Transaction Interface in Hibernate In hibernate framework, we have Transaction interface that defines the unit of work. It maintains abstraction from the transaction implementation (JTA,JDBC). A transaction is associated with Session and instantiated by calling session.

How do you define transaction management for Spring hibernate integration?

To perform transaction management within the database using Spring Framework with Hibernate and Annotations, we need an access to the source code to edit it and add @Transactional Annotation to class and methods involved in performing the transaction operations.


1 Answers

From the Spring Reference: Programmatic transaction management

like image 91
Sean Patrick Floyd Avatar answered Sep 27 '22 18:09

Sean Patrick Floyd