Reviewing code that adds a credit to an account balance in Datastore: which improvement should you suggest?
Choose an answer
Tap an option to check your answer.
Correct answer: Get and put the entity inside a Datastore transaction..
Why this is the answer
The correct answer is to get and put the entity inside a Datastore transaction. This ensures atomicity, meaning the read (get) and write (put) operations either both succeed or both fail. For financial transactions like adding credit, this prevents race conditions where multiple concurrent updates could lead to an incorrect final balance. Without a transaction, one update might overwrite another, resulting in lost data. Retrieving with an ancestor query is for structuring data hierarchically and doesn't inherently provide transactional guarantees for updates. Using a strongly consistent transactional database is a broader architectural choice, not a specific code improvement within Datastore. Not returning the account entity is a function design choice unrelated to data consistency issues during updates.
Pass your exam — without the endless answer hunt
Get every verified question and explanation for this exam in one place, and save hours of prep. 1,000+ certifications · 20+ languages · free to start.
Pass your exam faster → No card needed