google应用引擎- GAE与实例同步数据



如果我有这样一个类:

public class Example
{
    ArrayList<String> vector;
    public Example singleton = new Example();
    private Example()
    {
        //Read data from BD and fill the vector. Example vector:  ["foo","voo","faa","vuu","vee"]
    }   

    public synchronized removeElement()
    {
        vector.remove(0);
    }
    public synchronized changeElement()
    {
        vector.set(0,"fii");
    }
}

如果有多个实例正在运行,其中一个执行方法removeElement,另一个实例的值会发生什么?如果其中一个执行方法changeElement ?

GAE可以有多个JVM实例并行运行,因此内存中的更新值只在该实例上可见。

您应该使用共享数据存储:memcache(自由、快速,但易失)或datastore(昂贵、较慢,但一致)。

应用引擎实例运行在单独的jvm上,所以它们在内存中都有单独的'vector'实例。要跨这些节点同步状态,需要使用共享状态服务,如datastore或memcache。

最新更新