Browse Source

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

Liang Ding 2 years ago
parent
commit
10407d54d6
2 changed files with 17 additions and 10 deletions
  1. 1 4
      kernel/server/serve.go
  2. 16 6
      kernel/sql/database.go

+ 1 - 4
kernel/server/serve.go

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

+ 16 - 6
kernel/sql/database.go

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