Merge pull request #98 from Kay-Zee/Kay-Zee/close-sql-db-connections-on-driver-close

Add sql.DB to Postgres and Mysql structs to allow closing in Close()
This commit is contained in:
Dale Hui 2018-09-07 01:26:43 -07:00 committed by GitHub
commit f8c85735f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 6 deletions

View File

@ -47,6 +47,7 @@ type Mysql struct {
// mysql RELEASE_LOCK must be called from the same conn, so
// just do everything over a single conn anyway.
conn *sql.Conn
db *sql.DB
isLocked bool
config *Config
@ -85,6 +86,7 @@ func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {
mx := &Mysql{
conn: conn,
db: instance,
config: config,
}
@ -193,7 +195,12 @@ func (m *Mysql) Open(url string) (database.Driver, error) {
}
func (m *Mysql) Close() error {
return m.conn.Close()
connErr := m.conn.Close()
dbErr := m.db.Close()
if connErr != nil || dbErr != nil {
return fmt.Errorf("conn: %v, db: %v", connErr, dbErr)
}
return nil
}
func (m *Mysql) Lock() error {

View File

@ -40,6 +40,7 @@ type Config struct {
type Postgres struct {
// Locking and unlocking need to use the same connection
conn *sql.Conn
db *sql.DB
isLocked bool
// Open and WithInstance need to garantuee that config is never nil
@ -79,6 +80,7 @@ func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {
px := &Postgres{
conn: conn,
db: instance,
config: config,
}
@ -117,7 +119,12 @@ func (p *Postgres) Open(url string) (database.Driver, error) {
}
func (p *Postgres) Close() error {
return p.conn.Close()
connErr := p.conn.Close()
dbErr := p.db.Close()
if connErr != nil || dbErr != nil {
return fmt.Errorf("conn: %v, db: %v", connErr, dbErr)
}
return nil
}
// https://www.postgresql.org/docs/9.6/static/explicit-locking.html#ADVISORY-LOCKS