我们如何才能获得JPA EntityManager Flush工作

Nav*_*Nav 17 java jpa transactions entitymanager

我的问题是为什么冲洗不起作用:

public void ejbService(){
   Customer c = em.find(Customer.class,1);
   c.setName("newName");
   em.flush();
   //at this point when I query mysql table I can not see "newName"


   thread.sleep(10000);

   c.setName("anotherName");
}
Run Code Online (Sandbox Code Playgroud)

完成方法后,我在db中看到"anotherName",我也用em.find(Customer.class,1,Lock.None)检查它; 但仍然不行

RGDS

zie*_*mer 23

你正在刷新,但你没有提交 - 或以其他方式结束可能配置为自动提交的事务/会话.

是的,在调用之后flush(),DBMS现在知道您的数据 - 但是遵循ACID标准,在DBMS被告知提交之前,没有其他数据库会话将看到此数据.

如果不了解其他应用程序背后的架构的其他详细信息,您可能希望执行以下操作:

em.getTransaction().commit();
Run Code Online (Sandbox Code Playgroud)

  • 大多数JPA实现将缓存JVM内的操作(​​在EntityManager中).`flush()`只是强制将这些操作发送到数据库等 - 但并不意味着提交.http://stackoverflow.com/questions/4275111/correct-use-of-flush-in-jpa-hibernate有一些可能对您有所帮助的其他详细信息/讨论. (7认同)
  • 将刷新模式设置为commit正在考虑这种情况.这告诉EntityManager仅在提交时刷新.它并不意味着提交flush. (4认同)
  • 这是一个微妙的细节,但提交的刷新模式并不意味着仅在提交时刷新.这意味着总是在提交时刷新; 但是,在其他时候也可能发生刷新,具体取决于JPA提供商. (4认同)