Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

google app engine - JDO transactions + many GAE instances = overriding data

I have very simple PersistenceCapable class:

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable
public class TheEntity {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
public Long getId() {
    return id;
}
@Persistent
private int version;
public int getVersion() {
    return version;
}
public void setVersion(int version)
{
    this.version=version;
}
}  

And I simply update version by incrementing it (and I added some computations to make GAE warmup new instances sometimes)

public void test2(){
    PersistenceManager mgr = getPersistenceManager();
    mgr.currentTransaction().begin();
    TheEntity test = mgr.getObjectById(TheEntity.class, 5081359164899328L);
    test.setVersion(test.getVersion()+1);
    log.log(Level.SEVERE, "VERSION: " + test.getVersion());
        for (int i = 0; i < 10000; i++)
        {
            long x = i*i;
            x++;
        }
    mgr.currentTransaction().commit();
    mgr.close();}

So, I called the code 9 times, it never failed. The logs below:

W1. I 02:28:21.331 2015-01-10  200       0 B  6839ms /_ah/warmup
1. I 02:28:38.407 2015-01-10  204       0 B  4343ms /_ah/spi/com.vaktu.MyEndpoint.test1
   188.122.13.128 - - [09/Jan/2015:17:28:38 -0800] "POST /_ah/spi/com.vaktu.MyEndpoint.test1 HTTP/1.1" 204 0 - "Google-HTTP-Java-Client/1.18.0-rc (gzip)" "mmorpgmonsters.appspot.com" ms=4343 cpu_ms=2940 cpm_usd=0.000022 instance=00c61b117c4063829b2bf7ef35e1e6e8a0e41978 app_engine_release=1.9.17
   I 02:28:38.012 com.vaktu.MyEndpoint test2: VERSION: 1
2. I 02:28:42.493 2015-01-10  204       0 B   115ms /_ah/spi/com.vaktu.MyEndpoint.test1
   188.122.13.128 - - [09/Jan/2015:17:28:42 -0800] "POST /_ah/spi/com.vaktu.MyEndpoint.test1 HTTP/1.1" 204 0 - "Google-HTTP-Java-Client/1.18.0-rc (gzip)" "mmorpgmonsters.appspot.com" ms=115 cpu_ms=70 instance=00c61b117c4063829b2bf7ef35e1e6e8a0e41978 app_engine_release=1.9.17
   I 02:28:42.444 com.vaktu.MyEndpoint test2: VERSION: 2
W2. I 02:28:42.495 2015-01-10  200       0 B  7061ms /_ah/warmup
3. I 02:28:54.196 2015-01-10  204       0 B  4647ms /_ah/spi/com.vaktu.MyEndpoint.test1
   188.122.13.128 - - [09/Jan/2015:17:28:54 -0800] "POST /_ah/spi/com.vaktu.MyEndpoint.test1 HTTP/1.1" 204 0 - "Google-HTTP-Java-Client/1.18.0-rc (gzip)" "mmorpgmonsters.appspot.com" ms=4647 cpu_ms=3173 cpm_usd=0.000022 instance=00c61b117c31a94949f82af423e0f1a3c1ac35 app_engine_release=1.9.17
   I 02:28:53.814 com.vaktu.MyEndpoint test2: VERSION: 3
4. I 02:28:59.126 2015-01-10  204       0 B   163ms /_ah/spi/com.vaktu.MyEndpoint.test1
   188.122.13.128 - - [09/Jan/2015:17:28:59 -0800] "POST /_ah/spi/com.vaktu.MyEndpoint.test1 HTTP/1.1" 204 0 - "Google-HTTP-Java-Client/1.18.0-rc (gzip)" "mmorpgmonsters.appspot.com" ms=163 cpu_ms=70 instance=00c61b117c31a94949f82af423e0f1a3c1ac35 app_engine_release=1.9.17
   I 02:28:59.036 com.vaktu.MyEndpoint test2: VERSION: 4
5. I 02:29:02.426 2015-01-10  204       0 B   119ms /_ah/spi/com.vaktu.MyEndpoint.test1
  188.122.13.128 - - [09/Jan/2015:17:29:02 -0800] "POST /_ah/spi/com.vaktu.MyEndpoint.test1 HTTP/1.1" 204 0 - "Google-HTTP-Java-Client/1.18.0-rc (gzip)" "mmorpgmonsters.appspot.com" ms=119 cpu_ms=117 instance=00c61b117c31a94949f82af423e0f1a3c1ac35 app_engine_release=1.9.17
   I 02:29:02.373 com.vaktu.MyEndpoint test2: VERSION: 5
