{"id":"eb1195f79e0b815d","repo":"go-sql-driver/mysql","slug":"mysql-unsupported-isolation-level-v","errorCode":null,"errorMessage":"mysql: unsupported isolation level: %v","messagePattern":"mysql: unsupported isolation level: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"utils.go","lineNumber":803,"sourceCode":"\t\t\treturn nil, errors.New(\"mysql: driver does not support the use of Named Parameters\")\n\t\t}\n\t\tdargs[n] = param.Value\n\t}\n\treturn dargs, nil\n}\n\nfunc mapIsolationLevel(level driver.IsolationLevel) (string, error) {\n\tswitch sql.IsolationLevel(level) {\n\tcase sql.LevelRepeatableRead:\n\t\treturn \"REPEATABLE READ\", nil\n\tcase sql.LevelReadCommitted:\n\t\treturn \"READ COMMITTED\", nil\n\tcase sql.LevelReadUncommitted:\n\t\treturn \"READ UNCOMMITTED\", nil\n\tcase sql.LevelSerializable:\n\t\treturn \"SERIALIZABLE\", nil\n\tdefault:\n\t\treturn \"\", fmt.Errorf(\"mysql: unsupported isolation level: %v\", level)\n\t}\n}\n","sourceCodeStart":785,"sourceCodeEnd":806,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/c426bd93799de0f0e094c8f0582872c529d0ed0a/utils.go#L785-L806","documentation":"Thrown by mapIsolationLevel (utils.go:803) when a transaction's isolation level is not one of the four MySQL supports: ReadUncommitted, ReadCommitted, RepeatableRead, Serializable. MySQL has no equivalent for levels like Snapshot or Linearizable, so the driver cannot map them and rejects the begin.","triggerScenarios":"Calling db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelSnapshot}) (or any non-default, non-MySQL-supported level). Because LevelDefault is handled implicitly by MySQL, only the four named levels plus the default are accepted.","commonSituations":"Porting code from PostgreSQL/SQL Server that used LevelSnapshot or LevelLinearizable; an ORM defaulting to an isolation level MySQL doesn't have; copy-paste of TxOptions from another DB's example.","solutions":["Use one of: sql.LevelReadUncommitted, sql.LevelReadCommitted, sql.LevelRepeatableRead, sql.LevelSerializable, or leave Isolation unset (LevelDefault).","If you need snapshot-style behavior, use REPEATABLE READ (MySQL's default for InnoDB) or START TRANSACTION WITH CONSISTENT SNAPSHOT via raw exec.","Review your ORM's transaction options mapping to ensure it only requests MySQL-supported levels."],"exampleFix":"// before\ntx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelSnapshot})\n\n// after\ntx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelRepeatableRead})","handlingStrategy":"validation","validationCode":"// whitelist isolation levels MySQL supports before BeginTx\nvar mysqlIsolation = map[sql.IsolationLevel]bool{\n    sql.LevelReadUncommitted: true,\n    sql.LevelReadCommitted:   true,\n    sql.LevelRepeatableRead:  true,\n    sql.LevelSerializable:   true,\n}\n\nfunc beginTxSafe(db *sql.DB, ctx context.Context, iso sql.IsolationLevel) (*sql.Tx, error) {\n    opts := &sql.TxOptions{}\n    if iso != sql.LevelDefault {\n        if !mysqlIsolation[iso] {\n            return nil, fmt.Errorf(\"isolation %v not supported by mysql\", iso)\n        }\n        opts.Isolation = iso\n    }\n    return db.BeginTx(ctx, opts)\n}","typeGuard":"// isMySQLIsolationLevel narrows to the levels the driver accepts\nfunc isMySQLIsolationLevel(l sql.IsolationLevel) bool {\n    switch l {\n    case sql.LevelDefault, sql.LevelReadUncommitted, sql.LevelReadCommitted,\n        sql.LevelRepeatableRead, sql.LevelSerializable:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Restrict TxOptions.Isolation to the four MySQL-supported levels or LevelDefault.","When porting from another DB, audit transaction options for unsupported levels like Snapshot.","Centralize BeginTx behind a helper that validates the isolation level."],"tags":["transaction","isolation-level","api-misuse"],"analyzedSha":"c426bd93799de0f0e094c8f0582872c529d0ed0a","analyzedAt":"2026-08-04T21:52:59.219Z","schemaVersion":2}