Hey everyone,
Are you aware of any good way to integrate
@Transactional
and
Either
?
Basically I'm using
Either<Throwable, Something>
as return type for many of my services and now I'd like to add transactional control through annotations instead of doing it manually (just recently integrated micronaut into the project)
Do you have any tips on how(if possible) to accommodate both ?
a
Alex Johnson
06/15/2021, 7:26 PM
I've used the TransactionManager itself and a wrapper around the either block to encompass the transaction. You can shove the transactional context within the coroutine scope to access it from lower layers as well. Something similar to:
Copy code
fun save(m: Model): Either<Error, Model> = tx { stuff.. }
p
Pedro Sena
06/15/2021, 7:28 PM
I see your point Alex, makes sense to me.
Did you have cases where a method you are already opening a transaction needs to call another method that is doing the same ? That is my concern and one of the selling points of the
@Transactional
for me as it simply reuses any ongoing transaction (unless specified otherwise)
d
dnowak
06/16/2021, 7:49 AM
Hi Pedro,
@Transactional
is based on thread locals -> it will not work with
suspend
functions as coroutines. You have to switch to
TransactionalOperator
in such case.
p
Pedro Sena
06/16/2021, 2:25 PM
Thanks for the info Dariusz, I had no idea about this limitation, I'll investigate further another option, thanks
s
simon.vergauwen
06/17/2021, 10:34 AM
This sounds very interesting, if anyone wants to collaborate on some code I'd be happy to help.
@Alex Johnson do you have that