diff --git a/go/test/endtoend/vtgate/queries/misc/misc_test.go b/go/test/endtoend/vtgate/queries/misc/misc_test.go index 465b0adf883..eca46cfcc29 100644 --- a/go/test/endtoend/vtgate/queries/misc/misc_test.go +++ b/go/test/endtoend/vtgate/queries/misc/misc_test.go @@ -284,3 +284,36 @@ func TestAnalyze(t *testing.T) { }) } } + +// TestTransactionModeVar executes SELECT on `transaction_mode` variable +func TestTransactionModeVar(t *testing.T) { + utils.SkipIfBinaryIsBelowVersion(t, 19, "vtgate") + + mcmp, closer := start(t) + defer closer() + + tcases := []struct { + setStmt string + expRes string + }{{ + expRes: `[[VARCHAR("MULTI")]]`, + }, { + setStmt: `set transaction_mode = single`, + expRes: `[[VARCHAR("SINGLE")]]`, + }, { + setStmt: `set transaction_mode = multi`, + expRes: `[[VARCHAR("MULTI")]]`, + }, { + setStmt: `set transaction_mode = twopc`, + expRes: `[[VARCHAR("TWOPC")]]`, + }} + + for _, tcase := range tcases { + t.Run(tcase.setStmt, func(t *testing.T) { + if tcase.setStmt != "" { + utils.Exec(t, mcmp.VtConn, tcase.setStmt) + } + utils.AssertMatches(t, mcmp.VtConn, "select @@transaction_mode", tcase.expRes) + }) + } +} diff --git a/go/vt/vtgate/executor.go b/go/vt/vtgate/executor.go index c395fedacfe..867685b8fec 100644 --- a/go/vt/vtgate/executor.go +++ b/go/vt/vtgate/executor.go @@ -461,7 +461,11 @@ func (e *Executor) addNeededBindVars(vcursor *vcursorImpl, bindVarNeeds *sqlpars }) bindVars[key] = sqltypes.Int64BindVariable(v) case sysvars.TransactionMode.Name: - bindVars[key] = sqltypes.StringBindVariable(session.TransactionMode.String()) + txMode := session.TransactionMode + if txMode == vtgatepb.TransactionMode_UNSPECIFIED { + txMode = getTxMode() + } + bindVars[key] = sqltypes.StringBindVariable(txMode.String()) case sysvars.Workload.Name: var v string ifOptionsExist(session, func(options *querypb.ExecuteOptions) { diff --git a/go/vt/vtgate/executor_select_test.go b/go/vt/vtgate/executor_select_test.go index dd214f9dfae..9603b5da5bc 100644 --- a/go/vt/vtgate/executor_select_test.go +++ b/go/vt/vtgate/executor_select_test.go @@ -801,7 +801,7 @@ func TestSelectSystemVariables(t *testing.T) { sqltypes.NewInt64(0), sqltypes.NewInt64(0), sqltypes.NewInt64(0), - sqltypes.NewVarChar("UNSPECIFIED"), + sqltypes.NewVarChar("MULTI"), sqltypes.NewVarChar(""), // these have been set at the beginning of the test sqltypes.NewVarChar("a fine gtid"),