@@ -22,6 +22,7 @@ import (
22
22
var (
23
23
errTxAlreadyCommitted = xerrors .Wrap (fmt .Errorf ("transaction already committed" ))
24
24
errTxRollbackedEarly = xerrors .Wrap (fmt .Errorf ("transaction rollbacked early" ))
25
+ errTxFailedEarly = xerrors .Wrap (fmt .Errorf ("transaction failed early" ))
25
26
)
26
27
27
28
type txState struct {
@@ -42,6 +43,7 @@ const (
42
43
txStateInitialized txStateEnum = iota
43
44
txStateCommitted
44
45
txStateRollbacked
46
+ txStateFailed
45
47
)
46
48
47
49
type transaction struct {
@@ -73,11 +75,15 @@ func (tx *transaction) Execute(
73
75
switch tx .state .Load () {
74
76
case txStateCommitted :
75
77
return nil , xerrors .WithStackTrace (errTxAlreadyCommitted )
78
+ case txStateFailed :
79
+ return nil , xerrors .WithStackTrace (errTxFailedEarly )
76
80
case txStateRollbacked :
77
81
return nil , xerrors .WithStackTrace (errTxRollbackedEarly )
78
82
default :
79
83
_ , r , err = tx .s .Execute (ctx , tx .control , query , parameters , opts ... )
80
84
if err != nil {
85
+ tx .state .Store (txStateFailed )
86
+
81
87
return nil , xerrors .WithStackTrace (err )
82
88
}
83
89
@@ -115,11 +121,15 @@ func (tx *transaction) ExecuteStatement(
115
121
switch tx .state .Load () {
116
122
case txStateCommitted :
117
123
return nil , xerrors .WithStackTrace (errTxAlreadyCommitted )
124
+ case txStateFailed :
125
+ return nil , xerrors .WithStackTrace (errTxFailedEarly )
118
126
case txStateRollbacked :
119
127
return nil , xerrors .WithStackTrace (errTxRollbackedEarly )
120
128
default :
121
129
_ , r , err = stmt .Execute (ctx , tx .control , parameters , opts ... )
122
130
if err != nil {
131
+ tx .state .Store (txStateFailed )
132
+
123
133
return nil , xerrors .WithStackTrace (err )
124
134
}
125
135
@@ -148,6 +158,8 @@ func (tx *transaction) CommitTx(
148
158
switch tx .state .Load () {
149
159
case txStateCommitted :
150
160
return nil , xerrors .WithStackTrace (errTxAlreadyCommitted )
161
+ case txStateFailed :
162
+ return nil , xerrors .WithStackTrace (errTxFailedEarly )
151
163
case txStateRollbacked :
152
164
return nil , xerrors .WithStackTrace (errTxRollbackedEarly )
153
165
default :
@@ -174,6 +186,8 @@ func (tx *transaction) CommitTx(
174
186
175
187
response , err = tx .s .tableService .CommitTransaction (ctx , request )
176
188
if err != nil {
189
+ tx .state .Store (txStateFailed )
190
+
177
191
return nil , xerrors .WithStackTrace (err )
178
192
}
179
193
@@ -206,6 +220,8 @@ func (tx *transaction) Rollback(ctx context.Context) (err error) {
206
220
switch tx .state .Load () {
207
221
case txStateCommitted :
208
222
return nil // nop for committed tx
223
+ case txStateFailed :
224
+ return xerrors .WithStackTrace (errTxFailedEarly )
209
225
case txStateRollbacked :
210
226
return xerrors .WithStackTrace (errTxRollbackedEarly )
211
227
default :
@@ -222,6 +238,8 @@ func (tx *transaction) Rollback(ctx context.Context) (err error) {
222
238
},
223
239
)
224
240
if err != nil {
241
+ tx .state .Store (txStateFailed )
242
+
225
243
return xerrors .WithStackTrace (err )
226
244
}
227
245
0 commit comments