io.r2dbc.spi.Connection和PostgresqlConnection有createSavepoint
,releaseSavepoint
和rollbackTransactionToSavepoint
方法。我怎样才能通过拥有R2dbcTransactionManager
和来使用这些方法TransactionalOperator
?
我想创建一个尝试插入表的幂等服务,如果违反了唯一约束,则选择现有记录并继续
Mono.just(newOrder)
.flatMap(order -> orderRepository.save(order)
.onErrorResume(throwable -> orderRepository.findByUniqueField(newOrder.uniqueField))
.otherProcesses...
.as(transactionalOperator::transactional)
我current transaction is aborted, commands ignored until end of transaction block
从 PostgreSQL收到
我看到了这个答案https://stackoverflow.com/a/48771320/5275087但它的接缝autosave=always
不适用于 r2dbc
我想尝试类似的东西:
transactionalOperator.execute(reactiveTransaction -> {
GenericReactiveTransaction genericReactiveTransaction = (GenericReactiveTransaction) reactiveTransaction;
ConnectionFactoryTransactionObject txObject = (ConnectionFactoryTransactionObject) genericReactiveTransaction.getTransaction();
Connection connection = txObject.getConnectionHolder().getConnection();
return Mono.from(connection.createSavepoint(TRANSACTION_LEVEL_1))
.then(Mono.just(newOrder)
.flatMap(order -> orderRepository.save(order)
.onErrorResume(throwable ->
Mono.from(connection.rollbackTransactionToSavepoint(TRANSACTION_LEVEL_1))
.then(orderRepository.findByUniqueField(newOrder.uniqueField)))
));
不过R2dbcTransactionManager.ConnectionFactoryTransactionObject
是私教。
如何在不使用反射的情况下实现这一目标