Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close db connection before reload and rename to avoid confusion #13122

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions pkg/util/export/etl/db/db_holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,22 @@ func SetDBConn(conn *sql.DB) {
db.Store(conn)
}

func InitOrRefreshDBConn(forceNewConn bool, randomCN bool) (*sql.DB, error) {
func CloseDBConn() {
dbVal := db.Load()
if dbVal == nil {
return
}
dbConn := dbVal.(*sql.DB)
if dbConn != nil {
dbConn.Close()
}
}

func GetOrInitDBConn(forceNewConn bool, randomCN bool) (*sql.DB, error) {
initFunc := func() error {
dbMux.Lock()
defer dbMux.Unlock()
CloseDBConn()
dbUser, _ := GetSQLWriterDBUser()
if dbUser == nil {
return errNotReady
Expand Down Expand Up @@ -137,13 +149,10 @@ func WriteRowRecords(records [][]string, tbl *table.Table, timeout time.Duration
var dbConn *sql.DB

if DBConnErrCount.Load() > DBConnRetryThreshold {
if dbConn != nil {
dbConn.Close()
}
dbConn, err = InitOrRefreshDBConn(true, true)
dbConn, err = GetOrInitDBConn(true, true)
DBConnErrCount.Store(0)
} else {
dbConn, err = InitOrRefreshDBConn(false, false)
dbConn, err = GetOrInitDBConn(false, false)
}
if err != nil {
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/export/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (m *Merge) doMergeFiles(ctx context.Context, files []*FileMeta) error {
}

// Check if the first record already exists in the database
existed, err = db_holder.IsRecordExisted(ctx, firstLine, m.table, db_holder.InitOrRefreshDBConn)
existed, err = db_holder.IsRecordExisted(ctx, firstLine, m.table, db_holder.GetOrInitDBConn)
if err != nil {
m.logger.Error("error checking if the first record exists",
logutil.TableField(m.table.GetIdentify()),
Expand Down