Commit bd088036 authored by Matt Dee's avatar Matt Dee Committed by Daniel Theophanes

database/sql: fail on unsupported options when context is un-cancellable

Currently, the check for `ctx.Done() == context.Background().Done()`
comes before the check to see if we are ignoring any options.  That
check should be done earlier, so that the options are not silently
ignored.

Fixes #21350

Change-Id: I3704e4209854c7d99f3f92498bae831cabc7e419
Reviewed-on: https://go-review.googlesource.com/53970Reviewed-by: 's avatarDaniel Theophanes <kardianos@gmail.com>
Run-TryBot: Daniel Theophanes <kardianos@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent d401c427
...@@ -107,10 +107,6 @@ func ctxDriverBegin(ctx context.Context, opts *TxOptions, ci driver.Conn) (drive ...@@ -107,10 +107,6 @@ func ctxDriverBegin(ctx context.Context, opts *TxOptions, ci driver.Conn) (drive
return ciCtx.BeginTx(ctx, dopts) return ciCtx.BeginTx(ctx, dopts)
} }
if ctx.Done() == context.Background().Done() {
return ci.Begin()
}
if opts != nil { if opts != nil {
// Check the transaction level. If the transaction level is non-default // Check the transaction level. If the transaction level is non-default
// then return an error here as the BeginTx driver value is not supported. // then return an error here as the BeginTx driver value is not supported.
...@@ -125,6 +121,10 @@ func ctxDriverBegin(ctx context.Context, opts *TxOptions, ci driver.Conn) (drive ...@@ -125,6 +121,10 @@ func ctxDriverBegin(ctx context.Context, opts *TxOptions, ci driver.Conn) (drive
} }
} }
if ctx.Done() == context.Background().Done() {
return ci.Begin()
}
txi, err := ci.Begin() txi, err := ci.Begin()
if err == nil { if err == nil {
select { select {
......
...@@ -439,6 +439,20 @@ func TestTxContextWait(t *testing.T) { ...@@ -439,6 +439,20 @@ func TestTxContextWait(t *testing.T) {
waitForFree(t, db, 5*time.Second, 0) waitForFree(t, db, 5*time.Second, 0)
} }
// TestUnsupportedOptions checks that the database fails when a driver that
// doesn't implement ConnBeginTx is used with non-default options and an
// un-cancellable context.
func TestUnsupportedOptions(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)
_, err := db.BeginTx(context.Background(), &TxOptions{
Isolation: LevelSerializable, ReadOnly: true,
})
if err == nil {
t.Fatal("expected error when using unsupported options, got nil")
}
}
func TestMultiResultSetQuery(t *testing.T) { func TestMultiResultSetQuery(t *testing.T) {
db := newTestDB(t, "people") db := newTestDB(t, "people")
defer closeDB(t, db) defer closeDB(t, db)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment