質問

Has anyone tried to implement pessimistic locking on GAE? On my project there are some tasks that have to be mutually exclusive. I have done this by using:

javax.persistence.EntityManager.find(entityClass, primaryKey, LockModeType.PESSIMISTIC_READ);

which queries DB using SELECT FOR UPDATE and which works well ... as long as there is only one application instance that is processing the requests. If there were more instances my requests would be processed partially concurrently.

I have tested this by adding a sleep of 10 seconds inside of my mutually exclusive method. For one instance 6 requests were processed in about 60 seconds but for 3 instances sometimes 20 sometimes 30 but never 60 seconds.

Does it mean CloudSQL does not replicate Locks among SQL instances? Is there any other way to implement pessimistic locking on a table row ?

Br Marek

役に立ちましたか?

解決

By instances you seem to mean CloudSQL server instances, not AppEngine SQL client instances. CloudSQL is MySQL based and the scope of a MySQL lock is indeed limited to a single server instance. Replication copies SQL data but not sessions or locks. Therefore your measured results make sense.

It may not work for you, but you could keep the number of CloudSQL instances down at 1 but increase the number of AppEngine server instances, which will be CloudSQL clients. Hopefully that delivers some of the scalability without sacrificing consistency (pessimistic locking). Your predicament is an inevitable manifestation of the CAP theorem.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top