Comments (1)
There are also a few subtleties about how the package prepares and uses statements. There are some best-effort things that can cascade out of control when concurrency becomes high and new connections are opened because the connection a statement used to prepare isn't available at the time it's needed. This can result in statements being re-prepared a lot without being closed inside the server, and potentially causing issues on MySQL in particular (running over the limit).
Gustavo Kristic wrote the following to me privately, which I think is good reading. The context is that we were seeing some of these problems and trying to use VividCortex's metrics on statement prepare/close counts to figure out which statements were not being closed:
QueryRow()
has no difference from Query()
regarding the preparedness of statements; it's only a convenience wrapper on the later in terms of result handling.
In my opinion the problem is Go's underlying database/sql
implementation. Prepared statements are kept in the package per connection in the connection pool. When you try to run a previously prepared statement, Go tries to use the connection where the statement was prepared. But if that's not possible (cause the connection is already busy) then it will go ahead and prepare the statement again using a different connection, that will now end up running the required statement. (Also, if a connection is eventually dropped you won't see any close for all statements that were prepared for it. That may as well account for some difference.) This behavior is transparent for applications using database/sql
and cannot be changed. It's at the core of the package.
Let me stress something: you won't see the number of prepared statements increasing if throughput is not high enough. The database/sql
package will always favor connections where the statement has already been prepared. It will resort to a different connection only if the former is busy at that particular moment, which usually means the server is doing a lot of work in parallel.
from go-database-sql-tutorial.
Related Issues (20)
- go-sql-driver/mysql does provide error identifiers HOT 1
- 404 when using link
- question: are connections pooled per prepared statement? HOT 1
- question: are connections pooled per prepared statement?
- "Go doesn't support multiple result sets" statement is incorrect.
- Transaction + lock tables
- Explain idiomatic checking of error codes
- Error-handling unclear
- The type settings make this website generally hard to read HOT 6
- retrieving.md: description of rows.Close() handling doesn't match with the code HOT 4
- Adding a section that describes the table creation HOT 1
- Complete Example?
- License HOT 19
- Working with Unknown Columns witch has time with oracle HOT 2
- consider using sqlite3 as your example scenario?
- Stop using log.Fatal
- Consider teaching readers to insert data before fetching it HOT 1
- Mention variations with Context
- empty "leftnav" ? HOT 1
- Something broken after add translate. HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-database-sql-tutorial.