GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [Spanner] TransactionManager.close() not idempotent; API doc example is broken · Issue #3172 · googleapis/google-cloud-java

[Spanner] TransactionManager.close() not idempotent; API doc example is broken #3172

New issue
Copy link
New issue
Copy link
Closed
Closed
[Spanner] TransactionManager.close() not idempotent; API doc example is broken#3172
Copy link
Assignees
archiecobbs

Description

@archiecobbs
archiecobbs
opened on Apr 19, 2018
Issue body actions

I am using the new TransactionManager API and am getting mysterious "rollback can only be called if the transaction is in progress" exceptions.

I think I've tracked down the problem...

The API docs example shown with DatabaseClient.transactionManager() is this:

 long singerId = my_singer_id;
 try (TransactionManager manager = dbClient.transactionManager()) {
   TransactionContext txn = manager.begin();
   while (true) {
     String column = "FirstName";
     Struct row = txn.readRow("Singers", Key.of(singerId), Collections.singleton(column));
     String name = row.getString(column);
     txn.buffer(
         Mutation.newUpdateBuilder("Singers").set(column).to(name.toUpperCase()).build());
     try {
       manager.commit();
       break;
     } catch (AbortedException e) {
       Thread.sleep(e.getRetryDelayInMillis() / 1000);
       txn = manager.resetForRetry();
     }
   }
 }

However, this example usage is actually wrong.

The reason is that the implementation returned from DatabaseClient.transactionManager() is actually an AutoClosingTransactionManager (in SessionPool.java), which automatically close()s the transaction if commit() or rollback() is invoked. Sounds like a good idea, right?

The problem is that AutoClosingTransactionManager.close() is not idempotent:

    @Override
    public void close() {
      try {
        delegate.close();
      } finally {
        session.close();
      }
    }

If the transaction is closed twice, then the second time around the session is closed again, even though it has already been returned to the pool and possibly reassigned to another transaction. If so, this in turn causes that other transaction to fail on commit() with an exception "rollback can only be called if the transaction is in progress".

One fix for this is to document that a TransactionManager must only be closed exactly once, and fix your API javadoc example accordingly.

A much better and more robust fix would be to fix AutoClosingTransactionManager.close() to be idempotent. TransactionManager could then implement Closeable instead of AutoCloseable to denote this (if desired).

Activity

  1. archiecobbs commented on Apr 19, 2018

    @archiecobbs
    archiecobbs
    on Apr 19, 2018
    Author
    More actions

    Patch...

    diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java
    index 8c60e6a..3f21e28 100644
    --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java
    +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java
    @@ -199,6 +199,7 @@ final class SessionPool {
       private static class AutoClosingTransactionManager implements TransactionManager {
         final TransactionManager delegate;
         final PooledSession session;
    +    private boolean closed;
         
         AutoClosingTransactionManager(TransactionManager delegate, PooledSession session) {
           this.delegate = delegate;
    @@ -242,6 +243,10 @@ final class SessionPool {
     
         @Override
         public void close() {
    +      if (closed) {
    +        return;
    +      }
    +      closed = true;
           try {
             delegate.close();
           } finally {
    
    

    This is not thread-safe (should really be using AtomicBoolean.compareAndSet()) but it's consistent with similar existing code.

  2. vkedia commented on Apr 23, 2018

    @vkedia
    vkedia
    on Apr 23, 2018
    More actions

    @archiecobbs Thanks for reporting this. Your patch looks good. Do you want to send a PR for that?

  3. archiecobbs commented on Apr 23, 2018

    @archiecobbs
    archiecobbs
    on Apr 23, 2018
    Author
    More actions

    Sure, added #3183.

  4. archiecobbs
    closed this as completedon Apr 23, 2018
  5. archiecobbs
    reopened this on Apr 23, 2018
  6. vkedia
    added a commit that references this issue on Apr 23, 2018

    spanner: make TransactionManager.close() idempotent (#3172). (#3183)

    d7b1451
  7. vkedia commented on Apr 23, 2018

    @vkedia
    vkedia
    on Apr 23, 2018
    More actions

    Thanks. Closing this issue now.

  8. vkedia
    closed this as completedon Apr 23, 2018
  9. JustinBeckwith
    assigned
    archiecobbs
    on Feb 1, 2021
  10. chingor13
    added a commit that references this issue on Jan 14, 2026

    deps: update googleapis/sdk-platform-java action to v2.65.1 (#3172)

    fdd8e4f
  11. lqiu96
    added a commit that references this issue on Mar 20, 2026

    deps: update googleapis/sdk-platform-java action to v2.65.1 (#3172)

    8b0c19a
  12. chingor13
    added 3 commits that reference this issue on Mar 24, 2026

    deps: update opentelemetry-java monorepo to v1.42.0 (#3172)

    ef61b8c

    chore(main): release 2.45.0 (#3139)

    fbf904c

    chore(main): release 2.53.3-SNAPSHOT (#3172)

    c717418
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @archiecobbs
    archiecobbs

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.