|
41 | 41 | import com.google.cloud.spanner.ReadContext; |
42 | 42 | import com.google.cloud.spanner.ReadOnlyTransaction; |
43 | 43 | import com.google.cloud.spanner.ResultSet; |
| 44 | +import com.google.cloud.spanner.Spanner; |
44 | 45 | import com.google.cloud.spanner.SpannerException; |
| 46 | +import com.google.cloud.spanner.SpannerOptions; |
| 47 | +import com.google.cloud.spanner.SpannerOptions.Builder.DefaultReadWriteTransactionOptions; |
45 | 48 | import com.google.cloud.spanner.Statement; |
46 | 49 | import com.google.cloud.spanner.Struct; |
47 | 50 | import com.google.cloud.spanner.TimestampBound; |
|
52 | 55 | import com.google.common.collect.Sets; |
53 | 56 | import com.google.common.util.concurrent.SettableFuture; |
54 | 57 | import com.google.common.util.concurrent.Uninterruptibles; |
| 58 | +import com.google.spanner.v1.TransactionOptions.IsolationLevel; |
| 59 | +import com.google.spanner.v1.TransactionOptions.ReadWrite.ReadLockMode; |
55 | 60 | import java.util.ArrayList; |
56 | 61 | import java.util.Arrays; |
57 | 62 | import java.util.Collections; |
@@ -213,6 +218,54 @@ public void basicsUsingQuery() throws InterruptedException { |
213 | 218 | }); |
214 | 219 | } |
215 | 220 |
|
| 221 | + @Test |
| 222 | + public void isolationLevelAndReadLockModeSetAtClientLevelTest() { |
| 223 | + SpannerOptions options = |
| 224 | + env.getTestHelper().getOptions().toBuilder() |
| 225 | + .setDefaultTransactionOptions( |
| 226 | + DefaultReadWriteTransactionOptions.newBuilder() |
| 227 | + .setIsolationLevel(IsolationLevel.REPEATABLE_READ) |
| 228 | + .setReadLockMode(ReadLockMode.OPTIMISTIC) |
| 229 | + .build()) |
| 230 | + .build(); |
| 231 | + try (Spanner spanner = options.getService()) { |
| 232 | + DatabaseClient client = spanner.getDatabaseClient(db.getId()); |
| 233 | + Long updatedRows = |
| 234 | + client |
| 235 | + .readWriteTransaction() |
| 236 | + .run( |
| 237 | + transaction -> |
| 238 | + transaction.executeUpdate( |
| 239 | + Statement.of("INSERT INTO T (K, V) VALUES ('test1', 2)"))); |
| 240 | + assertThat(updatedRows).isEqualTo(1L); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + public void isolationLevelAndReadLockModeSetAtClientAndTxnLevelTest() { |
| 246 | + SpannerOptions options = |
| 247 | + env.getTestHelper().getOptions().toBuilder() |
| 248 | + .setDefaultTransactionOptions( |
| 249 | + DefaultReadWriteTransactionOptions.newBuilder() |
| 250 | + .setIsolationLevel(IsolationLevel.REPEATABLE_READ) |
| 251 | + .setReadLockMode(ReadLockMode.OPTIMISTIC) |
| 252 | + .build()) |
| 253 | + .build(); |
| 254 | + try (Spanner spanner = options.getService()) { |
| 255 | + DatabaseClient client = spanner.getDatabaseClient(db.getId()); |
| 256 | + Long updatedRows = |
| 257 | + client |
| 258 | + .readWriteTransaction( |
| 259 | + Options.isolationLevel(IsolationLevel.SERIALIZABLE), |
| 260 | + Options.readLockMode(ReadLockMode.PESSIMISTIC)) |
| 261 | + .run( |
| 262 | + transaction -> |
| 263 | + transaction.executeUpdate( |
| 264 | + Statement.of("INSERT INTO T (K, V) VALUES ('test1', 2)"))); |
| 265 | + assertThat(updatedRows).isEqualTo(1L); |
| 266 | + } |
| 267 | + } |
| 268 | + |
216 | 269 | @Test |
217 | 270 | public void userExceptionPreventsCommit() { |
218 | 271 | class UserException extends Exception { |
|
0 commit comments