Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move connCheck into ResetSession method #1476

Closed
lennonwoo opened this issue Sep 2, 2023 · 1 comment · Fixed by #1496
Closed

Move connCheck into ResetSession method #1476

lennonwoo opened this issue Sep 2, 2023 · 1 comment · Fixed by #1496

Comments

@lennonwoo
Copy link

Issue description

Now the MySQL connection implements database/sql driver's ResetSession of SessionResetter interface by set mc.reset to be true and call connCheck in connection's writePacket method in #934.

I'm wondering whether we can put connection liveness checking logic at ResetSession method directly?

From user point of view, they may want to get one sticky connection to set session variable then do query by using db.Conn.

In such scenario, it's possible to get bad connection if user doesn't configure connection lifetime and the connection get from the pool exceed the server side's wait_timeout. If we can put connCheck at ResetSession method, I think the issue can be resolved.

Example code

conn, err := db.Conn(ctx)
if err != nil {
	// err can't be driver.ErrBadConn because db.Conn method will fallback to create new connection after retry.
}
rows, err := conn.QueryContext(ctx, query, args)
if err != nil {
	// err can be driver.ErrBadConn if connection lifetime exceed server side wait_timeout.
}

Error log

N/A

Configuration

Driver version (or git SHA): master branch

Go version: go 1.20.1

Server version: MySQL 5.7

Server OS: MacOS

@methane
Copy link
Member

methane commented Sep 4, 2023

When ResetSession was added, it was called when the connection is put into the pool.
Thet is why we only flag on ResetSession and checkConnLiveness on sending query.

In Go 1.15, this behavior was changed.

golang/go@971f8a2

  • ResetSession is called when the connection is checked out from the pool. So we can call checkConnLiveness() on ResetSession.
  • IsValid is called right before the connection is checking into the pool. We can report error from previous query.

@methane methane changed the title Question: is it possible to put connCheck at ResetSession method? Move connCheck into ResetSession method Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants