-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MySQL driver: on connect try setting wsrep_sync_wait=4, swallow error…
… 1193 In Galera clusters wsrep_sync_wait=4 ensures inserted rows to be synced over all nodes before reporting success to their inserter. That allows inserting child rows immediately after that on another node without running into foreign key errors. MySQL single nodes will reject this with error 1193 "Unknown system variable" which is OK.
- Loading branch information
Showing
2 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package driver | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
"github.com/go-sql-driver/mysql" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var errUnknownSysVar = &mysql.MySQLError{Number: 1193} | ||
|
||
// setGaleraOpts tries SET SESSION wsrep_sync_wait=4. | ||
// Error 1193 "Unknown system variable" is ignored to support MySQL single nodes. | ||
func setGaleraOpts(ctx context.Context, conn driver.Conn) error { | ||
const galeraOpts = "SET SESSION wsrep_sync_wait=4" | ||
|
||
stmt, err := conn.(driver.ConnPrepareContext).PrepareContext(ctx, galeraOpts) | ||
if err != nil { | ||
err = errors.Wrap(err, "can't prepare "+galeraOpts) | ||
} else if _, err = stmt.(driver.StmtExecContext).ExecContext(ctx, nil); err != nil { | ||
err = errors.Wrap(err, "can't execute "+galeraOpts) | ||
} | ||
|
||
if err != nil && errors.Is(err, errUnknownSysVar) { | ||
err = nil | ||
} | ||
|
||
if stmt != nil { | ||
if errClose := stmt.Close(); errClose != nil && err == nil { | ||
err = errors.Wrap(errClose, "can't close statement "+galeraOpts) | ||
} | ||
} | ||
|
||
return err | ||
} |