我正在尝试使用 spring-data-r2dbc 存储库结合 TransactionalDatabaseClient 来实现事务:
class SongService(
private val songRepo: SongRepo,
private val databaseClient: DatabaseClient
){
private val tdbc = databaseClient as TransactionalDatabaseClient
...
...
fun save(song: Song){
return tdbc.inTransaction{
songRepo
.save(mapRow(song, albumId)) //Mapping to a row representation
.delayUntil { savedSong -> tdbc.execute.sql(...).fetch.rowsUpdated() } //saving a many to many relation
.map(::mapSong) //Mapping back to actual song and retrieve the relationship data.
}
}
}
我目前有@Configuration
一个@EnableR2dbcRepositories
从AbstractR2dbcConfiguration
. 在这里,我重写了databaseClient
返回一个TransactionalDatabaseClient
. 这应该与 SongService 类中的实例相同。
在仅订阅和打印的测试中运行代码时,我得到org.springframework.transaction.NoTransactionException: ReactiveTransactionSynchronization not active
并且不返回关系数据。
但是,当使用项目 Reactors stepverifier 时,我得到java.lang.IllegalStateException: Connection is closed
. 同样在这种情况下,不返回关系数据。
只是为了记录,我已经看到了https://github.com/spring-projects/spring-data-r2dbc/issues/44