Skip to content

Commit

Permalink
meta/mysql: set defalut transation_isolation to repeatable-read (#5432)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuyuchao-juice authored Dec 25, 2024
1 parent d54c653 commit 1297700
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/meta/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ func recoveryMysqlPwd(addr string) string {
return addr
}

var setTransationIsolation func(dns string) (string, error)

func newSQLMeta(driver, addr string, conf *Config) (Meta, error) {
var searchPath string
if driver == "postgres" {
Expand All @@ -285,8 +287,12 @@ func newSQLMeta(driver, addr string, conf *Config) (Meta, error) {
}

// escaping is not necessary for mysql password https://github.com/go-sql-driver/mysql#password
if driver == "mysql" {
if driver == "mysql" && setTransationIsolation != nil {
addr = recoveryMysqlPwd(addr)
var err error
if addr, err = setTransationIsolation(addr); err != nil {
return nil, err
}
}

if driver == "sqlite3" {
Expand Down
13 changes: 13 additions & 0 deletions pkg/meta/sql_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,20 @@ func isMySQLDuplicateEntryErr(err error) bool {
return false
}

func setMySQLTransationIsolation(dns string) (string, error) {
cfg, err := mysql.ParseDSN(dns)
if err != nil {
return "", err
}
if cfg.Params == nil {
cfg.Params = make(map[string]string)
}
cfg.Params["transaction_isolation"] = "'repeatable-read'"
return cfg.FormatDSN(), nil
}

func init() {
dupErrorCheckers = append(dupErrorCheckers, isMySQLDuplicateEntryErr)
setTransationIsolation = setMySQLTransationIsolation
Register("mysql", newSQLMeta)
}

0 comments on commit 1297700

Please sign in to comment.