1

I using R2bc in Spring Webflux project with Kotlin. It is working nicely. But I have this method

@Component
class UserRepository(private val client: DatabaseClient, private val operator: TransactionalOperator) {

    suspend fun updateUser(user: User, value: String): Int {

        client.execute("INSERT INTO log(user_id, activity) VALUES (:user_id, :activity)")
              .bind("activity", user.activity)
              .bind("user_id", user.id)
              .fetch()
              .awaitRowsUpdated()

        return client.execute("UPDATE users SET value = :value WHERE id = :id")
                     .bind("value", value)
                     .bind("id", user.id)
                     .fetch()
                     .awaitRowsUpdated()
}

This method is working but I would like to use DB transaction. Is it supported in Kotlin.

4
  • why are you using the database client and not spring-data repositories? Commented Feb 15, 2020 at 20:12
  • Because I need to manage several entities in one method. This one is much simplified version there are few that are more involved. But we do use R2BC repo in most simpler use cases. Commented Feb 15, 2020 at 21:10
  • because this awaitRowsUpdated is blocking Commented Feb 15, 2020 at 21:34
  • Even if you call from suspended method. I don’t think so can you show me any examples. Commented Feb 15, 2020 at 22:15

1 Answer 1

6

Ok I found the solution I am posting it here so that other can benefit from it. All I had to do was.

@Component
class UserRepository(private val client: DatabaseClient, private val operator: TransactionalOperator) {

    suspend fun updateUser(user: User, value: String) =

        operator.executeAndAwait {

            client.execute("INSERT INTO log(user_id, activity) VALUES (:user_id, :activity)")
                  .bind("activity", user.activity)
                  .bind("user_id", user.id)
                  .await()

            client.execute("UPDATE users SET value = :value WHERE id = :id")
                  .bind("value", value)
                  .bind("id", user.id)
                  .await()
        }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.