Reputation: 165
I have a spring boot service class method which is annotated with @Transactional, where I will need to commit the transaction to MYSQL DB and then execute the finally block of code which is to push the entity to a Redis queue. Below is my code snippet:
@Transactional
public void save(Entity t) {
try {
entityRepository.save(t);
} catch(Exception ex) {
log.error("Error while executing transaction",e);
} finally {
//code to push to redis
}
}
My doubt is whether the transaction will be committed and the finally block will be executed? Or the @Transactional will complete all the transaction only after the finally block completes. If it is the latter, how do I ensure that my transaction is committed before pushing to Redis?
Please advice.
NOTE: entityRepository is a class which extends CrudRepository
Upvotes: 0
Views: 2941
Reputation: 1010
The transaction will be commited only after the finally block.
For the use case mentioned look at: Creating a post commit when using transaction in Spring
Register a TransactionSynchronization with an afterCommit or afterCompletion.
Upvotes: 2