pgjdbc / r2dbc-postgresql

Postgresql R2DBC Driver

Home Page:https://r2dbc.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CoroutineCrudRepository updateAll() function starts its own transaction instead of using existing one

MaxBeauchemin opened this issue · comments

Bug Report

Calling the updateAll() function on a CoroutineCrudRepository that is annotated with @Transactional doesn't re-use the existing transaction, instead it appears to start / commit its own transaction, and this can lead to rollbacks if its dependent on the uncommitted changes of the parent transaction

Versions

  • Driver:
  • Database: Postgres 14.6-alpine
  • Java: 17
  • OS: MacOS Monterey 12.3 (21E230)

Current Behavior

The function creates a row in a table, and then updates existing items to reference that row (with a foreign key). The entire transaction is rolled back due to a foreign key constraint violation, even though running the same code without the foreign key creates valid results in the database

  • Begin Transaction
    • Validations
    • Begin Transaction
      • Gets Items to Update
      • Commit Transaction
    • Insert new row
    • (updateAll) Begin Transaction
    • (updateAll) Update Entities
    • (updateAll) Commit Transaction
    • (updateAll) Rollback Transaction

Steps to reproduce

Write a function (transactional) that creates an entity and saves it, then uses the generated ID to update existing entities with a foreign key. Call updateAll().collect() to perform the update

Expected behavior/code

  • udpateAll() should return open transaction

Possible Solution

  • calling update() on a loop instead of updateAll() seems to fix the issue

Looks like that's not a driver problem, but a spring (data) problem.

Please file a ticket along with a reproducer at https://github.com/spring-projects/spring-data-relational/issues