I recently started using Kotlins Result in my Springboot Kotlin project.
One thing I realised it that it doesnt work together with Springs @Transactional
annotation since we are wrapping errors in a Result.failure object rather than throwing them.
A bit of googling only lead me to a really old github issue:
https://github.com/spring-projects/spring-framework/issues/27323
where they deemed it not popular enough to integrate with spring..
Is there any decent workaround to this problem?
Besides injecting a transactionManager into any service that needs transaction management and manually rolling back like so:
fun upsert(): Result<Unit> {
val transactionTemplate = TransactionTemplate(transactionManager)
return transactionTemplate.execute { status ->
Result.catch {
multiple db operations here
}
.onFailure { status.setRollbackOnly() }
}!!
}