Skip to content

Ping() does not trigger a reconnect after DB restart #921

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

Closed
dmitriivoitovich opened this issue Feb 18, 2019 · 3 comments
Closed

Ping() does not trigger a reconnect after DB restart #921

dmitriivoitovich opened this issue Feb 18, 2019 · 3 comments

Comments

@dmitriivoitovich
Copy link

When the driver loses connection to MySQL, Ping() starts returning errors regardless of an actual DB server status

For instance if we restarted the MySQL docker container during this script running

func main() {
	conn, _ := sql.Open("mysql", "root:toor@tcp(127.0.0.1:3306)/?charset=utf8&parseTime=true")

	for {
		err := conn.Ping()
		if err != nil {
			fmt.Println("Ping failed: " + err.Error())
		} else {
			fmt.Println("Pinged successfully")
		}

		time.Sleep(10 * time.Second)
	}
}
$ docker stop 806185fd82bb && docker start 806185fd82bb
806185fd82bb
806185fd82bb

we would start receiving errors forever:

Pinged successfully
Pinged successfully
Ping failed: commands out of sync. You can't run this command now
[mysql] 2019/02/18 19:15:14 packets.go:393: busy buffer
Ping failed: bad connection
[mysql] 2019/02/18 19:15:24 packets.go:393: busy buffer
Ping failed: bad connection
[mysql] 2019/02/18 19:15:34 packets.go:393: busy buffer
Ping failed: bad connection

But if we added any Exec() call after the Ping() call

func main() {
	conn, _ := sql.Open("mysql", "root:toor@tcp(127.0.0.1:3306)/?charset=utf8&parseTime=true")

	for {
		err := conn.Ping()
		conn.Exec("SELECT NOW()")

		if err != nil {
			fmt.Println("Ping failed: " + err.Error())
		} else {
			fmt.Println("Pinged successfully")
		}

		time.Sleep(10 * time.Second)
	}
}

it would solve the problem

$ docker stop 806185fd82bb && docker start 806185fd82bb
806185fd82bb
806185fd82bb
Pinged successfully
Pinged successfully
[mysql] 2019/02/18 19:19:35 packets.go:412: busy buffer
[mysql] 2019/02/18 19:19:35 packets.go:393: busy buffer
Ping failed: commands out of sync. You can't run this command now
Pinged successfully
Pinged successfully

Configuration

  • driver: 72cd26f
  • go: go1.11.1 darwin/amd64
  • Server version: mariadb:10.2.9
@magodo
Copy link

magodo commented Feb 20, 2019

Same problem here.

I suppose in this case you should Close() and Open() by yourself. If this is the case, I think this is not meeting the expectation from POV of database/sql since it says here:

Thus, the Open function should be called just once. It is rarely necessary to close a DB.

@methane
Copy link
Member

methane commented Feb 20, 2019

Have you tried master branch?
Isn't this issue duplicate of #875?

@dmitriivoitovich
Copy link
Author

Have just tried the latest version of master branch 972a708cf97995463843c08c8585b26997daf0e1 and it looks like the issue has already been fixed there:

Pinged successfully
Pinged successfully
Pinged successfully
# here the docker container was restarted...
Ping failed: commands out of sync. You can't run this command now
Ping failed: driver: bad connection
[mysql] 2019/02/20 11:25:59 packets.go:397: busy buffer
[mysql] 2019/02/20 11:25:59 packets.go:397: busy buffer
Pinged successfully
Pinged successfully
Pinged successfully

Sorry for your time. I think the issue could be closed now.

@methane methane closed this as completed Feb 20, 2019
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

No branches or pull requests

3 participants