Transactions and context handling

352 views
Skip to first unread message

otto....@docly.com

unread,
Nov 8, 2018, 7:20:46 AM11/8/18
to golang-sql
Hello,

https://golang.org/pkg/database/sql/#DB.BeginTx says:
If the context is canceled, the sql package will roll back the transaction.

This was somewhat unexpected to me. Sure, it is fine for read only queries, but I want to disable this for transactions that modify my db. So I wrapped my request context with a context that can not cancel and used it as input to BeginTx(). So far so good.

The provided context is used until the transaction is committed or rolled back.

If that is the case, how is the context passed into the transaction methods like ExecContext() used? What if I pass a context that can cancel into that one? Should I instead use the plain Exec() method? Would that use the context that was used as input for BeginTx()?

Thanks,
Otto 

Daniel Theophanes

unread,
Nov 8, 2018, 1:40:34 PM11/8/18
to otto....@docly.com, golan...@googlegroups.com
The context provided to BeginTx controls the context.
The context provided to ExecContext() or QueryContext are separate. However, if either ExecContext or QueryContext are running in a transaction, and the transaction context is cancelled, the The exec or query is also canceled.


I'm glad you are thinking about your context life span and how you want your Read and your Writes to behave.


--
You received this message because you are subscribed to the Google Groups "golang-sql" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-sql+...@googlegroups.com.
To post to this group, send email to golan...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-sql/39493bf9-f0de-4daf-a19d-1834b1a76809%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

otto....@docly.com

unread,
Nov 9, 2018, 11:17:27 AM11/9/18
to golang-sql


On Thursday, November 8, 2018 at 7:40:34 PM UTC+1, Daniel Theophanes wrote:
The context provided to BeginTx controls the context.
The context provided to ExecContext() or QueryContext are separate. However, if either ExecContext or QueryContext are running in a transaction, and the transaction context is cancelled, the The exec or query is also canceled.

Thanks for the quick reply!

What happens then if I stick a noCancelContext (1) into the BeginTx and then a context that can cancel (2) into ExecContext(). And then context 2 is cancelled. How will that effect the ExecContext() query?

Daniel Theophanes

unread,
Nov 9, 2018, 1:14:05 PM11/9/18
to otto....@docly.com, golan...@googlegroups.com
The exec will be canceled (if possible by the driver). The tx will remain.

--
You received this message because you are subscribed to the Google Groups "golang-sql" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-sql+...@googlegroups.com.
To post to this group, send email to golan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages