嗨,我有谷歌应用引擎应用程序。我使用 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上显示之间有几秒钟的延迟。
问题出在哪里?