Skip to content

Commit

Permalink
proper lock fix for db driver
Browse files Browse the repository at this point in the history
  • Loading branch information
l3pp4rd committed Dec 28, 2021
1 parent c598687 commit 0e9ad8c
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,12 @@ func (d *txDriver) Open(dsn string) (driver.Conn, error) {
}
d.conns[dsn] = c
}
c.Lock()
defer c.Unlock()
c.opened++
c.opened++ // safe since conn.Close() must acquire driver lock first
return c, nil
}

func (d *txDriver) deleteConn(dsn string) error {
d.Lock()
defer d.Unlock()

// d must be locked before call
delete(d.conns, dsn)
if len(d.conns) == 0 && d.db != nil {
if err := d.db.Close(); err != nil {
Expand All @@ -177,8 +173,8 @@ func (c *conn) beginOnce() (*sql.Tx, error) {
}

func (c *conn) Close() (err error) {
c.Lock()
defer c.Unlock()
c.drv.Lock()
defer c.drv.Unlock()

c.opened--
if c.opened == 0 {
Expand Down

0 comments on commit 0e9ad8c

Please sign in to comment.