From 3af2c6dc8279d2fc7026711a0bd92c50e0f04e6d Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Thu, 24 Oct 2024 13:08:20 -0700 Subject: [PATCH] fix(sample): change update entity sample to use transaction --- .../com/google/datastore/snippets/ConceptsTest.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/samples/snippets/src/test/java/com/google/datastore/snippets/ConceptsTest.java b/samples/snippets/src/test/java/com/google/datastore/snippets/ConceptsTest.java index 1397728ba..045b16a9a 100644 --- a/samples/snippets/src/test/java/com/google/datastore/snippets/ConceptsTest.java +++ b/samples/snippets/src/test/java/com/google/datastore/snippets/ConceptsTest.java @@ -297,8 +297,17 @@ public void testLookup() { public void testUpdate() { datastore.put(testEntity); // [START datastore_update] - Entity task = Entity.newBuilder(datastore.get(taskKey)).set("priority", 5).build(); - datastore.update(task); + Entity task; + Transaction txn = datastore.newTransaction(); + try { + task = Entity.newBuilder(txn.get(taskKey)).set("priority", 5).build(); + txn.put(task); + txn.commit(); + } finally { + if (txn.isActive()) { + txn.rollback(); + } + } // [END datastore_update] assertEquals(task, datastore.get(taskKey)); }