Skip to content

Commit c44f17a

Browse files
authored
fix(sample): change update entity sample to use transaction (#1633)
1 parent d3bce79 commit c44f17a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

‎samples/snippets/src/test/java/com/google/datastore/snippets/ConceptsTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,17 @@ public void testLookup() {
297297
public void testUpdate() {
298298
datastore.put(testEntity);
299299
// [START datastore_update]
300-
Entity task = Entity.newBuilder(datastore.get(taskKey)).set("priority", 5).build();
301-
datastore.update(task);
300+
Entity task;
301+
Transaction txn = datastore.newTransaction();
302+
try {
303+
task = Entity.newBuilder(txn.get(taskKey)).set("priority", 5).build();
304+
txn.put(task);
305+
txn.commit();
306+
} finally {
307+
if (txn.isActive()) {
308+
txn.rollback();
309+
}
310+
}
302311
// [END datastore_update]
303312
assertEquals(task, datastore.get(taskKey));
304313
}

0 commit comments

Comments
 (0)