r2dbc / r2dbc-spi

Service Provider Interface for R2DBC Implementations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Refine `Connection.setTransactionIsolationLevel` documentation

rusher opened this issue · comments

While reading the specification, I'm not completly sure what are the exact behavior of Connecton.setTransactionIsolationLevel :
must transaction isolation be the default state when creating a transaction, or must that apply only for the next transaction.

JDBC Connection.setTransactionIsolation(int) indicates:

Attempts to change the transaction isolation level for this Connection object to the one given

Every transaction will default to newly the transaction isolation value.

R2DBC Connection.setTransactionIsolationLevel(IsolationLevel) :

Configures the isolation level for the current transaction.

https://github.com/r2dbc/r2dbc-spi/blob/main/r2dbc-spec/src/main/asciidoc/transactions.adoc doesn't clarify that point.

This differ from JDBC, and I think that's an error, expecting that Connection.beginTransaction(TransactionDefinition) to set a transaction isolation for the scope of the transaction, and Connection.setTransactionIsolationLevel(isolationLvl) to set default transaction level for the connection. (postgresql implementation does set isolation for connection, not only next transaction)

Could you confirm the expected behavior ?

Thanks for bringing this up. The description is indeed a bit imprecise as setting the isolation level applies the isolation level to the current connection and the state in which it resides.

When a transaction is in progress, it would change the isolation level for the current transaction (although we define vendor-specific behavior in the spec). When the connection has no ongoing transaction (auto-commit mode or auto-commit disabled and no transaction started or the previous transaction was cleaned up), then the isolation level is applied to each transaction that is started.

It is not specified how setting the isolation level during a transaction affects new transactions after the current transaction is stopped. It seems that some databases reset the isolation level to the level that was set before the transaction was started.

Going forward, calling Connection.begin(TransactionDefinition) should ideally apply transaction attributes such as the isolation level only to the transaction that is being started and it should not leak these attributes to every transaction that is started later.

For example, the SQL Server driver captures the currently set isolation level on Connection.begin(TransactionDefinition) to set it on commit/rollback.

Does that make sense?

Agree : to resume :

  • for Connection.setTransactionIsolationLevel if not in a transaction, the isolation level must be the new default isolation for all new transactions. Vendor specific: If in a transaction, can change the current transaction isolation level (MySQL/MariaDB doesn't support this kind of feature)
  • for Connection.beginTransaction isolation level must apply to the new transaction only.