我对Google App Engine JDO实施有疑问,我无法弄清楚。文档(http://code.google.com/intl/sv-SE/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata.html)声明“对makePersistent()的调用是同步的,并且在保存对象并更新索引之前不会返回。”但我的经历不同。
我想将(makePersistent)对象保存到数据存储区中。保存完成后,我希望能够立即从数据存储区中获取它(查询执行)。我知道我不必获取它(因为我已经有了内存中的对象)但重点是我希望下一个请求能够从数据存储中检索数据。如果第二个请求足够快,则无法使用当前实现。
我注意到的一件奇怪的事情是,如果我试图在循环中多次从数据存储区中获取对象(下面的代码),则返回的对象非常快(通常<10ms)。但是如果我跳过循环而是在makePersistent和查询执行之间运行Thread.sleep(..)5000毫秒,则不确定是否找到了该对象。这些解决方案都不是我想要的。我希望能够在没有睡眠或循环的情况下立即获取数据。
访问下面的DataStoreTestServlet的代码和结果如您所见,包括“等待”找到数据的循环。同样,我不想要循环。
有谁知道我错过了什么?我想它必须是某种东西。这个实现对我来说感觉不对:)
我正在使用appengine-java-sdk-1.6.0。这是本地(开发服务器)和部署在Google服务器上的问题。
这是访问servlet的结果。
Created users:
User [password=password, userName=user1321190966416] took 18ms, 2 loop(s)
User [password=password, userName=user1321190966438] took 15ms, 6 loop(s)
User [password=password, userName=user1321190966456] took 2ms, 1 loop(s)
User [password=password, userName=user1321190966460] took 10ms, 5 loop(s)
User [password=password, userName=user1321190966472] took 0ms, 1 loop(s)
User [password=password, userName=user1321190966472] took 0ms, 1 loop(s)
User [password=password, userName=user1321190966472] took 16ms, 1 loop(s)
User [password=password, userName=user1321190966488] took 0ms, 2 loop(s)
User [password=password, userName=user1321190966488] took 0ms, 1 loop(s)
User [password=password, userName=user1321190966488] took 16ms, 1 loop(s)
代码和配置。
jdoconfig.xml
<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">
<persistence-manager-factory name="transactions-optional">
<property name="javax.jdo.PersistenceManagerFactoryClass" value="org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory"/>
<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.datastoreReadConsistency" value="STRONG" />
</persistence-manager-factory>
</jdoconfig>
PMF.java
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
public final class PMF {
private static final PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional");
private PMF() {
}
public static PersistenceManagerFactory get() {
return pmfInstance;
}
}
User.java
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User {
@PrimaryKey
@Persistent
private String userName;
@Persistent
private String password;
public User(String userName, String password) {
super();
this.setUserName(userName);
this.setPassword(password);
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return "User [password=" + password + ", userName=" + userName + "]";
}
}
DataStoreTestServlet.java
import java.io.IOException;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class DataStoreTestServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
StringBuffer sb = new StringBuffer();
sb.append("Created users:\n");
for (int i = 0; i < 10; i++) {
String uniqueName = "user" + System.currentTimeMillis();
User user = new User(uniqueName, "password");
save(user);
User userFromDS = null;
long startTime = System.currentTimeMillis();
long loop = 0;
while (userFromDS == null) {
userFromDS = get(uniqueName);
loop++;
if (userFromDS != null) {
long endTime = System.currentTimeMillis();
sb.append(userFromDS.toString() + " took " + (endTime - startTime) + "ms, " + loop + " loop(s)\n");
}
}
}
resp.setContentType("text/plain");
resp.getWriter().println(sb.toString());
}
public Object save(Object obj) {
PersistenceManager pm = PMF.get().getPersistenceManager();
Object savedObject = null;
try {
savedObject = pm.makePersistent(obj);
} finally {
pm.close();
}
return savedObject;
}
public User get(String userName) {
User user = null;
List<User> users = null;
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery(User.class);
query.setFilter("userName == nameParam");
query.declareParameters("String nameParam");
try {
users = (List<User>) query.execute(userName);
if (users != null && users.size() > 0) {
user = users.get(0);
}
} finally {
query.closeAll();
pm.close();
}
return user;
}
}
答案 0 :(得分:3)
尝试在jdoconfig.xml中添加:
<property name="datanucleus.appengine.datastoreReadConsistency" value="STRONG" />
提高表现;应用引擎数据存储区“最终一致”。这意味着当您创建新对象或更改现有对象时,它不会立即显示;允许查找时间的性能提升。与此相反的是“STRONG”一致性,这意味着每个请求都是使用数据存储区中的最新数据进行的。
现在,根据app engine documentation for this STRONG一致是默认值,您必须明确设置最终一致性。但是,根据我的观察,你必须设置STRONG一致性,EVENTUAL是默认值(可能是错误?)。所以尝试将上面的内容添加到你的jdoconfig xml中,如果你观察到同样的事情,那么我可能会打开一个针对app引擎的错误,假设还没有为此问题打开过。
您必须记住的一件事是,如果您设置STRONG一致性,那么您将会受到性能影响。我只设置它,因为我的界面的某些部分被搞砸了,因为我会使用不那么新鲜的数据构建它的一部分,并且在同一请求期间,另一部分将使用新数据构建;使我的界面不一致。这可能是解决问题的广泛方法;但它有效:)。