This repository was archived by the owner on Mar 31, 2026. It is now read-only.
Commit 997bab0
committed
fix: add retries for failed first statements
Add retries if the first statement in a read/write transaction fails, as the
statement then does not return a transaction ID. In order to ensure that we
get a transaction ID, we first execute an explicit BeginTransaction RPC and
then retry the original statement. We return the response of the retry to
the application, regardless whether the retry fails or succeeds.
The reason that we do a retry with a BeginTransaction AND include the
first statement, is to guarantee transaction consistency. If we were to
leave the first statement out of the transaction, then it will not be
guaranteed that the error condition that cause the failure in the first
place is actually still true when the transaction commits. This would break
the transaction guarantees.
Example (pseudo-code):
```sql
-- The following statement fails with ALREADY_EXISTS
insert into some_table (id, value) values (1, 'One');
-- Execute an explicit BeginTransaction RPC.
begin;
-- Retry the initial statement. This ensures that
-- whatever the response is, this response will be
-- valid for the entire transaction.
insert into some_table (id, value) values (1, 'One');
-- This is guaranteed to return a row.
select * from some_table where id=1;
-- ... execute the rest of the transaction ...
commit;
```
If we had not included the initial insert statement in the retried transaction,
then there is no guarantee that the select statement would actually return any
rows, as other transactions could in theory have deleted it in the meantime.1 parent ac59847 commit 997bab0
File tree
7 files changed
+775
-90
lines changed- google/cloud
- spanner_dbapi
- spanner_v1
- testing
- tests/mockserver_tests
7 files changed
+775
-90
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| |||
116 | 121 | | |
117 | 122 | | |
118 | 123 | | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
119 | 129 | | |
120 | 130 | | |
121 | 131 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
366 | 366 | | |
367 | 367 | | |
368 | 368 | | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
369 | 379 | | |
370 | 380 | | |
371 | 381 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | | - | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
56 | 63 | | |
57 | 64 | | |
58 | 65 | | |
59 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
60 | 75 | | |
61 | 76 | | |
62 | 77 | | |
| |||
94 | 109 | | |
95 | 110 | | |
96 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
97 | 118 | | |
98 | 119 | | |
99 | 120 | | |
| |||
103 | 124 | | |
104 | 125 | | |
105 | 126 | | |
| 127 | + | |
106 | 128 | | |
107 | 129 | | |
108 | 130 | | |
| |||
115 | 137 | | |
116 | 138 | | |
117 | 139 | | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
118 | 149 | | |
119 | 150 | | |
120 | 151 | | |
| |||
176 | 207 | | |
177 | 208 | | |
178 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
179 | 218 | | |
180 | 219 | | |
181 | 220 | | |
| |||
184 | 223 | | |
185 | 224 | | |
186 | 225 | | |
187 | | - | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
188 | 229 | | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
189 | 236 | | |
190 | 237 | | |
191 | 238 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
217 | 223 | | |
218 | 224 | | |
219 | 225 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
68 | 82 | | |
69 | 83 | | |
70 | 84 | | |
| |||
174 | 188 | | |
175 | 189 | | |
176 | 190 | | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
177 | 194 | | |
178 | 195 | | |
179 | 196 | | |
| |||
0 commit comments