6. I 02:29:06.947 2015-01-10  204       0 B   114ms /_ah/spi/com.vaktu.MyEndpoint.test1
  188.122.13.128 - - [09/Jan/2015:17:29:06 -0800] "POST /_ah/spi/com.vaktu.MyEndpoint.test1 HTTP/1.1" 204 0 - "Google-HTTP-Java-Client/1.18.0-rc (gzip)" "mmorpgmonsters.appspot.com" ms=114 cpu_ms=70 instance=00c61b117c31a94949f82af423e0f1a3c1ac35 app_engine_release=1.9.17
   I 02:29:06.896 com.vaktu.MyEndpoint test2: VERSION: 6
7. I 02:29:11.705 2015-01-10  204       0 B   101ms /_ah/spi/com.vaktu.MyEndpoint.test1
   188.122.13.128 - - [09/Jan/2015:17:29:11 -0800] "POST /_ah/spi/com.vaktu.MyEndpoint.test1 HTTP/1.1" 204 0 - "Google-HTTP-Java-Client/1.18.0-rc (gzip)" "mmorpgmonsters.appspot.com" ms=101 cpu_ms=47 instance=00c61b117c31a94949f82af423e0f1a3c1ac35 app_engine_release=1.9.17
   I 02:29:11.665 com.vaktu.MyEndpoint test2: VERSION: 7
8. I 02:29:30.133 2015-01-10  204       0 B   111ms /_ah/spi/com.vaktu.MyEndpoint.test1
  188.122.13.128 - - [09/Jan/2015:17:29:30 -0800] "POST /_ah/spi/com.vaktu.MyEndpoint.test1 HTTP/1.1" 204 0 - "Google-HTTP-Java-Client/1.18.0-rc (gzip)" "mmorpgmonsters.appspot.com" ms=111 cpu_ms=117 instance=00c61b117c4063829b2bf7ef35e1e6e8a0e41978 app_engine_release=1.9.17
   I 02:29:30.085 com.vaktu.MyEndpoint test2: VERSION: 3
9. I 02:29:33.598 2015-01-10  204       0 B   158ms /_ah/spi/com.vaktu.MyEndpoint.test1
  188.122.13.128 - - [09/Jan/2015:17:29:33 -0800] "POST /_ah/spi/com.vaktu.MyEndpoint.test1 HTTP/1.1" 204 0 - "Google-HTTP-Java-Client/1.18.0-rc (gzip)" "mmorpgmonsters.appspot.com" ms=158 cpu_ms=93 instance=00c61b117c4063829b2bf7ef35e1e6e8a0e41978 app_engine_release=1.9.17
   I 02:29:33.509 com.vaktu.MyEndpoint test2: VERSION: 4

test1 just calls test2, and the code of test2 is above. W1 = warmup of instance 1 W2 = warmup of instance 2 Requests run on instance 1: 1, 2, 8, 9 Requests run on instance 2: 3, 4, 5, 6, 7 Now, after 9 updates that should increase the version to 9, I have version = 4 in my database. Seriously, it is a disaster for me.. Any help very appreciated. If anyone implemented JPA and could test if it works better please give feedback.

My jdoconfig.xml:

   <property name="datanucleus.appengine.datastoreEnableXGTransactions" value="true"/>
   <property name="javax.jdo.PersistenceManagerFactoryClass" value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory"/>
   <property name="javax.jdo.option.ConnectionURL" value="appengine"/>
   <property name="javax.jdo.option.NontransactionalRead" value="true"/>
   <property name="javax.jdo.option.NontransactionalWrite" value="true"/>
   <property name="javax.jdo.option.RetainValues" value="true"/>
   <property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
   <property name="datanucleus.appengine.singletonPMFForName" value="true"/>

My persistence.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence         http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="transactions-optional">
    <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
    <properties>
        <property name="datanucleus.NontransactionalRead" value="true"/>
        <property name="datanucleus.NontransactionalWrite" value="true"/>
        <property name="datanucleus.ConnectionURL" value="appengine"/>
    </properties>
</persistence-unit>
</persistence>

JARS in WEB-INF/lib:

appengine-api-1.0-sdk-1.9.17.jar
appengine-api-labs.jar
appengine-api.jar
appengine-endpoints-deps.jar
appengine-endpoints.jar
appengine-jsr107cache-1.9.17.jar
asm-4.0.jar
datanucleus-api-jdo-3.1.3.jar
datanucleus-api-jpa-3.1.3.jar
datanucleus-appengine-2.1.2.jar
datanucleus-cache-3.1.3.jar //I want to use memcache after I finally make it work without it
datanucleus-core-3.1.3.jar
gcm-server.jar
geronimo-jpa_2.0_spec-1.0.jar
jdo-api-3.0.1.jar
json_simple-1.1.jar
jsr107cache-1.1.jar
jta-1.1.jar

I'll be very, very grateful for help...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...