@@ -22,6 +22,7 @@ import (
2222var (
2323 errTxAlreadyCommitted = xerrors .Wrap (fmt .Errorf ("transaction already committed" ))
2424 errTxRollbackedEarly = xerrors .Wrap (fmt .Errorf ("transaction rollbacked early" ))
25+ errTxFailedEarly = xerrors .Wrap (fmt .Errorf ("transaction failed early" ))
2526)
2627
2728type txState struct {
@@ -42,6 +43,7 @@ const (
4243 txStateInitialized txStateEnum = iota
4344 txStateCommitted
4445 txStateRollbacked
46+ txStateFailed
4547)
4648
4749type transaction struct {
@@ -73,11 +75,15 @@ func (tx *transaction) Execute(
7375 switch tx .state .Load () {
7476 case txStateCommitted :
7577 return nil , xerrors .WithStackTrace (errTxAlreadyCommitted )
78+ case txStateFailed :
79+ return nil , xerrors .WithStackTrace (errTxFailedEarly )
7680 case txStateRollbacked :
7781 return nil , xerrors .WithStackTrace (errTxRollbackedEarly )
7882 default :
7983 _ , r , err = tx .s .Execute (ctx , tx .control , query , parameters , opts ... )
8084 if err != nil {
85+ tx .state .Store (txStateFailed )
86+
8187 return nil , xerrors .WithStackTrace (err )
8288 }
8389
@@ -115,11 +121,15 @@ func (tx *transaction) ExecuteStatement(
115121 switch tx .state .Load () {
116122 case txStateCommitted :
117123 return nil , xerrors .WithStackTrace (errTxAlreadyCommitted )
124+ case txStateFailed :
125+ return nil , xerrors .WithStackTrace (errTxFailedEarly )
118126 case txStateRollbacked :
119127 return nil , xerrors .WithStackTrace (errTxRollbackedEarly )
120128 default :
121129 _ , r , err = stmt .Execute (ctx , tx .control , parameters , opts ... )
122130 if err != nil {
131+ tx .state .Store (txStateFailed )
132+
123133 return nil , xerrors .WithStackTrace (err )
124134 }
125135
@@ -148,6 +158,8 @@ func (tx *transaction) CommitTx(
148158 switch tx .state .Load () {
149159 case txStateCommitted :
150160 return nil , xerrors .WithStackTrace (errTxAlreadyCommitted )
161+ case txStateFailed :
162+ return nil , xerrors .WithStackTrace (errTxFailedEarly )
151163 case txStateRollbacked :
152164 return nil , xerrors .WithStackTrace (errTxRollbackedEarly )
153165 default :
@@ -174,6 +186,8 @@ func (tx *transaction) CommitTx(
174186
175187 response , err = tx .s .tableService .CommitTransaction (ctx , request )
176188 if err != nil {
189+ tx .state .Store (txStateFailed )
190+
177191 return nil , xerrors .WithStackTrace (err )
178192 }
179193
@@ -206,6 +220,8 @@ func (tx *transaction) Rollback(ctx context.Context) (err error) {
206220 switch tx .state .Load () {
207221 case txStateCommitted :
208222 return nil // nop for committed tx
223+ case txStateFailed :
224+ return xerrors .WithStackTrace (errTxFailedEarly )
209225 case txStateRollbacked :
210226 return xerrors .WithStackTrace (errTxRollbackedEarly )
211227 default :
@@ -222,6 +238,8 @@ func (tx *transaction) Rollback(ctx context.Context) (err error) {
222238 },
223239 )
224240 if err != nil {
241+ tx .state .Store (txStateFailed )
242+
225243 return xerrors .WithStackTrace (err )
226244 }
227245
0 commit comments