I want to register a global transaction before commit event handler on Application startup. What is the best way to do this?
I tried @TransactionalEventListener annotation but this requires a call to be done to this method inside every transaction. I want to register a global handler.
Edit: I have found a working solution but still am wondering if there is something simpler.
My solution:
@Primary
@Order(Int.MIN_VALUE)
@EachBean(DataSource::class)
@TypeHint(DataSourceTransactionManager::class)
class AuditedDataSourceTransactionManager(
dataSource: DataSource
) : DataSourceTransactionManager(dataSource) {
override fun doCommit(status: DefaultTransactionStatus<*>?) {
if (status?.isReadOnly == false) {
// do my stuff
}
super.doCommit(status)
}
}