0
我一直在研究有關ejb3休眠的JPA 2。 所以我做了一個示例類來測試這些功能。我嘗試過使用BMT交易,但面臨交易問題。 從下面的示例代碼中,如果dosomething()
中出現錯誤,將引發異常,因此UserTransaction將被回滾。 但是,我發現即使引發異常,編輯的實體也會更新到數據庫。任何人都可以指出我是否缺少設置中的某些東西?事務沒有回滾,但提交使用Hibernate JPA 2.0和EJB3與BMT
@Stateless(mappedName = "MyManagementBean")
@Local
@TransactionManagement(TransactionManagementType.BEAN)
public class MyManagement implements MyManagementLocal,MyManagementRemote {
@PersistenceUnit(unitName="MyEjb") EntityManagerFactory emf;
@Resource UserTransaction utx;
@Resource SessionContext ctx;
/**
* Default constructor.
*/
public MyManagement() {
// TODO Auto-generated constructor stub
}
public void dosomething(String id) throws Exception
{
try {
utx.begin();
em = emf.createEntityManager();
Myline line = em.find(Myline.class, id);
line.setStatus("R");
em.flush();
utx.commit();
}
catch (Exception e) {
e.printStackTrace();
if (utx != null) utx.rollback();
throw e; // or display error message
}
finally {
em.close();
}
}
有沒有例外。整段代碼似乎運行良好。除了在執行JPA查詢之前,它會自動刷新並將以前的更改提交給數據庫。 – Quincy
對不起,我誤解了這個問題,「但是,我發現編輯後的實體更新到數據庫中,即使拋出了異常。如果我在設置中丟失了某些東西,任何人都可以指出我嗎?」 – Luke
http://stackoverflow.com/questions/6516973/hibernate-entity-manager-auto-flush-before-query-and-commit-changes-to-db-in-tran/6537016#6537016 - 更詳細的相關問題 – Luke