The isolation level cannot be set/passed to the r2dbc connection after upgrading to Spring Boot 3 and updating r2dbc.
This can be reproduced with the following test (the second exception is thrown):
@Transactional(isolation = Isolation.SERIALIZABLE)
public Mono<Void> runAndEnsureSerializable() {
return Mono.deferContextual(ctx -> {
var transactionContext = ctx.get(TransactionContext.class);
if (transactionContext.getCurrentTransactionIsolationLevel() != Isolation.SERIALIZABLE.value()) {
throw new RuntimeException("Expected isolation level to be SERIALIZABLE");
}
return db.inConnection(connection -> {
if (connection.getTransactionIsolationLevel() != IsolationLevel.SERIALIZABLE) {
throw new RuntimeException("Expected isolation level to be SERIALIZABLE");
}
return Mono.empty();
});
});
}
and the following configuration:
@Bean
public ConnectionFactory connectionFactory(PostgreSQLContainer postgres) {
return new ConnectionPool(ConnectionPoolConfiguration.builder()
.initialSize(0)
.validationDepth(ValidationDepth.REMOTE)
.connectionFactory(ConnectionFactories.get(ConnectionFactoryOptions.builder()
.option(DRIVER, "postgresql")
.option(HOST, postgres.getHost())
.option(PORT, postgres.getFirstMappedPort())
.option(USER, postgres.getUsername())
.option(PASSWORD, postgres.getPassword())
.option(DATABASE, postgres.getDatabaseName())
.build()))
.customizer(builder -> builder.releaseHandler(Connection::rollbackTransaction))
.build());
}
````
We are using:
The problem seems to be that autocommit is switched in the `R2dbcTransactionManager` in spring-r2dbc:6.0.4 but not in spring-r2dbc:5.3.24:
spring-r2dbc:6.0.4:
```java
connectionMono.flatMap(con -> switchAutoCommitIfNecessary(con, transaction)
.then(Mono.from(doBegin(definition, con)))
.then(prepareTransactionalConnection(con, definition))
spring-r2dbc:5.3.24:
connectionMono.flatMap(con -> prepareTransactionalConnection(con, definition, transaction)
.then(Mono.from(con.beginTransaction()))
.then(prepareTransactionalConnection(con, definition))
Any ideas?
Comment From: jhoeller
The isolation level is being passed through R2DBC 1.0's TransactionDefinition
in the begin call now. This should apply in any case, even with an explicit auto-commit off switch before. @mp911de any insight into the impact of the call sequence here?
Comment From: jhoeller
This should finally be fixed through #30508 for the upcoming 6.0.10 release. The auto-commit handling is implicit with beginTransaction
in R2DBC 1.0 now, so we have removed our custom auto-commit check completely.