I have an application deployed in Google app engine. I am getting inconsistent data when i fetch an entity by id immediately after updating that entity. I'm using JDO 3.0 to access the app engine datastore.
I have an entity Employee
@PersistenceCapable(detachable = "true")
public class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = -8319851654750418424L;
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY, defaultFetchGroup = "true")
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String id;
@Persistent(defaultFetchGroup = "true")
private String name;
@Persistent(defaultFetchGroup = "true")
private String designation;
@Persistent(defaultFetchGroup = "true")
private Date dateOfJoin;
@Persistent(defaultFetchGroup = "true")
private String email;
@Persistent(defaultFetchGroup = "true")
private Integer age;
@Persistent(defaultFetchGroup = "true")
private Double salary;
@Persistent(defaultFetchGroup = "true")
private HashMap<String, String> experience;
@Persistent(defaultFetchGroup = "true")
private List<Address> address;
/**
* Setters and getters, toString() * */
}
Initially, when I create an employee I do not set the fields salary and email.
I update the Employee entity to add salary and email later. The update works fine and the data gets persisted into the appengine datastore. However, when i immediately try to fetch the same employee entity by id, I sometimes get the stale data, where salary and email are null. The code I use to create and to fetch the employee entity is given below.
public Employee create(Employee object) {
Employee persObj = null;
PersistenceManager pm = PMF.get().getPersistenceManager();
Transaction tx = null;
try {
tx = pm.currentTransaction();
tx.begin();
persObj = pm.makePersistent(object);
tx.commit();
} finally {
if ((tx != null) && tx.isActive()) {
tx.rollback();
}
pm.close();
}
return persObj;
}
public Employee findById(Serializable id) {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
Employee e = pm.getObjectById(Employee.class, id);
System.out.println("INSIDE EMPLOYEE DAO : " + e.toString());
return e;
} finally {
pm.close();
}
}
public void update(Employee object) {
PersistenceManager pm = PMF.get().getPersistenceManager();
Transaction tx = null;
try {
tx = pm.currentTransaction();
tx.begin();
Employee e = pm.getObjectById(object.getClass(), object.getId());
e.setName(object.getName());
e.setDesignation(object.getDesignation());
e.setDateOfJoin(object.getDateOfJoin());
e.setEmail(object.getEmail());
e.setAge(object.getAge());
e.setSalary(object.getSalary());
tx.commit();
} finally {
if (tx != null && tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
I have set the number of idle instances to 5 and there are around 8 instances running at a time. When I checked the logs of various instances this is what I found.
Why do i get stale data when the request is served by certain instances. I can assure that, if the fetch request is handled by the instance which initially handled the update request I always get the updated data. But when other instances handle the fetch request stale data may be returned. I have explicitly set the datastore read consistency to strong in my jdoconfig.xml.
<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig_3_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig http://java.sun.com/xml/ns/jdo/jdoconfig_3_0.xsd">
<persistence-manager-factory name="transactions-optional">
<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"/>
<property name="datanucleus.appengine.datastoreEnableXGTransactions" value="true"/>
<property name="datanucleus.query.jdoql.allowAll" value="true"/>
<property name="datanucleus.appengine.datastoreReadConsistency" value="STRONG" />
</persistence-manager-factory>
</jdoconfig>