Entitymanager.flush()与EntityManager.getTransaction()。commit-我应该首选什么?

【字号: 日期:2024-02-24浏览:40作者:雯心
如何解决Entitymanager.flush()与EntityManager.getTransaction()。commit-我应该首选什么??

在第一个示例中,对数据的更改在遇到刷新后会反映在数据库中,但仍在事务中。

但是在第二个示例中,您将立即提交事务。因此,对数据库所做的更改以及事务也在那里结束。

有时,刷新可能有助于将数据保留在正在进行的事务之间,然后最终提交更改。因此,如果以后发生某些问题,例如批量插入/更新,您也可以回滚以前的更改。

解决方法

更新数据库时我更喜欢什么?哪种方法的优缺点是什么,什么时候应该使用另一种方法?

public void disemployEmployee(Integer employeeId,Date endDate) { Employee employee = (Employee)em.find('Employee',employeeId); employee.getPeriod().setEndDate(endDate); em.flush();}public void disemployEmployee(Integer employeeId,employeeId); em.getTransaction().begin(); employee.getPeriod().setEndDate(endDate); em.getTransaction().commit();}

相关文章: