diff --git a/pkg/meta/sql.go b/pkg/meta/sql.go index 87ab1e3ba6d2..169aae5ef7a7 100644 --- a/pkg/meta/sql.go +++ b/pkg/meta/sql.go @@ -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" { @@ -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" { diff --git a/pkg/meta/sql_mysql.go b/pkg/meta/sql_mysql.go index 373c6cb50cc3..923cd855d908 100644 --- a/pkg/meta/sql_mysql.go +++ b/pkg/meta/sql_mysql.go @@ -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) }