Spring Boot Hibernate efficient transaction management

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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.


@Transactional


@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!!




1 Answer
1



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


@Transactional


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.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard