🐛 2.7.1dev3 重建索引时报数据库被锁定 https://github.com/siyuan-note/siyuan/issues/7167

This commit is contained in:
Liang Ding 2023-01-26 11:56:06 +08:00
parent bea767d4a6
commit 10407d54d6
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 17 additions and 10 deletions

View file

@ -63,10 +63,7 @@ func Serve(fastMode bool) {
})
ginServer.Use(sessions.Sessions("siyuan", cookieStore))
if "dev" == util.Mode {
serveDebug(ginServer)
}
serveDebug(ginServer)
serveAssets(ginServer)
serveAppearance(ginServer)
serveWebSocket(ginServer)

View file

@ -23,6 +23,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
"unicode/utf8"
@ -65,7 +66,6 @@ func InitDatabase(forceRebuild bool) (err error) {
if forceRebuild {
ClearQueue()
WaitForWritingDatabase()
}
initDBConnection()
@ -80,12 +80,12 @@ func InitDatabase(forceRebuild bool) (err error) {
// 不存在库或者版本不一致都会走到这里
db.Close()
closeDatabase()
if gulu.File.IsExist(util.DBPath) {
if err = removeDatabaseFile(); nil != err {
logging.LogErrorf("remove database file [%s] failed: %s", util.DBPath, err)
util.PushClearProgress()
return
err = nil
}
}
if gulu.File.IsExist(util.BlockTreePath) {
@ -209,7 +209,7 @@ func initHistoryDBTables() {
func initDBConnection() {
if nil != db {
db.Close()
closeDatabase()
}
dsn := util.DBPath + "?_journal_mode=WAL" +
"&_synchronous=OFF" +
@ -1023,7 +1023,7 @@ func batchUpdateHPath(tx *sql.Tx, boxID, rootID, oldHPath, newHPath string) (err
}
func CloseDatabase() {
if err := db.Close(); nil != err {
if err := closeDatabase(); nil != err {
logging.LogErrorf("close database failed: %s", err)
return
}
@ -1111,7 +1111,7 @@ func execStmtTx(tx *sql.Tx, stmt string, args ...interface{}) (err error) {
if _, err = tx.Exec(stmt, args...); nil != err {
if strings.Contains(err.Error(), "database disk image is malformed") {
tx.Rollback()
db.Close()
closeDatabase()
removeDatabaseFile()
logging.LogFatalf("database disk image [%s] is malformed, please restart SiYuan kernel to rebuild it", util.DBPath)
}
@ -1180,3 +1180,13 @@ func removeDatabaseFile() (err error) {
}
return
}
func closeDatabase() (err error) {
if nil == db {
return
}
err = db.Close()
runtime.GC() // 没有这句的话文件句柄不会释放,后面就无法删除文件
return
}