@@ -334,7 +334,7 @@ BerkeleyDatabase::~BerkeleyDatabase()
334334 }
335335}
336336
337- BerkeleyBatch::BerkeleyBatch (BerkeleyDatabase& database, const char * pszMode, bool fFlushOnCloseIn ) : pdb(nullptr ), activeTxn(nullptr ), m_cursor(nullptr )
337+ BerkeleyBatch::BerkeleyBatch (BerkeleyDatabase& database, const char * pszMode, bool fFlushOnCloseIn ) : pdb(nullptr ), activeTxn(nullptr ), m_cursor(nullptr ), m_database(database)
338338{
339339 fReadOnly = (!strchr (pszMode, ' +' ) && !strchr (pszMode, ' w' ));
340340 fFlushOnClose = fFlushOnCloseIn ;
@@ -408,7 +408,7 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo
408408 fReadOnly = fTmp ;
409409 }
410410 }
411- ++env-> mapFileUseCount [strFilename] ;
411+ database. AddRef () ;
412412 strFile = strFilename;
413413 }
414414}
@@ -446,11 +446,7 @@ void BerkeleyBatch::Close()
446446 if (fFlushOnClose )
447447 Flush ();
448448
449- {
450- LOCK (cs_db);
451- --env->mapFileUseCount [strFile];
452- }
453- env->m_db_in_use .notify_all ();
449+ m_database.RemoveRef ();
454450}
455451
456452void BerkeleyEnvironment::CloseDb (const std::string& strFile)
@@ -846,6 +842,21 @@ bool BerkeleyBatch::HasKey(CDataStream&& key)
846842 return ret == 0 ;
847843}
848844
845+ void BerkeleyDatabase::AddRef ()
846+ {
847+ LOCK (cs_db);
848+ ++env->mapFileUseCount [strFile];
849+ }
850+
851+ void BerkeleyDatabase::RemoveRef ()
852+ {
853+ {
854+ LOCK (cs_db);
855+ --env->mapFileUseCount [strFile];
856+ }
857+ env->m_db_in_use .notify_all ();
858+ }
859+
849860std::unique_ptr<BerkeleyBatch> BerkeleyDatabase::MakeBatch (const char * mode, bool flush_on_close)
850861{
851862 return MakeUnique<BerkeleyBatch>(*this , mode, flush_on_close);
0 commit comments