Skip to content

Commit 420fbff

Browse files
authored
Merge pull request #51 from Icinga/wsrep_sync_wait-prepare-execute
setGaleraOpts(): execute SET SESSION wsrep_sync_wait directly
2 parents 87fa4f2 + 87f219b commit 420fbff

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

database/utils.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/icinga/icinga-go-library/strcase"
99
"github.com/icinga/icinga-go-library/types"
1010
"github.com/pkg/errors"
11+
"strconv"
1112
)
1213

1314
// CantPerformQuery wraps the given error with the specified query that cannot be executed.
@@ -50,29 +51,17 @@ func SplitOnDupId[T IDer]() com.BulkChunkSplitPolicy[T] {
5051
//
5152
// https://mariadb.com/kb/en/galera-cluster-system-variables/#wsrep_sync_wait
5253
func setGaleraOpts(ctx context.Context, conn driver.Conn, wsrepSyncWait int64) error {
53-
const galeraOpts = "SET SESSION wsrep_sync_wait=?"
54+
galeraOpts := "SET SESSION wsrep_sync_wait=" + strconv.FormatInt(wsrepSyncWait, 10)
5455

55-
stmt, err := conn.(driver.ConnPrepareContext).PrepareContext(ctx, galeraOpts)
56+
_, err := conn.(driver.ExecerContext).ExecContext(ctx, galeraOpts, nil)
5657
if err != nil {
5758
if errors.Is(err, &mysql.MySQLError{Number: 1193}) { // Unknown system variable
5859
return nil
5960
}
6061

61-
return errors.Wrap(err, "cannot prepare "+galeraOpts)
62-
}
63-
// This is just for an unexpected exit and any returned error can safely be ignored and in case
64-
// of the normal function exit, the stmt is closed manually, and its error is handled gracefully.
65-
defer func() { _ = stmt.Close() }()
66-
67-
_, err = stmt.(driver.StmtExecContext).ExecContext(ctx, []driver.NamedValue{{Value: wsrepSyncWait}})
68-
if err != nil {
6962
return errors.Wrap(err, "cannot execute "+galeraOpts)
7063
}
7164

72-
if err = stmt.Close(); err != nil {
73-
return errors.Wrap(err, "cannot close prepared statement "+galeraOpts)
74-
}
75-
7665
return nil
7766
}
7867

0 commit comments

Comments
 (0)