@@ -151,25 +151,23 @@ func NewBaseDatabase(p PartialDatabase) BaseDatabase {
151
151
// BaseDatabase and PartialDatabase
152
152
type database struct {
153
153
PartialDatabase
154
- baseTx BaseTx
155
-
156
154
db.Settings
157
155
156
+ lookupNameOnce sync.Once
157
+ name string
158
+
159
+ mu sync.Mutex // guards ctx, txOptions
158
160
ctx context.Context
159
161
txOptions * sql.TxOptions
160
162
161
- collectionMu sync.Mutex
162
- mu sync.Mutex
163
-
164
- name string
163
+ sessMu sync.Mutex // guards sess, baseTx
165
164
sess * sql.DB
166
- sessMu sync.Mutex
167
-
168
- psMu sync.Mutex
165
+ baseTx BaseTx
169
166
170
167
sessID uint64
171
168
txID uint64
172
169
170
+ cacheMu sync.Mutex // guards cachedStatements and cachedCollections
173
171
cachedStatements * cache.Cache
174
172
cachedCollections * cache.Cache
175
173
@@ -243,12 +241,11 @@ func (d *database) Transaction() BaseTx {
243
241
244
242
// Name returns the database named
245
243
func (d * database ) Name () string {
246
- d .mu .Lock ()
247
- defer d .mu .Unlock ()
248
-
249
- if d .name == "" {
250
- d .name , _ = d .PartialDatabase .LookupName ()
251
- }
244
+ d .lookupNameOnce .Do (func () {
245
+ if d .name == "" {
246
+ d .name , _ = d .PartialDatabase .LookupName ()
247
+ }
248
+ })
252
249
253
250
return d .name
254
251
}
@@ -312,8 +309,8 @@ func (d *database) SetMaxOpenConns(n int) {
312
309
313
310
// ClearCache removes all caches.
314
311
func (d * database ) ClearCache () {
315
- d .collectionMu .Lock ()
316
- defer d .collectionMu .Unlock ()
312
+ d .cacheMu .Lock ()
313
+ defer d .cacheMu .Unlock ()
317
314
d .cachedCollections .Clear ()
318
315
d .cachedStatements .Clear ()
319
316
if d .template != nil {
@@ -376,8 +373,8 @@ func (d *database) Close() error {
376
373
377
374
// Collection returns a db.Collection given a name. Results are cached.
378
375
func (d * database ) Collection (name string ) db.Collection {
379
- d .collectionMu .Lock ()
380
- defer d .collectionMu .Unlock ()
376
+ d .cacheMu .Lock ()
377
+ defer d .cacheMu .Unlock ()
381
378
382
379
h := cache .String (name )
383
380
0 commit comments