事务没有回滚,但使用 Hibernate JPA 2.0 和 EJB3 与 BMT 提交
我一直在研究 JPA 2 和 ejb3 上的 hibernate。 所以我做了一个示例类来测试功能。我尝试过使用 BMT 交易,但遇到交易问题。 从下面的示例代码来看,如果 dosomething()
出现问题,将会抛出异常,因此 UserTransaction 将被回滚。 但是,我发现即使抛出异常,编辑的实体也会更新到数据库。如果我在设置中遗漏了某些内容,有人可以指出我吗?
@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();
}
}
I have been studying about JPA 2 with hibernate on ejb3.
So I made a sample class to test the functionalities. I have tried using BMT transactions but facing problem on transaction.
From the sample code below, if something goes wrong in dosomething()
, an exception will be thrown and so UserTransaction will be rollbacked.
However, I find that the edited entity is updated to the DB even the exception is thrown. Can anyone point me out if I am missing something in the setting?
@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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
异常的类型是什么?程序是否曾调用过
尝试
What is the type of the exception ?. Does program ever hit the call to
try