Reputation:
I have a @Component
class with @Transactional
annotation.
In onApplicationEvent
method I save same data in db and after that I call System.exit(0)
.
But data isn't saved and I want to check when the data is saved.
I use TransactionSynchronizationManager.isActualTransactionActive()
but always is true.
How I can check safe if a transaction is finished?
Upvotes: 1
Views: 1880
Reputation:
I resolve the problem with another service class where I save data in DB and this service is @Transactional
. And in @Component
class I called the service method.
Upvotes: 2
Reputation: 1907
1) Not sure it is latest, but you need a transaction interceptor, like that: https://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/Interceptor.html
and listen to afterTransactionCompletion(...)
2) you can simply wrap the DB calling method by try-catch and if calling method exited without exception - transaction is done. (in case you do not catch exceptions inside).
wrap your java code that calls the DB save like that:
try{
saveData()...
//transaction was OK here, I can System.exit
System.exit();
}catch(Exception e){
//transaction failed, I need so something
soSomething();
}
Upvotes: 0
Reputation: 7521
What kind of ApplicationEvent do you listen to? If it is a custom event produced by your own code (via ApplicationEventPublisher
) then you can put org.springframework.transaction.event.TransactionalEventListener
annotation on your event-handling method and it will be invoked after transaction is committed. (if event was published of course)
Upvotes: 0