Add sql.DB to Postgres and Mysql structs to allow closing in Close()

This commit is contained in:
Kay-Zee 2018-09-06 17:16:02 -07:00
parent 93d53a5ae8
commit 1e4c50b70d
No known key found for this signature in database
GPG Key ID: 3F14F947FDA6D48C
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