summaryrefslogtreecommitdiff
path: root/src/database/sql/sql_test.go
Commit message (Collapse)AuthorAgeFilesLines
* database/sql: fix deadlock test in prepare statementDaniel Theophanes2021-06-211-1/+1
| | | | | | | | | | | | | | | | | | | | | | The issue go#46783 correctly diagnosed the context timeout caused an intermittent failure when the context was canceled prior to the BeginTx call. However due to the asynchronous nature of canceling a Tx through a context on fast systems, the tx.Prepare also succeeded. On slower systems or if a time.Sleep was inserted between the BeginTx and Prepare, the Prepare would fail. Resolve this by moving the context cancel after the Prepare. This will still trigger the deadlock which I tested locally. In addition, I interspersed multiple time.Sleep calls and the test still functioned. Fixes #46852 Change-Id: I9cbf90d3c12b2555493a37799738772b615ae39d Reviewed-on: https://go-review.googlesource.com/c/go/+/329830 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Bryan C. Mills <bcmills@google.com>
* database/sql: do not rely on timeout for deadlock testDaniel Theophanes2021-06-191-1/+2
| | | | | | | | | | | Fixes #46783 Change-Id: I8a8d1716279a041a7411c0c47a440a7997b39c80 Reviewed-on: https://go-review.googlesource.com/c/go/+/328649 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com> Trust: Carlos Amedee <carlos@golang.org>
* database/sql: add NullInt16 and NullByteAriel Mashraki2021-05-041-0/+24
| | | | | | | | | | | Fixes #40082 Change-Id: I01cd4d0e23c0376a6ee6e0b196c9f840cd662325 Reviewed-on: https://go-review.googlesource.com/c/go/+/311572 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org>
* all: fix spellingsNaman Gera2021-04-101-2/+2
| | | | | | | | | | | | | | This follows the spelling choices that the Go project has made for English words. https://github.com/golang/go/wiki/Spelling Change-Id: Ie7c586d2cf23020cb492cfff58c0831d2d8d3a78 GitHub-Last-Rev: e16a32cd225a275f73d236bcb33703986d110ded GitHub-Pull-Request: golang/go#45442 Reviewed-on: https://go-review.googlesource.com/c/go/+/308291 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Emmanuel Odeke <emmanuel@orijtech.com>
* database/sql: close driver.Connector if it implements io.CloserIvan Trubach2021-02-251-1/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | This change allows driver implementations to manage resources in driver.Connector, e.g. to share the same underlying database handle between multiple connections. That is, it allows embedded databases with in-memory backends like SQLite and Genji to safely release the resources once the sql.DB is closed. This makes it possible to address oddities with in-memory stores in SQLite and Genji drivers without introducing too much complexity in the driver implementations. See also: - https://github.com/mattn/go-sqlite3/issues/204 - https://github.com/mattn/go-sqlite3/issues/511 - https://github.com/genjidb/genji/issues/210 Fixes #41790 Change-Id: Idbd19763134438ed38288b9d44f16608e4e97fd7 GitHub-Last-Rev: 962c785dfb3bb6ad98b2216bcedd84ba383fe872 GitHub-Pull-Request: golang/go#41710 Reviewed-on: https://go-review.googlesource.com/c/go/+/258360 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Trust: Emmanuel Odeke <emmanuel@orijtech.com> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org>
* database: remove race in TestTxContextWaitJosh Bleecher Snyder2021-02-241-8/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | This test contained a data race. On line 437, db.BeginTx starts a goroutine that runs tx.awaitDone, which reads tx.keepConnOnRollback. On line 445, the test writes to tx.keepConnOnRollback. tx.awaitDone waits on ctx, but because ctx is timeout-based, there's no ordering guarantee between the write and the read. The race detector never caught this before because the context package implementation of Done contained enough synchronization to make it safe. That synchronization is not package of the context API or guarantees, and the first several releases it was not present. Another commit soon will remove that synchronization, exposing the latent data race. To fix the race, emulate a time-based context using an explicit cancellation-based context. This gives us enough control to avoid the race. Change-Id: I103fe9b987b1d4c02e7a20ac3c22a682652128b6 Reviewed-on: https://go-review.googlesource.com/c/go/+/288493 Trust: Josh Bleecher Snyder <josharian@gmail.com> Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Daniel Theophanes <kardianos@gmail.com>
* database/sql, runtime: correct *.Fatal inside goroutines in testsEmmanuel T Odeke2020-10-311-5/+15
| | | | | | | | | | Found by go vet pass "testinggoroutines". Change-Id: I6360af2079617b7aa62dcb9bd7254578ca5d1c1d Reviewed-on: https://go-review.googlesource.com/c/go/+/235527 Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Emmanuel Odeke <emmanuel@orijtech.com>
* database/sql: fix TestTxStmtDeadlock testTzu-Chiao Yeh2020-10-291-4/+2
| | | | | | | | | | | | | | | | Drop error check because errors can be not only ErrTxDone for tx stmt executions, and the purpose of the test is just reproducing deadlock. Fixes #42259 Change-Id: I9e7105ada1403ec7064dcc1c3385b36893a1c195 Reviewed-on: https://go-review.googlesource.com/c/go/+/266097 Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Trust: Emmanuel Odeke <emmanuel@orijtech.com> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org>
* database/sql: fix tx stmt deadlock when rollbackTzu-Chiao Yeh2020-10-281-0/+30
| | | | | | | | | | | | | | | | | | | | Tx acquires tx.closemu W-lock and then acquires stmt.closemu.W-lock to fully close the transaction and associated prepared statement. Stmt query and execution run in reverse ways - acquires stmt.closemu.R-lock and then acquires tx.closemu.R-lock to grab tx connection, which may cause deadlock. Prevent the lock is held around tx.closePrepared to ensure no deadlock happens. Fixes #40985 Change-Id: If53909822b87bce11861a6e3035ecb9476d2cd17 Reviewed-on: https://go-review.googlesource.com/c/go/+/250178 Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Trust: Emmanuel Odeke <emmanuel@orijtech.com>
* database/sql: make Rows.Scan properly wrap underlying errorsTim Möhlmann2020-08-141-0/+35
| | | | | | | | | | | | | | | | | | The prior implementation used the format verb %v which unfortunately improperly wrapped any underlying scanner errors, and we couldn't use errors.Is nor errors.As. This change fixes that by using the %w verb. Added a unit to ensure that both error sub string matching works, but also that errors.Is works as expected. Fixes #38099 Change-Id: Iea667041dd8081d961246f77f2542330417292dc Reviewed-on: https://go-review.googlesource.com/c/go/+/248337 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* database/sql: de-flake TestTxCannotCommitAfterRollbackDaniel Theophanes2020-04-221-1/+5
| | | | | | | | | | | | | Do not cancel rows during test. Only cancel the Tx. Correct the referenced issue number on the test. Fixes #38597 Change-Id: I0e8ba1bf2a8ba638d121c9c6938501fec1d5e961 Reviewed-on: https://go-review.googlesource.com/c/go/+/229478 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* database/sql: on Tx rollback, retain connection if driver can reset sessionDaniel Theophanes2020-04-201-0/+30
| | | | | | | | | | | | Previously the Tx would drop the connection after rolling back from a context cancel. Now if the driver can reset the session, keep the connection. Change-Id: Ie6a3124275632787629844d91a06bb2e70cc060b Reviewed-on: https://go-review.googlesource.com/c/go/+/216241 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* database/sql: prevent Tx statement from committing after rollbackDaniel Theophanes2020-04-201-0/+69
| | | | | | | | | | | | | | | | | | It was possible for a Tx that was aborted for rollback asynchronously to execute a query after the rollback had completed on the database, which often would auto commit the query outside of the transaction. By W-locking the tx.closemu prior to issuing the rollback connection it ensures any Tx query either fails or finishes on the Tx, and never after the Tx has rolled back. Fixes #34775 Fixes #32942 Change-Id: I017b7932082f2f4ead70bae08b61ed9068ac1d01 Reviewed-on: https://go-review.googlesource.com/c/go/+/216240 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
* database/sql: check conn expiry when returning to pool, not when handing it outDaniel Theophanes2020-04-201-0/+89
| | | | | | | | | | | | | | | | | | | | | | | With the original connection reuse strategy, it was possible that when a new connection was requested, the pool would wait for an an existing connection to return for re-use in a full connection pool, and then it would check if the returned connection was expired. If the returned connection expired while awaiting re-use, it would return an error to the location requestiong the new connection. The existing call sites requesting a new connection was often the last attempt at returning a connection for a query. This would then result in a failed query. This change ensures that we perform the expiry check right before a connection is inserted back in to the connection pool for while requesting a new connection. If requesting a new connection it will no longer fail due to the connection expiring. Fixes #32530 Change-Id: If16379befe0e14d90160219c0c9396243fe062f7 Reviewed-on: https://go-review.googlesource.com/c/go/+/216197 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
* database/sql: add test for Conn.Validator interfaceDaniel Theophanes2020-03-291-0/+31
| | | | | | | | | | | This addresses comments made by Russ after https://golang.org/cl/174122 was merged. It addes a test for the connection validator and renames the interface to just "Validator". Change-Id: Iea53e9b250c9be2e86e9b75906e7353e26437c5c Reviewed-on: https://go-review.googlesource.com/c/go/+/223963 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
* database/sql: add method Err on sql.RowTim Möhlmann2020-03-191-0/+18
| | | | | | | | | | | | | | | | | The Row.Err method is intended to assist wrapping sql.DB. Because sql.Row is a struct with private fields, a wrapper in an existing code base cannot easily provide users with a different implementation without large rewrites. Adding this method allows query level errors to be handled centrally. Fixes #35804 Change-Id: I94e6329de89a7ee1284ce9ef76af4363d2d081f9 Reviewed-on: https://go-review.googlesource.com/c/go/+/214317 Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* database/sql: add SetConnMaxIdleTimeDaniel Theophanes2020-02-211-0/+55
| | | | | | | | | | | | | | Allow removing a connection from the connection pool after it has been idle for a period of time, without regard to the total lifespan of the connection. Fixes #25232 Change-Id: Icff157b906769a2d2d45c67525e04a72feb8d832 Reviewed-on: https://go-review.googlesource.com/c/go/+/145758 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* all: fix invalid invocations of Fatalf in goroutinesEmmanuel T Odeke2020-01-031-1/+5
| | | | | | | | | | | Found by running the go vet pass 'testinggoroutine' that I started in CL 212920. Change-Id: Ic9462fac85dbafc437fe4a323b886755a67a1efa Reviewed-on: https://go-review.googlesource.com/c/go/+/213097 Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* all: fix typosAinar Garipov2019-09-081-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Use the following (suboptimal) script to obtain a list of possible typos: #!/usr/bin/env sh set -x git ls-files |\ grep -e '\.\(c\|cc\|go\)$' |\ xargs -n 1\ awk\ '/\/\// { gsub(/.*\/\//, ""); print; } /\/\*/, /\*\// { gsub(/.*\/\*/, ""); gsub(/\*\/.*/, ""); }' |\ hunspell -d en_US -l |\ grep '^[[:upper:]]\{0,1\}[[:lower:]]\{1,\}$' |\ grep -v -e '^.\{1,4\}$' -e '^.\{16,\}$' |\ sort -f |\ uniq -c |\ awk '$1 == 1 { print $2; }' Then, go through the results manually and fix the most obvious typos in the non-vendored code. Change-Id: I3cb5830a176850e1a0584b8a40b47bde7b260eae Reviewed-on: https://go-review.googlesource.com/c/go/+/193848 Reviewed-by: Robert Griesemer <gri@golang.org>
* database/sql: add support for decimal interfaceDaniel Theophanes2019-06-131-6/+6
| | | | | | | | | | | | | | | | | Add support for scanning decimal types into values. If the dest supports the decimal composer interface and the src supports the decimal decomposer, set the value of the decimal when Scanning. Add support for sending decimal decomposer interface values as parameters. For #30870 Change-Id: Ic5dbf9069df8d56405852b17542a9188d55c2947 Reviewed-on: https://go-review.googlesource.com/c/go/+/174181 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* database/sql: add Conn.Raw to expose the driver Conn safelyDaniel Theophanes2019-06-131-0/+48
| | | | | | | | | | | | | | Exposing the underlying driver conn will allow the use of the standard connection pool while still able to run special function directly on the driver. Fixes #29835 Change-Id: Ib6d3b9535e730f008916805ae3bf76e4494c88f9 Reviewed-on: https://go-review.googlesource.com/c/go/+/174182 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* database/sql: add NullInt32Daniel Theophanes2019-04-261-0/+13
| | | | | | | | | | | | | | It is common for database integers to be represented as int32 internally. Although NullInt64 is already defined, this should remove some type casts and make working with those eaiser. For #31231 Change-Id: Ia0c37ecef035fee0734c1d1fb6f58aef6905cf5e Reviewed-on: https://go-review.googlesource.com/c/go/+/174178 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* database/sql: check if src is nil before converting to stringDaniel Theophanes2019-04-261-1/+1
| | | | | | | | | | | | | | | | A nil src (NULL database value) will result in a "nil" string, which will never parse correctly in a ParseInt or similar numeric conversion. The resulting error is confusing. Check for a nil src prior to converting the value to string if the resulting string will be parsed after that. Closes #31274 Change-Id: I90f12cceff00fbbfdd3e343b04fa7e2596390e6d Reviewed-on: https://go-review.googlesource.com/c/go/+/174177 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* database/sql: add NullTimeDaniel Theophanes2019-04-051-0/+15
| | | | | | | | | | | | This matches NullBool, NullFloat64, and NullInt64. Fixes #30305 Change-Id: I79bfcf04a3d43b965d2a3159b0ac22f3e8084a53 Reviewed-on: https://go-review.googlesource.com/c/go/+/170699 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* database/sql: add support for returning cursors to clientDaniel Theophanes2018-11-081-0/+46
| | | | | | | | | | | This CL add support for converting a returned cursor (presented to this package as a driver.Rows) and scanning it into a *Rows. Fixes #28515 Change-Id: Id8191c568dc135af9e5e8555efcd01987708edcb Reviewed-on: https://go-review.googlesource.com/c/145738 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* all: fix a bunch of misspellingsIgor Zhilianin2018-10-081-1/+1
| | | | | | | | | | Change-Id: I94cebca86706e072fbe3be782d3edbe0e22b9432 GitHub-Last-Rev: 8e15a40545704fb21b41a8768079f2da19341ef3 GitHub-Pull-Request: golang/go#28067 Reviewed-on: https://go-review.googlesource.com/c/140437 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* all: fix a bunch of misspellingsIgor Zhilianin2018-10-061-1/+1
| | | | | | | | | | Change-Id: If2954bdfc551515403706b2cd0dde94e45936e08 GitHub-Last-Rev: d4cfc41a5504cf10befefdb881d4c45986a1d1f8 GitHub-Pull-Request: golang/go#28049 Reviewed-on: https://go-review.googlesource.com/c/140299 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* database/sql: correctly report MaxIdleClosed statDaniel Theophanes2018-10-021-0/+52
| | | | | | | | | | | | | | Previously the MaxIdleClosed counter was incremented when added to the free connection list, rather then when it wasn't added to the free connection list. Flip this logic to correct. Fixes #27792 Change-Id: I405302c14fb985369dab48fbe845e5651afc4ccf Reviewed-on: https://go-review.googlesource.com/c/138578 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* database/sql: return context errors from Rows.ScanEric Rykwalder2018-04-111-2/+2
| | | | | | | | | | | | | | The previous implementation would return "sql: Rows are closed" for any context errors, which can be confusing for context timeouts or cancelations. Fixes #24431 Change-Id: I884904ec43204c43f4e94e2335b2802aab77a888 Reviewed-on: https://go-review.googlesource.com/104276 Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* all: fix misspellingsShawn Smith2018-02-201-1/+1
| | | | | | | | | | GitHub-Last-Rev: 468df242d07419c228656985702325aa78952d99 GitHub-Pull-Request: golang/go#23935 Change-Id: If751ce3ffa3a4d5e00a3138211383d12cb6b23fc Reviewed-on: https://go-review.googlesource.com/95577 Run-TryBot: Andrew Bonventre <andybons@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Bonventre <andybons@golang.org>
* database/sql: include SQL column name in Scan() error messageHaraldNordgren2018-02-131-1/+52
| | | | | | | | | | | | | | | | | | When 'convertAssign' gives an error, instead of giving just the index of the failing column -- which is not always helpful, especially when there are lots of columns in the query -- utilize 'rs.rowsi.Columns()' to extract the underlying column name and include that in the error string: sql: Scan error on column index 0, name "some_column": ... Fixes #23362 Change-Id: I0fe71ff3c25f4c0dd9fc6aa2c2da2360dd93e3e0 Reviewed-on: https://go-review.googlesource.com/86537 Reviewed-by: Harald Nordgren <haraldnordgren@gmail.com> Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* database/sql: buffers provided to Rows.Next should not be modified by driversDaniel Theophanes2018-01-251-37/+3
| | | | | | | | | | | | | | Previously we allowed drivers to modify the row buffer used to scan values when closing Rows. This is no longer acceptable and can lead to data races. Fixes #23519 Change-Id: I91820a6266ffe52f95f40bb47307d375727715af Reviewed-on: https://go-review.googlesource.com/89936 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
* database/sql: allow OpenConnector in a driver.Driver interfaceDaniel Theophanes2017-11-161-0/+13
| | | | | | | | | | | | | | | | | | While driver.Connector was previously added to allow non-string connection arguments and access to the context, most users of the sql package will continue to rely on a string DSN. Allow drivers to implement a string DSN to Connector interface that both allows a single parsing of the string DSN and uses the Connector interface which passes available context to the driver dialer. Fixes #22713 Change-Id: Ia0b862262f4c4670effe2538d0d6d43733fea18d Reviewed-on: https://go-review.googlesource.com/77550 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org>
* database/sql: ensure all driver interfaces are called under single lockDaniel Theophanes2017-10-251-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | Russ pointed out in a previous CL golang.org/cl/65731 that not only was the locking incomplete, previous changes did not correctly lock driver calls in other sections. After inspecting driverConn, driverStmt, driverResult, Tx, and Rows structs where driver interfaces are stored, I discovered a few more places that failed to lock driver calls. The largest of these was the parameter type converter "driverArgs". driverArgs was typically called right before another call to the driver in a locked region, so I made the entire driverArgs expect a locked driver mutex and combined the region. This should not be a problem because the connection is pulled out of the connection pool either way so there shouldn't be contention. Fixes #21117 Change-Id: I88d46f74dca25fb11a30f0bf8e79785a73133d23 Reviewed-on: https://go-review.googlesource.com/71433 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
* database/sql: add driver.ResetSessioner and add pool supportDaniel Theophanes2017-10-241-16/+47
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | A single database connection ususally maps to a single session. A connection pool is logically also a session pool. Most sessions have a way to reset the session state which is desirable to prevent one bad query from poisoning another later query with temp table name conflicts or other persistent session resources. It also lets drivers provide users with better error messages from queryies when the underlying transport or query method fails. Internally the driver connection should now be marked as bad, but return the actual connection. When ResetSession is called on the connection it should return driver.ErrBadConn to remove it from the connection pool. Previously drivers had to choose between meaningful error messages or poisoning the connection pool. Lastly update TestPoolExhaustOnCancel from relying on a WAIT query fixing a flaky timeout issue exposed by this change. Fixes #22049 Fixes #20807 Change-Id: I2b5df6d954a38d0ad93bf1922ec16e74c827274c Reviewed-on: https://go-review.googlesource.com/73033 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* Revert "database/sql: add driver.ResetSessioner and add pool support"Russ Cox2017-10-241-45/+15
| | | | | | | | | | This reverts commit 2620ac3aeafe75a62fa81bd5094a8e1e4ef1ca8b. Reason for revert: broke all the builds. Change-Id: I26fc09a13f5f80fa708de66c843442ff9d934694 Reviewed-on: https://go-review.googlesource.com/73050 Reviewed-by: Russ Cox <rsc@golang.org>
* database/sql: add driver.ResetSessioner and add pool supportDaniel Theophanes2017-10-241-15/+45
| | | | | | | | | | | | | | | | | | | | | | | | | | | A single database connection ususally maps to a single session. A connection pool is logically also a session pool. Most sessions have a way to reset the session state which is desirable to prevent one bad query from poisoning another later query with temp table name conflicts or other persistent session resources. It also lets drivers provide users with better error messages from queryies when the underlying transport or query method fails. Internally the driver connection should now be marked as bad, but return the actual connection. When ResetSession is called on the connection it should return driver.ErrBadConn to remove it from the connection pool. Previously drivers had to choose between meaningful error messages or poisoning the connection pool. Lastly update TestPoolExhaustOnCancel from relying on a WAIT query fixing a flaky timeout issue exposed by this change. Fixes #22049 Fixes #20807 Change-Id: Idffa1a7ca9ccfe633257c4a3ae299b864f46c5b6 Reviewed-on: https://go-review.googlesource.com/67630 Reviewed-by: Ian Lance Taylor <iant@golang.org>
* database/sql: allow drivers to only implement Context variantsDaniel Theophanes2017-10-241-0/+121
| | | | | | | | | | | | | | Drivers shouldn't need to implement both Queryer and QueryerContext, they should just implement QueryerContext. Same with Execer and ExecerContext. This CL tests for QueryContext and ExecerContext first so drivers do not need to implement Queryer and Execer with an empty definition. Fixes #21663 Change-Id: Ifbaa71da669f4bc60f8da8c41a04a4afed699a9f Reviewed-on: https://go-review.googlesource.com/65733 Reviewed-by: Ian Lance Taylor <iant@golang.org>
* Revert "database/sql: prevent race in driver by locking dc in Next"Daniel Theophanes2017-10-171-7/+1
| | | | | | | | | | | | This reverts commit 897080d5cbb1793f8ad3ef5fb7c6fafba2e97d42. Reason for revert: Fails to fix all the locking issues. Updates #21117 Change-Id: I6fc9cb7897244d6e1af78c089a2bf383258ec049 Reviewed-on: https://go-review.googlesource.com/71450 Reviewed-by: Ian Lance Taylor <iant@golang.org>
* database/sql: prevent race in driver by locking dc in NextDaniel Theophanes2017-10-131-1/+7
| | | | | | | | | | | | | | | | | | | | | | | | Database drivers should be called from a single goroutine to ease driver's design. If a driver chooses to handle context cancels internally it may do so. The sql package violated this agreement when calling Next or NextResultSet. It was possible for a concurrent rollback triggered from a context cancel to call a Tx.Rollback (which takes a driver connection lock) while a Rows.Next is in progress (which does not tack the driver connection lock). The current internal design of the sql package is each call takes roughly two locks: a closemu lock which prevents an disposing of internal resources (assigning nil or removing from lists) and a driver connection lock that prevents calling driver code from multiple goroutines. Fixes #21117 Change-Id: Ie340dc752a503089c27f57ffd43e191534829360 Reviewed-on: https://go-review.googlesource.com/65731 Reviewed-by: Ian Lance Taylor <iant@golang.org>
* database/sql: fix unreachable code in ColumnTypes testJulien Schmidt2017-10-041-6/+6
| | | | | | | | | | | Before this change the ct == 0 check could never be true. Moreover the values were not properly indirected. Change-Id: Ice47e36e3492babc4b47d2f9099e8772be231c96 Reviewed-on: https://go-review.googlesource.com/68130 Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* database/sql: add OpenDB to directly create a *DB without a DSN.James Lawrence2017-09-231-6/+13
| | | | | | | | | | | | | | | | | | The current Open method limits the ability for driver maintainers to expose options for their drivers by forcing all the configuration to pass through the DSN in order to create a *DB. This CL allows driver maintainers to write their own initialization functions that return a *DB making configuration of the underlying drivers easier. Fixes #20268 Change-Id: Ib10b794f36a201bbb92c23999c8351815d38eedb Reviewed-on: https://go-review.googlesource.com/53430 Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* all: fix easy-to-miss typosAgniva De Sarker2017-08-231-1/+1
| | | | | | | | | | Using the wonderful https://github.com/client9/misspell tool. Change-Id: Icdbc75a5559854f4a7a61b5271bcc7e3f99a1a24 Reviewed-on: https://go-review.googlesource.com/57851 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com> Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* database/sql: fail on unsupported options when context is un-cancellableMatt Dee2017-08-091-0/+14
| | | | | | | | | | | | | | | 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/53970 Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* database/sql: ensure a Stmt from a Conn executes on the same driver.ConnDaniel Theophanes2017-06-131-1/+37
| | | | | | | | | | | | | | Ensure a Stmt prepared on a Conn executes on the same driver.Conn. This also removes another instance of duplicated prepare logic as a side effect. Fixes #20647 Change-Id: Ia00a19e4dd15e19e4d754105babdff5dc127728f Reviewed-on: https://go-review.googlesource.com/45391 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* database/sql: correct level of write to same var for race detectorDaniel Theophanes2017-06-121-8/+4
| | | | | | | | | | | Rather then write to the same variable per fakeConn, write to either fakeConn or rowsCursor. Fixes #20646 Change-Id: Ifc79f989bd1606b8e3ebecb1e7844cce3ad06e17 Reviewed-on: https://go-review.googlesource.com/45393 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* database/sql: prevent race on Rows close with Tx RollbackDaniel Theophanes2017-06-121-11/+64
| | | | | | | | | | | | | | | | | | In addition to adding a guard to the Rows close, add a var in the fakeConn that gets read and written to on each operation, simulating writing or reading from the server. TestConcurrency/TxStmt* tests have been commented out as they now fail after checking for races on the fakeConn. See issue #20646 for more information. Fixes #20622 Change-Id: I80b36ea33d776e5b4968be1683ff8c61728ee1ea Reviewed-on: https://go-review.googlesource.com/45275 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* Revert "database/sql: Use Tx.ctx in Tx non-context methods"Daniel Theophanes2017-06-121-29/+0
| | | | | | | | | | | | | | This reverts commit ef0f7fb92b9458d7d35ee3c10ae853e3dc3077eb. Reason for revert: Altered behavior of Queries prior to Tx commit. See #20631. Change-Id: I2548507c2935a7c60b92aae377dcc8e9aca66331 Reviewed-on: https://go-review.googlesource.com/45231 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> Reviewed-by: Bulat Gaifullin <gaifullinbf@gmail.com> Reviewed-by: Michael Hudson-Doyle <michael.hudson@canonical.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* database/sql: Use Tx.ctx in Tx non-context methodsBulat Gaifullin2017-06-071-0/+29
| | | | | | | | | | | | | | | | | | | | | | The Tx methods Query and Exec uses context.Background() even Tx was created by context. This patch enables using Tx.ctx in all Tx methods which do not has context arg. Backward compatibility: - If Tx has created without context, nothing changes. - If Tx has created with context and non-context method is called: - If context is expired, the execution fails, but it can fail on Commit or Rollback as well, so in terms of whole transaction - nothing changes. - If context is not expired, nothing changes too. Fixes #20098 Change-Id: I9570a2deaace5875bb4c5dcf7b3a084a6bcd0d00 Reviewed-on: https://go-review.googlesource.com/44956 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* database/sql: ensure Rows is closed when Tx closesDaniel Theophanes2017-06-051-0/+26
| | | | | | | | | | | Close any Rows queried within a Tx when the Tx is closed. This prevents the Tx from blocking on rollback if a Rows query has not been closed yet. Fixes #20575 Change-Id: I4efe9c4150e951d8a0f1c40d9d5e325964fdd608 Reviewed-on: https://go-review.googlesource.com/44812 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>