Skip to content

Commit 0280d98

Browse files
committed
sqlite3 API: Prevent that busy DB are left open
If we follow the steps described in sqlitebrowser#2910 (comment) we get this warning: "Database didn't close correctly, probably still busy" The error has not been prevented, but by using sqlite3_close_v2 instead of sqlite3_close, we can assure that the connection is eventually released.
1 parent f324729 commit 0280d98

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/sqlitedb.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ bool DBBrowserDB::tryEncryptionSettings(const QString& filePath, bool* encrypted
438438
if(err == SQLITE_BUSY || err == SQLITE_PERM || err == SQLITE_NOMEM || err == SQLITE_IOERR || err == SQLITE_CORRUPT || err == SQLITE_CANTOPEN)
439439
{
440440
lastErrorMessage = QString::fromUtf8(sqlite3_errmsg(dbHandle));
441-
sqlite3_close(dbHandle);
441+
sqlite3_close_v2(dbHandle);
442442
return false;
443443
}
444444

@@ -498,14 +498,14 @@ bool DBBrowserDB::tryEncryptionSettings(const QString& filePath, bool* encrypted
498498
{
499499
*cipherSettings = cipherDialog->getCipherSettings();
500500
} else {
501-
sqlite3_close(dbHandle);
501+
sqlite3_close_v2(dbHandle);
502502
*encrypted = false;
503503
return false;
504504
}
505505
}
506506

507507
// Close and reopen database first to be in a clean state after the failed read attempt from above
508-
sqlite3_close(dbHandle);
508+
sqlite3_close_v2(dbHandle);
509509
if(sqlite3_open_v2(filePath.toUtf8(), &dbHandle, SQLITE_OPEN_READONLY, nullptr) != SQLITE_OK)
510510
return false;
511511

@@ -527,12 +527,12 @@ bool DBBrowserDB::tryEncryptionSettings(const QString& filePath, bool* encrypted
527527
*encrypted = true;
528528
#else
529529
lastErrorMessage = QString::fromUtf8(sqlite3_errmsg(dbHandle));
530-
sqlite3_close(dbHandle);
530+
sqlite3_close_v2(dbHandle);
531531
return false;
532532
#endif
533533
} else {
534534
sqlite3_finalize(vm);
535-
sqlite3_close(dbHandle);
535+
sqlite3_close_v2(dbHandle);
536536
return true;
537537
}
538538
}
@@ -559,7 +559,7 @@ void DBBrowserDB::getSqliteVersion(QString& sqlite, QString& sqlcipher)
559559
sqlite3_finalize(stmt);
560560
}
561561

562-
sqlite3_close(dummy);
562+
sqlite3_close_v2(dummy);
563563
}
564564
#endif
565565
}
@@ -668,7 +668,7 @@ bool DBBrowserDB::create ( const QString & db)
668668

669669
if( openresult != SQLITE_OK ){
670670
lastErrorMessage = QString::fromUtf8(sqlite3_errmsg(_db));
671-
sqlite3_close(_db);
671+
sqlite3_close_v2(_db);
672672
_db = nullptr;
673673
return false;
674674
}
@@ -686,7 +686,7 @@ bool DBBrowserDB::create ( const QString & db)
686686

687687
// Close database and open it through the code for opening existing database files. This is slightly less efficient but saves us some duplicate
688688
// code.
689-
sqlite3_close(_db);
689+
sqlite3_close_v2(_db);
690690
return open(db);
691691
} else {
692692
return false;
@@ -731,7 +731,7 @@ bool DBBrowserDB::close()
731731
revertAll(); //not really necessary, I think... but will not hurt.
732732
}
733733

734-
if(sqlite3_close(_db) != SQLITE_OK)
734+
if(sqlite3_close_v2(_db) != SQLITE_OK)
735735
qWarning() << tr("Database didn't close correctly, probably still busy");
736736

737737
_db = nullptr;
@@ -775,7 +775,7 @@ bool DBBrowserDB::saveAs(const std::string& filename) {
775775
sqlite3_backup* pBackup = sqlite3_backup_init(pTo, "main", _db, "main");
776776
if(pBackup == nullptr) {
777777
qWarning() << tr("Cannot backup to file: '%1'. Message: %2").arg(filename.c_str(), sqlite3_errmsg(pTo));
778-
sqlite3_close(pTo);
778+
sqlite3_close_v2(pTo);
779779
return false;
780780
} else {
781781
sqlite3_backup_step(pBackup, -1);
@@ -786,15 +786,15 @@ bool DBBrowserDB::saveAs(const std::string& filename) {
786786

787787
if(rc == SQLITE_OK) {
788788
// Close current database and set backup as current
789-
sqlite3_close(_db);
789+
sqlite3_close_v2(_db);
790790
_db = pTo;
791791
curDBFilename = QString::fromStdString(filename);
792792

793793
return true;
794794
} else {
795795
qWarning() << tr("Cannot backup to file: '%1'. Message: %2").arg(filename.c_str(), sqlite3_errmsg(pTo));
796796
// Close failed database connection.
797-
sqlite3_close(pTo);
797+
sqlite3_close_v2(pTo);
798798
return false;
799799
}
800800
}

0 commit comments

Comments
 (0)