jmoiron / sqlx

general purpose extensions to golang's database/sql
http://jmoiron.github.io/sqlx/
MIT License
16.3k stars 1.09k forks source link

ConnectContext should close the db when PingContext returns an error #910

Open chad-wheeler-sp opened 9 months ago

chad-wheeler-sp commented 9 months ago

ConnectContext() should close the db when PingContext() returns an error.

The Open() function does not test the validity of the actual connection....auth or host....but does create a routine to handle opening connection through the sql.Open()->sql.OpenDB() statement. When PingContext() return an error due to bad auth or host connectivity then the wrapped db with the open routine is returned along with the error. It is not obvious that the db returned from the ConnectContext should be closed when an error is returned.

The Connect() function is executing the db.Close() so why doesn't the ConnectContext()?

Solution should be just as Connect() in that the db.Close() is executed and nil returned for the db.