mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
database/sql: do not store Tx options in Context
Drivers which previously supported tip will need to update to this revision before release. Fixes #18284 Change-Id: I70b8e7afff1558a8b5348885ce9f50e067c72ee9 Reviewed-on: https://go-review.googlesource.com/34330 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
fe07091f9e
commit
d0501f1da9
6 changed files with 65 additions and 84 deletions
|
|
@ -111,25 +111,32 @@ func ctxDriverStmtQuery(ctx context.Context, si driver.Stmt, nvdargs []driver.Na
|
|||
|
||||
var errLevelNotSupported = errors.New("sql: selected isolation level is not supported")
|
||||
|
||||
func ctxDriverBegin(ctx context.Context, ci driver.Conn) (driver.Tx, error) {
|
||||
if ciCtx, is := ci.(driver.ConnBeginContext); is {
|
||||
return ciCtx.BeginContext(ctx)
|
||||
func ctxDriverBegin(ctx context.Context, opts *TxOptions, ci driver.Conn) (driver.Tx, error) {
|
||||
if ciCtx, is := ci.(driver.ConnBeginTx); is {
|
||||
dopts := driver.TxOptions{}
|
||||
if opts != nil {
|
||||
dopts.Isolation = driver.IsolationLevel(opts.Isolation)
|
||||
dopts.ReadOnly = opts.ReadOnly
|
||||
}
|
||||
return ciCtx.BeginTx(ctx, dopts)
|
||||
}
|
||||
|
||||
if ctx.Done() == context.Background().Done() {
|
||||
return ci.Begin()
|
||||
}
|
||||
|
||||
// Check the transaction level in ctx. If set and non-default
|
||||
// then return an error here as the BeginContext driver value is not supported.
|
||||
if level, ok := driver.IsolationFromContext(ctx); ok && level != driver.IsolationLevel(LevelDefault) {
|
||||
return nil, errors.New("sql: driver does not support non-default isolation level")
|
||||
}
|
||||
if opts != nil {
|
||||
// Check the transaction level. If the transaction level is non-default
|
||||
// then return an error here as the BeginTx driver value is not supported.
|
||||
if opts.Isolation != LevelDefault {
|
||||
return nil, errors.New("sql: driver does not support non-default isolation level")
|
||||
}
|
||||
|
||||
// Check for a read-only parameter in ctx. If a read-only transaction is
|
||||
// requested return an error as the BeginContext driver value is not supported.
|
||||
if ro := driver.ReadOnlyFromContext(ctx); ro {
|
||||
return nil, errors.New("sql: driver does not support read-only transactions")
|
||||
// If a read-only transaction is requested return an error as the
|
||||
// BeginTx driver value is not supported.
|
||||
if opts.ReadOnly {
|
||||
return nil, errors.New("sql: driver does not support read-only transactions")
|
||||
}
|
||||
}
|
||||
|
||||
txi, err := ci.Begin()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue