google-app-engine


Will two entities get written to datastore with this endpoint?


I have an endpoint method that first uses a query to see if an entity with certain params exists, and if it does not it will create it. If it exists, I want to increase a counter in the variable:
Report report = ofy().load().type(Report.class)
.filter("userID", userID)
.filter("state", state).first().now();
if (report == null) {
//write new entity to datastore with userID and state
} else {
//increase counter in entity +1
report.setCount(report.count+1)
//save entity to datastore
}
My question is, what if someone clicks a button to execute the above endpoint with the same params very rapidly, what will happen? Will two identical Report entities get written to the datastore? I only want to make sure one is written.
By itself this code is not safe and has a race condition that will allow multiple Reports to be created.
To make this safe, you need to run the code in a transaction. Which means you must have an ancestor query (or convert it to a simple primary key lookup). One option is to give Report a #Parent of the User. Then you can so something like this:
ofy().transact(() -> {
Report report = ofy().load().type(Report.class)
.ancestor(user)
.filter("state", state).first().now();
if (report == null) {
//write new entity to datastore with userID and state
} else {
//increase counter in entity +1
report.setCount(report.count+1)
//save entity to datastore
}
});

Related Links

GAE doesn't import gflags
Real time notification system on Google App Engine(GAE)
How can I easily get a list of the indexed terms from a search index in the google appengine full text search api in java?
jpa #version on google appengine with #OneToMany: appengine bug or usage error?
How can I delete old backup via cron?
Error on itext-gae.jar
How to get the original from address in a forwarded message in google app engine?
Cannot authorize my google app to access my Cloud SQL instance
GooglePlaces autocomplete - more than one input
Import GAE data through Storage into BigQuery fails
Google AppEngine Tutorial, difference between code snippets
OpenID sign in not working in Gmail contextual gadget
Problems getting a basic JAXRS + JPA version working on GAE
Appengine's Indexing order, cursors, and aggregation
Works locally but throws java.lang.IncompatibleClassChangeError when deployed to GAE
Google App Engine Datastore: Turkish Characters won't sort properly

Categories

HOME
shell
xml
hyperlink
checkstyle
apache-flink
components
jsf-2.2
tail
settings
crystal-reports-2013
jtextpane
joomla3.0
spin
owin
minecraft-forge
drop-down-menu
endeca
text-mining
x-cart
endpoint
enterprise-architect
symbol
coin3d
modbus-tcp
glyphicons
template-engine
production
nodemcu
swagger-2.0
oledb
drive
articulate-storyline
epsilon
direct3d11
mount
google-cloud-print
failover
winpe
finagle
nsurl
eviews
kudu
dsx-desktop
protege4
raft
android-softkeyboard
preloader
importerror
strftime
mesosphere
dtsearch
insight
type-theory
monkey
template-toolkit
objectscript
javafx-css
reachability
vcloud-director-rest-api
chromium-os
seek
tinkerpop3
signalr.client
drawstring
installshield-2010
clipboard.js
shopping
cassia
bizagi
yajsw
nullable
invoke-sqlcmd
httpruntime.cache
highland.js
snackbar
mmwormhole
post-commit-hook
contrast
linkedin-jsapi
matrix-inverse
renderer
gae-quotas
accesscontrolexception
embedded-fonts
c++builder-xe5
observium
kaleidoscope
font-awesome-4.0.0
asplinkbutton
uimanageddocument
saleslogix
appjs
hibernateexception
node-redis
cdonts
facebook-iframe
lobo-cobra
technical-debt
information-management

Resources

Encrypt Message