@Override public Void run(TransactionContext transaction) throws Exception { String column = "FirstName"; Struct row = transaction.readRow("Singers", Key.of(singerId), Collections.singleton(column)); String name = row.getString(column); transaction.buffer( Mutation.newUpdateBuilder("Singers").set(column).to(name.toUpperCase()).build()); return null; } });
/** Example of using {@link TransactionManager}. */ // [TARGET transactionManager()] // [VARIABLE my_singer_id] public void transactionManager(final long singerId) throws InterruptedException { // [START transactionManager] try (TransactionManager manager = dbClient.transactionManager()) { TransactionContext txn = manager.begin(); while (true) { String column = "FirstName"; Struct row = txn.readRow("Singers", Key.of(singerId), Collections.singleton(column)); String name = row.getString(column); txn.buffer(Mutation.newUpdateBuilder("Singers").set(column).to(name.toUpperCase()).build()); try { manager.commit(); break; } catch (AbortedException e) { Thread.sleep(e.getRetryDelayInMillis() / 1000); txn = manager.resetForRetry(); } } } // [END transactionManager] } }
@Override public Void run(TransactionContext transaction) throws UserException { transaction.buffer(Mutation.newInsertOrUpdateBuilder("T").set("K").to(key).build()); throw new UserException("User failure"); } };
@Override public Void run(TransactionContext transaction) { transaction.buffer(Mutation.newInsertOrUpdateBuilder("T").set("K").to(key).build()); throw newSpannerException(ErrorCode.OUT_OF_RANGE, "User failure"); } };
@Override public Long run(TransactionContext transaction) throws SpannerException { Struct row = strategy.read(transaction, key); long newValue = row.getLong(0) + 1; transaction.buffer( Mutation.newUpdateBuilder("T").set("K").to(key).set("V").to(newValue).build()); commitBarrier.countDown(); // Synchronize so that all threads attempt to commit at the same time. Uninterruptibles.awaitUninterruptibly(commitBarrier); return newValue; } };
@Override public Void run(TransactionContext transaction) { // DML long rowCount = transaction.executeUpdate( Statement.of("INSERT INTO T (k, v) VALUES ('" + key1 + "', 1)")); assertThat(rowCount).isEqualTo(1); // Mutations transaction.buffer( Mutation.newInsertOrUpdateBuilder("T").set("K").to(key2).set("V").to(2).build()); return null; } };
@Test public void rollback() { TransactionManager manager = client.transactionManager(); TransactionContext txn = manager.begin(); txn.buffer( Mutation.newInsertBuilder("T").set("K").to("Key2").set("BoolValue").to(true).build()); manager.rollback(); assertThat(manager.getState()).isEqualTo(TransactionState.ROLLED_BACK); // Row should not have been inserted. assertThat(client.singleUse().readRow("T", Key.of("Key2"), Arrays.asList("K", "BoolValue"))) .isNull(); }
@Test public void simpleInsert() { TransactionManager manager = client.transactionManager(); TransactionContext txn = manager.begin(); assertThat(manager.getState()).isEqualTo(TransactionState.STARTED); txn.buffer( Mutation.newInsertBuilder("T").set("K").to("Key1").set("BoolValue").to(true).build()); manager.commit(); assertThat(manager.getState()).isEqualTo(TransactionState.COMMITTED); Struct row = client.singleUse().readRow("T", Key.of("Key1"), Arrays.asList("K", "BoolValue")); assertThat(row.getString(0)).isEqualTo("Key1"); assertThat(row.getBoolean(1)).isTrue(); }
txn2.readRow("T", Key.of("Key3"), Arrays.asList("K", "BoolValue")); txn1.buffer( Mutation.newUpdateBuilder("T").set("K").to("Key3").set("BoolValue").to(false).build()); manager1.commit(); txn2 = manager2.resetForRetry(); txn2.buffer( Mutation.newUpdateBuilder("T").set("K").to("Key3").set("BoolValue").to(true).build()); manager2.commit();
@Test public void invalidInsert() { TransactionManager manager = client.transactionManager(); TransactionContext txn = manager.begin(); txn.buffer( Mutation.newInsertBuilder("InvalidTable") .set("K") .to("Key1") .set("BoolValue") .to(true) .build()); try { manager.commit(); fail("Expected exception"); } catch (SpannerException e) { // expected } assertThat(manager.getState()).isEqualTo(TransactionState.COMMIT_FAILED); // We cannot retry for non aborted errors. expectedException.expect(IllegalStateException.class); manager.resetForRetry(); }
@Override protected void applyMutations(Collection<Mutation> mutations) { this.transactionContext.buffer(mutations); }
@Override protected void applyMutations(Collection<Mutation> mutations) { this.transactionContext.buffer(mutations); }
protected void applyMutations(Collection<Mutation> mutations) { LOGGER.debug("Applying Mutation: " + mutations); doWithOrWithoutTransactionContext((x) -> { x.buffer(mutations); return null; }, () -> { this.databaseClient.write(mutations); return null; }); }
protected void applyMutations(Collection<Mutation> mutations) { LOGGER.debug("Applying Mutation: " + mutations); doWithOrWithoutTransactionContext((x) -> { x.buffer(mutations); return null; }, () -> { this.databaseClient.write(mutations); return null; }); }