mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-17 19:00:25 +08:00
程序意外退出时关闭sqlite指针
This commit is contained in:
@@ -5,6 +5,8 @@ package dbs
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/events"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
@@ -14,9 +16,15 @@ type DB struct {
|
||||
}
|
||||
|
||||
func NewDB(rawDB *sql.DB) *DB {
|
||||
return &DB{
|
||||
var db = &DB{
|
||||
rawDB: rawDB,
|
||||
}
|
||||
|
||||
events.OnKey(events.EventTerminated, fmt.Sprintf("db_%p", db), func() {
|
||||
_ = rawDB.Close()
|
||||
})
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func (this *DB) EnableStat(b bool) {
|
||||
@@ -65,5 +73,6 @@ func (this *DB) QueryRow(query string, args ...interface{}) *sql.Row {
|
||||
}
|
||||
|
||||
func (this *DB) Close() error {
|
||||
events.Remove(fmt.Sprintf("db_%p", this))
|
||||
return this.rawDB.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user