user3386594
user3386594

Reputation: 19

Spring Boot Hibernate efficient transaction management

In a spring boot application integrated with Hibernate JPA. What are different and most effective ways for transaction management and what are the key points to consider for various scenarios.

At this point, we are using @Transactional annotation on our service layer calls from Controller layer; service layer consequently performing various read/ write DAO calls. DAO methods with @Transactional.

In must straight forward and basic scenarios, this serves us well where we are operating on a single entity in a single service layer call and we want the complete set of operations being wrapped up in a singe transaction.

But what if we want different behaviours on case by basis; for e.g. if we have a loop operating on a set of entities inside the service layer call, and although we want operations on every entity to be transactional, but if we don't wish the whole loop to be a single transaction. OR, if when operating on a particular entity, several operations need to be performed as part of a workflow, a subset of these workflows may be desirable to have no impact on the operations that were performed before them in case of an exception/ error scenario in the subset.

I know there are a lot of different ways to probably achieve this, but i was looking for a leads on best and most efficient and effective approaches.

Thanks in advance!!

Upvotes: 1

Views: 202

Answers (1)

Antoniossss
Antoniossss

Reputation: 32507

if we have a loop operating on a set of entities inside the service layer call, and although we want operations on every entity to be transactional,

Iterate in non-transactional and process interation item in transactional method, or interate in transactional and process in @Transactional with Propagation.REQUIRES_NEW

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html#isolation--

As a last result, handle transaction yourself. https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/transaction.html

OR, if when operating on a particular entity, several operations need to be performed as part of a workflow, a subset of these workflows may be desirable to have no impact on the operations that were performed before them in case of an exception/ error scenario in the subset.

typical long running transaction

Best would be to avoid that - eg creating draft of a contract in multiple steps - and at the end - serialization of that contract into proper entities and persisting it in single transaction.

Upvotes: 1

Related Questions