Skip to content

database/sql: Transaction commit closes the transaction even if there was an error on commit. #7777

@gopherbot

Description

@gopherbot

by Thetawaves:

What steps reproduce the problem?
If possible, include a link to a program on play.golang.org.

1. Using the sqlite driver, start a long running query.
2. Attempt to insert a row into the same table you queried.
3. Attempt to commit the insert transaction. The commit will fail because of a lock on
the table.

This is fine except that the transaction commit function in database/sql closes the
database/sql transaction through the use of defer tx.close() even if the commit was
unsuccessful. I have not been able to regain control of this transaction once the
database/sql representation has closed.


What should have happened instead?

The database/sql transaction commit function should first attempt to commit the driver
transaction, and only if it has succeeded, close the database/sql representation.

The function in question:

  1058  // Commit commits the transaction.
  1059  func (tx *Tx) Commit() error {
  1060      if tx.done {
  1061          return ErrTxDone
  1062      }
  1063      defer tx.close()
  1064      tx.dc.Lock()
  1065      defer tx.dc.Unlock()
  1066      return tx.txi.Commit()
  1067  }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions