Your teammate has asked you to review the code below, which is adding a credit to an account balance in Cloud Datastore. Which improvement should you suggest your teammate make?
A.
Get the entity with an ancestor query.
B.
Get and put the entity in a transaction.
C.
Use a strongly consistent transactional database.
D.
Don't return the account entity from the function.
B. Get and put the entity in a transaction.
The code provided is updating an account balance in Cloud Datastore. To ensure data integrity and consistency, such an update should be done within a transaction to avoid issues with concurrent updates which could result in an incorrect balance if the account is accessed by multiple processes at the same time.
B. Get and put the entity in a transaction.
It is a good practice to perform a get and put within a transaction to ensure that the update to the balance is atomic. This prevents other processes from reading the balance and making updates to it simultaneously, which could lead to incorrect or inconsistent results. By using a transaction, your teammate can ensure that the balance is updated correctly and consistently.
https://cloud.google.com/datastore/docs/concepts/transactions#uses_for_transactions:
"This requires a transaction because the value of balance in an entity may be updated by another user after this code fetches the object, but before it saves the modified object. Without a transaction, the user's request uses the value of balance prior to the other user's update, and the save overwrites the new value. With a transaction, the application is told about the other user's update."
B is the answer
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
santoshchauhan
7 months, 3 weeks ago__rajan__
1 year, 1 month agoomermahgoub
1 year, 9 months agotomato123
2 years, 2 months agoParagSanyashiv
2 years, 9 months agosyu31svc
3 years, 3 months ago[Removed]
3 years, 9 months agoStelSen
3 years, 8 months ago