Skip to content

Commit 17b411f

Browse files
committed
fix(sample): change update entity sample to use transaction (#1633)
1 parent 4461f5b commit 17b411f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

com/google/datastore/snippets/ConceptsTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,17 @@ public void testLookup() {
278278
public void testUpdate() {
279279
datastore.put(testEntity);
280280
// [START datastore_update]
281-
Entity task = Entity.newBuilder(datastore.get(taskKey)).set("priority", 5).build();
282-
datastore.update(task);
281+
Entity task;
282+
Transaction txn = datastore.newTransaction();
283+
try {
284+
task = Entity.newBuilder(txn.get(taskKey)).set("priority", 5).build();
285+
txn.put(task);
286+
txn.commit();
287+
} finally {
288+
if (txn.isActive()) {
289+
txn.rollback();
290+
}
291+
}
283292
// [END datastore_update]
284293
assertEquals(task, datastore.get(taskKey));
285294
}

0 commit comments

Comments
 (0)