Setting up a PoC using R2DBC, so I'm using `Corout...
# spring
e
Setting up a PoC using R2DBC, so I'm using
CoroutineCrudRepository
and just a simple
@RestController
. I'm trying to make sure I'm handling transactions properly. Using
@Transactional
isn't supported. I'm injecting a
TransactionalOperator
to my controller and using it like this:
Copy code
@PostMapping("/")
    suspend fun create(@Valid @RequestBody info: Info): ResponseEntity<Model> =
            doInTransaction { repo.save(convert(info)).created() }

    @GetMapping("/{id}")
    suspend fun read(@PathVariable id: Id): ResponseEntity<Model> = doInTransaction { repo.findById(id).okOrNotFound() }

    @GetMapping
    suspend fun readAll(): ResponseEntity<List<Model>> = doInTransaction { repo.findAll().toList().ok() }

    private suspend fun <T> doInTransaction(block: suspend () -> T): T =
            transactionalOperator.executeAndAwait { block() }
Is using
executeAndAwait
like this correct?
w
I had written the following
Copy code
import kotlinx.coroutines.reactor.awaitSingle
import kotlinx.coroutines.reactor.mono

transactionalOperator
    .transactional(mono(block = block))
    .awaitSingle()
👍 1