Michele Orsi
Michele Orsi

Reputation: 762

List access synchronization in GAE

I just synchronized my java code to avoid multiple access to the same List. Here it is my code:

List<Category> list = Collections.synchronizedList(listCat); 
..
pm.currentTransaction().begin();
..
synchronized (list) {
    for (Category category : list) {
        category.setKey(null);
        tempUser.addToCategories(category);
    }
}
..
pm.currentTransaction().commit();

.. but I continue to get

java.util.ConcurrentModificationException: too much contention on these datastore entities. please try again.
at com.google.appengine.api.datastore.DatastoreApiHelper.translateError(DatastoreApiHelper.java:39)
at com.google.appengine.api.datastore.DatastoreApiHelper$1.convertException(DatastoreApiHelper.java:76)
at com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:106)
at com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:90)
at com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:90)
at com.google.appengine.api.datastore.FutureHelper.getInternal(FutureHelper.java:72)
at com.google.appengine.api.datastore.FutureHelper.quietGet(FutureHelper.java:33)
at com.google.appengine.api.datastore.TransactionImpl.commit(TransactionImpl.java:116)
at org.datanucleus.store.appengine.jdo.DatastoreJDOTransaction.commit(DatastoreJDOTransaction.java:68)
at (the row where the transaction is committed)

.. when different instances access to that list. Is there a way to avoid that? I mean a different way from the one specified here: Uses for Transactions.

I use JDO 2.3, but I don't think it affects in anyway this behaviour

Upvotes: 0

Views: 208

Answers (1)

Peter Knego
Peter Knego

Reputation: 80340

GAE transactions don't do locking: they use optimistic concurrency control, meaning if multiple transactions change same entity group the first will succeed the others will fail and will need to be retried.

Upvotes: 1

Related Questions