Skip to content

Spanner: BatchReadOnlyTransaction closes shared sessions on close() #1651

@nielm

Description

@nielm

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

  1. OS type and version: all
  2. Java version: all
  3. version(s): latest (6.17.4)

Steps to reproduce

  1. Use the sample code at https://cloud.google.com/spanner/docs/samples/spanner-batch-client#spanner_batch_client-java
  2. 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()) { ... } } } ... 
  3. Run the code on a large enough table to get a reasonably partitioned transaction in a single process
  4. Watch the code fail as one thread uses a session that has been closed by another

Metadata

Metadata

Assignees

Labels

api: spannerIssues related to the googleapis/java-spanner API.priority: p2Moderately-important priority. Fix may not be included in next release.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions