您好我有谷歌应用程序引擎应用程序。我使用JPA和简单的servlet和jsp。
我的代码:
Training training = new Training();
training.setCoachName(req.getParameter("coachName"));
training.setDate(req.getParameter("date"));
training.setTime(req.getParameter("time"));
EntityManager entityManager = EMFService.get().createEntityManager();
try {
entityManager.getTransaction().begin();
entityManager.persist(training);
entityManager.flush();
entityManager.getTransaction().commit();
} finally {
entityManager.close();
}
resp.sendRedirect("/overview");
和用于查看培训列表的servlet
EntityManager entityManager = EMFService.get().createEntityManager();
Query q = entityManager.createQuery("select t from " + Training.class.getName() + " t ");
List<Training> result = q.getResultList();
req.setAttribute("trainigs", result);
entityManager.close();
RequestDispatcher view = req.getRequestDispatcher("index.jsp");
view.forward(req, resp);
问题是,我存储到db记录,但是没有显示index.jsp。我有时尝试刷新页面,然后记录就在那里。所以在存储记录和显示在jsp之间是延迟几秒钟。
问题在哪里?
答案 0 :(得分:1)
您遇到的是名为eventual consistency的数据存储区属性。
基本上发生的是:当您向HRD写入数据时,它会将数据写入表,然后在构建索引之前返回(=索引是异步构建的)。由于查询需要索引,因此在构建索引之前,它们不会找到新写入的数据。
解决方案(在上面的链接中了解更多信息):