Desired / expected behavior for Tx context

85 views
Skip to first unread message

Jack Christensen

unread,
Apr 12, 2017, 6:48:25 PM4/12/17
to golan...@googlegroups.com
>From the docs for BeginTx:

> The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled. 

What does / should happen when the underlying network goes away before the context is canceled. It appears that the cancel boils down to a call of driver.Tx.Rollback(). It seems likely that
driver.Tx. Rollback() would send a "ROLLBACK" command over the wire and would hang until a network io or timeout error.

Also, the interaction between contexts at the Tx level and at the statement level (tx.QueryContext or tx.ExecContext is unclear). If a context on a statement is canceled, is the Tx still valid?

Jack

Daniel Theophanes

unread,
Apr 12, 2017, 8:18:50 PM4/12/17
to golang-sql
It is assumed drivers should have some sort of read/write deadline for all operations. If a link goes out, then it will stall, but only until the deadline is hit, where it will return with a net error.

If a Tx context is canceled, an in progress Query operation may succeed, but the tx will be rolled back afterwards. After a Tx context is canceled no additional operations may be preformed on it. The Query context will allow canceling that specific query (if supported) and ensuring the Rows is closed when completed.

Asif Jalil

unread,
Mar 11, 2018, 1:33:46 PM3/11/18
to golang-sql
Am I correct in assuming that if a transaction is started using DB.BeginTx(...), then that context is part of the transaction.  That is the Tx struct that is returned from DB.BeginTx will store the context and will use it until the transaction is committed or rolled back.  If Tx.ExecContext(...) or Tx.QueryContext(...) is issued, then there will be two contexts: one that is part of the transaction and the other from the ExecContext/QueryContext.  If Exec/Query context occurs before the transaction context, the query is cancelled and the Rows is closed.  Will the transaction need to roll back because the transaction context has not been cancelled?

Thanks. 

Asif

Daniel Theophanes

unread,
Mar 13, 2018, 12:27:00 PM3/13/18
to golang-sql
If the ctx porivded to BeginTx is canceled, then the Tx will be rolled back.

If the ctx provided with QueryContext is canceled, then the query will be canceled and any Rows will be closed.

Ping again if that doesn't answer your question. Thanks, -Daniel

Asif Jalil

unread,
Mar 13, 2018, 12:48:23 PM3/13/18
to golang-sql
Thanks, Daniel. 
Reply all
Reply to author
Forward
0 new messages