- Notifications
You must be signed in to change notification settings - Fork 135
Description
class BatchReadOnlyTransactionImpl is an AutoClosable (via MultiUseReadOnlyTransaction->AbstractReadContext->ReadContext->AutoClosable)
The close() method always closes both the session and the transaction.
This is fine if a session has been created for this transaction, using batchReadOnlyTransaction(TimestampBound)
But a BatchReadOnlyTransaction can also be created from an existing shared BatchTransaction: batchReadOnlyTransaction(BatchTransactionId).
In this case, the Session is not created for this transaction, but is taken from the SessionID from the BatchTransactionId, and so it is a shared session so should not be closed when this transaction is closed.
Therefore the close() method should be fixed to only close the session when the session has been created for this transaction.
Environment details
- OS type and version: all
- Java version: all
- version(s): latest (6.17.4)
Steps to reproduce
- Use the sample code at https://cloud.google.com/spanner/docs/samples/spanner-batch-client#spanner_batch_client-java
- Modify the code so that each thread creates a new separate batchReadOnlyTransaction, and uses that to perform the read (so that a different channel is used for the read)
try (BatchReadOnlyTransaction readTxn = batchClient.batchReadOnlyTransaction(txn.getBatchTransactionId())) { try (ResultSet results = readTxn.execute(p)) { while (results.next()) { ... } } } ... - Run the code on a large enough table to get a reasonably partitioned transaction in a single process
- Watch the code fail as one thread uses a session that has been closed by another