1

我尝试使用:

Google App Engine
Data Store 
JPA 
Spring Framework

我做了一个页面来列出客户。在该列表下方,我制作了一个表格来插入新客户。

Customer List 
<table>
    <c:forEach items="${listCustomer}" var="customer" varStatus="status">
        <tr>
            <td>${customer.id}</td>
            <td>${customer.name}</td>
            <td>${customer.desc}</td>

        </tr>
    </c:forEach>
</table>
New Customer    
<form:form action="add" commandName="customer"
    methodParam="POST">
    <form:input path="name"/>
    <form:input path="desc"/>
    <input type="submit" value="Add" />
</form:form>

控制器 :

@RequestMapping(value="/list", method = RequestMethod.GET)
public String list( ModelMap model) {
    List<Customer> list=CustomerDAO.INSTANCE.listCustomers();
    model.addAttribute("listCustomer",list);
    return "customer";

}

@RequestMapping(value="/add", method = RequestMethod.POST)
public String add( ModelMap model, Customer form) {
    CustomerDAO.INSTANCE.add(form.getName(),form.getDesc());
    List<Customer> list=CustomerDAO.INSTANCE.listCustomers();
    model.addAttribute("listCustomer",list);
    model.addAttribute("customer",new Customer());
    return "customer";
}

客户DAO

public enum CustomerDAO {
INSTANCE;

  public List<Customer> listCustomers() {
    EntityManager em = EMFService.get().createEntityManager();
    // Read the existing entries
    Query q = em.createQuery("select c from Customer c");
    List<Customer> customers = q.getResultList();
    System.out.println("size : "+customers.size());
    return customers;
  }

  public void add( String name, String desc) {
    synchronized (this) {
      System.out.println("CUSTOMERDAO::add");   
      EntityManager em = EMFService.get().createEntityManager();
      Customer customer = new Customer();
      //customer.setId(new Key());
      customer.setName(name);
      customer.setDesc(desc);
      em.persist(customer);
      System.out.println("name "+ name +" desc "+desc+" created");
      em.close();
    }
  }
//another function
}

持久性.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

<persistence-unit name="transactions-daniel">
    <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>

它实际上工作正常,但问题有时(间歇性)当我插入新客户时,它不会立即更新客户列表,我需要刷新页面以确保我的新记录是否成功插入。我检查了我的数据存储管理页面并在那里获得了我的新记录。

--更新我每次调用 listCustomers 时都会尝试打印大小,这证明它没有立即插入:

CUSTOMERDAO::add
name 4 desc 44 created
size : 4
CUSTOMERDAO::add
name 5 desc 5 created
size : 4
CUSTOMERDAO::add
name 6 desc 6 created
size : 6

照顾名称 5 desc 5 创建后,列表大小仍为 4(应为 5 行)。当我插入另一行时,它再次正常工作(6行)。

4

1 回答 1

1

这是因为高复制数据存储的最终一致性。请阅读这篇文章: https ://developers.google.com/appengine/docs/python/datastore/structuring_for_strong_consistency

解决方法是在 Customer 对象上有父键并使用该祖先键进行查询,或者传递新添加对象的信息并显示它。

于 2013-03-22T08:24:09.397 回答