@@ -34,11 +34,6 @@ class DatabaseClientImpl implements DatabaseClient {
34
34
private static final String PARTITION_DML_TRANSACTION = "CloudSpanner.PartitionDMLTransaction" ;
35
35
private static final Tracer tracer = Tracing .getTracer ();
36
36
37
- private enum SessionMode {
38
- READ ,
39
- READ_WRITE
40
- }
41
-
42
37
@ VisibleForTesting final String clientId ;
43
38
@ VisibleForTesting final SessionPool pool ;
44
39
@@ -53,21 +48,15 @@ private enum SessionMode {
53
48
}
54
49
55
50
@ VisibleForTesting
56
- PooledSessionFuture getReadSession () {
57
- return pool .getReadSession ();
58
- }
59
-
60
- @ VisibleForTesting
61
- PooledSessionFuture getReadWriteSession () {
62
- return pool .getReadWriteSession ();
51
+ PooledSessionFuture getSession () {
52
+ return pool .getSession ();
63
53
}
64
54
65
55
@ Override
66
56
public Timestamp write (final Iterable <Mutation > mutations ) throws SpannerException {
67
57
Span span = tracer .spanBuilder (READ_WRITE_TRANSACTION ).startSpan ();
68
58
try (Scope s = tracer .withSpan (span )) {
69
59
return runWithSessionRetry (
70
- SessionMode .READ_WRITE ,
71
60
new Function <Session , Timestamp >() {
72
61
@ Override
73
62
public Timestamp apply (Session session ) {
@@ -94,7 +83,6 @@ public Timestamp writeAtLeastOnce(final Iterable<Mutation> mutations) throws Spa
94
83
Span span = tracer .spanBuilder (READ_WRITE_TRANSACTION ).startSpan ();
95
84
try (Scope s = tracer .withSpan (span )) {
96
85
return runWithSessionRetry (
97
- SessionMode .READ_WRITE ,
98
86
new Function <Session , Timestamp >() {
99
87
@ Override
100
88
public Timestamp apply (Session session ) {
@@ -120,7 +108,7 @@ public CommitResponse writeAtLeastOnceWithOptions(
120
108
public ReadContext singleUse () {
121
109
Span span = tracer .spanBuilder (READ_ONLY_TRANSACTION ).startSpan ();
122
110
try (Scope s = tracer .withSpan (span )) {
123
- return getReadSession ().singleUse ();
111
+ return getSession ().singleUse ();
124
112
} catch (RuntimeException e ) {
125
113
TraceUtil .endSpanWithFailure (span , e );
126
114
throw e ;
@@ -131,7 +119,7 @@ public ReadContext singleUse() {
131
119
public ReadContext singleUse (TimestampBound bound ) {
132
120
Span span = tracer .spanBuilder (READ_ONLY_TRANSACTION ).startSpan ();
133
121
try (Scope s = tracer .withSpan (span )) {
134
- return getReadSession ().singleUse (bound );
122
+ return getSession ().singleUse (bound );
135
123
} catch (RuntimeException e ) {
136
124
TraceUtil .endSpanWithFailure (span , e );
137
125
throw e ;
@@ -142,7 +130,7 @@ public ReadContext singleUse(TimestampBound bound) {
142
130
public ReadOnlyTransaction singleUseReadOnlyTransaction () {
143
131
Span span = tracer .spanBuilder (READ_ONLY_TRANSACTION ).startSpan ();
144
132
try (Scope s = tracer .withSpan (span )) {
145
- return getReadSession ().singleUseReadOnlyTransaction ();
133
+ return getSession ().singleUseReadOnlyTransaction ();
146
134
} catch (RuntimeException e ) {
147
135
TraceUtil .endSpanWithFailure (span , e );
148
136
throw e ;
@@ -153,7 +141,7 @@ public ReadOnlyTransaction singleUseReadOnlyTransaction() {
153
141
public ReadOnlyTransaction singleUseReadOnlyTransaction (TimestampBound bound ) {
154
142
Span span = tracer .spanBuilder (READ_ONLY_TRANSACTION ).startSpan ();
155
143
try (Scope s = tracer .withSpan (span )) {
156
- return getReadSession ().singleUseReadOnlyTransaction (bound );
144
+ return getSession ().singleUseReadOnlyTransaction (bound );
157
145
} catch (RuntimeException e ) {
158
146
TraceUtil .endSpanWithFailure (span , e );
159
147
throw e ;
@@ -164,7 +152,7 @@ public ReadOnlyTransaction singleUseReadOnlyTransaction(TimestampBound bound) {
164
152
public ReadOnlyTransaction readOnlyTransaction () {
165
153
Span span = tracer .spanBuilder (READ_ONLY_TRANSACTION ).startSpan ();
166
154
try (Scope s = tracer .withSpan (span )) {
167
- return getReadSession ().readOnlyTransaction ();
155
+ return getSession ().readOnlyTransaction ();
168
156
} catch (RuntimeException e ) {
169
157
TraceUtil .endSpanWithFailure (span , e );
170
158
throw e ;
@@ -175,7 +163,7 @@ public ReadOnlyTransaction readOnlyTransaction() {
175
163
public ReadOnlyTransaction readOnlyTransaction (TimestampBound bound ) {
176
164
Span span = tracer .spanBuilder (READ_ONLY_TRANSACTION ).startSpan ();
177
165
try (Scope s = tracer .withSpan (span )) {
178
- return getReadSession ().readOnlyTransaction (bound );
166
+ return getSession ().readOnlyTransaction (bound );
179
167
} catch (RuntimeException e ) {
180
168
TraceUtil .endSpanWithFailure (span , e );
181
169
throw e ;
@@ -186,9 +174,9 @@ public ReadOnlyTransaction readOnlyTransaction(TimestampBound bound) {
186
174
public TransactionRunner readWriteTransaction () {
187
175
Span span = tracer .spanBuilder (READ_WRITE_TRANSACTION ).startSpan ();
188
176
try (Scope s = tracer .withSpan (span )) {
189
- return getReadWriteSession ().readWriteTransaction ();
177
+ return getSession ().readWriteTransaction ();
190
178
} catch (RuntimeException e ) {
191
- TraceUtil .setWithFailure (span , e );
179
+ TraceUtil .endSpanWithFailure (span , e );
192
180
throw e ;
193
181
} finally {
194
182
span .end (TraceUtil .END_SPAN_OPTIONS );
@@ -199,7 +187,7 @@ public TransactionRunner readWriteTransaction() {
199
187
public TransactionManager transactionManager () {
200
188
Span span = tracer .spanBuilder (READ_WRITE_TRANSACTION ).startSpan ();
201
189
try (Scope s = tracer .withSpan (span )) {
202
- return getReadWriteSession ().transactionManager ();
190
+ return getSession ().transactionManager ();
203
191
} catch (RuntimeException e ) {
204
192
TraceUtil .endSpanWithFailure (span , e );
205
193
throw e ;
@@ -210,7 +198,7 @@ public TransactionManager transactionManager() {
210
198
public AsyncRunner runAsync () {
211
199
Span span = tracer .spanBuilder (READ_WRITE_TRANSACTION ).startSpan ();
212
200
try (Scope s = tracer .withSpan (span )) {
213
- return getReadWriteSession ().runAsync ();
201
+ return getSession ().runAsync ();
214
202
} catch (RuntimeException e ) {
215
203
TraceUtil .endSpanWithFailure (span , e );
216
204
throw e ;
@@ -221,7 +209,7 @@ public AsyncRunner runAsync() {
221
209
public AsyncTransactionManager transactionManagerAsync () {
222
210
Span span = tracer .spanBuilder (READ_WRITE_TRANSACTION ).startSpan ();
223
211
try (Scope s = tracer .withSpan (span )) {
224
- return getReadWriteSession ().transactionManagerAsync ();
212
+ return getSession ().transactionManagerAsync ();
225
213
} catch (RuntimeException e ) {
226
214
TraceUtil .endSpanWithFailure (span , e );
227
215
throw e ;
@@ -232,10 +220,7 @@ public AsyncTransactionManager transactionManagerAsync() {
232
220
public long executePartitionedUpdate (final Statement stmt ) {
233
221
Span span = tracer .spanBuilder (PARTITION_DML_TRANSACTION ).startSpan ();
234
222
try (Scope s = tracer .withSpan (span )) {
235
- // A partitioned update transaction does not need a prepared write session, as the transaction
236
- // object will start a new transaction with specific options anyway.
237
223
return runWithSessionRetry (
238
- SessionMode .READ ,
239
224
new Function <Session , Long >() {
240
225
@ Override
241
226
public Long apply (Session session ) {
@@ -248,17 +233,13 @@ public Long apply(Session session) {
248
233
}
249
234
}
250
235
251
- private <T > T runWithSessionRetry (SessionMode mode , Function <Session , T > callable ) {
252
- PooledSessionFuture session =
253
- mode == SessionMode .READ_WRITE ? getReadWriteSession () : getReadSession ();
236
+ private <T > T runWithSessionRetry (Function <Session , T > callable ) {
237
+ PooledSessionFuture session = getSession ();
254
238
while (true ) {
255
239
try {
256
240
return callable .apply (session );
257
241
} catch (SessionNotFoundException e ) {
258
- session =
259
- mode == SessionMode .READ_WRITE
260
- ? pool .replaceReadWriteSession (e , session )
261
- : pool .replaceReadSession (e , session );
242
+ session = pool .replaceSession (e , session );
262
243
}
263
244
}
264
245
}
0 commit comments