Skip to content

Commit

Permalink
Merge pull request #460 from VojtechVitek/master
Browse files Browse the repository at this point in the history
Fix deadlock in sqladapter
  • Loading branch information
VojtechVitek authored Aug 29, 2018
2 parents d90922b + d53b67f commit 199d13d
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions internal/sqladapter/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,23 @@ func NewBaseDatabase(p PartialDatabase) BaseDatabase {
// BaseDatabase and PartialDatabase
type database struct {
PartialDatabase
baseTx BaseTx

db.Settings

lookupNameOnce sync.Once
name string

mu sync.Mutex // guards ctx, txOptions
ctx context.Context
txOptions *sql.TxOptions

collectionMu sync.Mutex
mu sync.Mutex

name string
sessMu sync.Mutex // guards sess, baseTx
sess *sql.DB
sessMu sync.Mutex

psMu sync.Mutex
baseTx BaseTx

sessID uint64
txID uint64

cacheMu sync.Mutex // guards cachedStatements and cachedCollections
cachedStatements *cache.Cache
cachedCollections *cache.Cache

Expand Down Expand Up @@ -243,12 +241,11 @@ func (d *database) Transaction() BaseTx {

// Name returns the database named
func (d *database) Name() string {
d.mu.Lock()
defer d.mu.Unlock()

if d.name == "" {
d.name, _ = d.PartialDatabase.LookupName()
}
d.lookupNameOnce.Do(func() {
if d.name == "" {
d.name, _ = d.PartialDatabase.LookupName()
}
})

return d.name
}
Expand Down Expand Up @@ -312,8 +309,8 @@ func (d *database) SetMaxOpenConns(n int) {

// ClearCache removes all caches.
func (d *database) ClearCache() {
d.collectionMu.Lock()
defer d.collectionMu.Unlock()
d.cacheMu.Lock()
defer d.cacheMu.Unlock()
d.cachedCollections.Clear()
d.cachedStatements.Clear()
if d.template != nil {
Expand Down Expand Up @@ -376,8 +373,8 @@ func (d *database) Close() error {

// Collection returns a db.Collection given a name. Results are cached.
func (d *database) Collection(name string) db.Collection {
d.collectionMu.Lock()
defer d.collectionMu.Unlock()
d.cacheMu.Lock()
defer d.cacheMu.Unlock()

h := cache.String(name)

Expand Down

0 comments on commit 199d13d

Please sign in to comment.