如何在JPA中回滚事务? [英] How to rollback transaction in JPA?
问题描述
我有一个由Spring框架维护的 EntityManager
对象,我使用 @PersistenceContext $ c将它注入到我想要的任何DAO类中
pre $ $ $ $
private私人EntityManager em;
我使用这些DAO类来保存数据库中的东西。
class MyClass
{
@Resource(name =myDao)
private MyDao dao;
@Resource(name =myAnotherDao)
private MyAnotherDao anotherDao;
$ b $ public void save(String s1,String s2)
{
try
{
MyEntity m = new MyEntity();
m.setName(s1);
// ..等等。
XYZ x = new XYZ();
x.setDEF(s2);
anotherDao.save(x);
m.setXYZ(x);
// ..一些其他的东西.. //
dao.saveEntity(m);
}
catch(Exception e)
{
//我想回滚事务
}
}
}
现在,这两个daos都使用相同的 EntityManager
注入通过 @PersistenceContext(unitName =entityManager)
。现在,如果在 setXYZ()
之后发生异常,那么我想回滚甚至保存的 XYZ
实体。但是,如何从中获取 EntityManager
?
如果所有daos拥有相同的对象,则可以我只是调用 EntityManager
类的 getTransaction()。rollback()
方法? getTransaction()
是否返回一个新的事务或任何当前与 EntityManager
?
EntityManager em = createEntityManager();
尝试{
em.getTransaction()。begin();
//对EntityManager执行一些操作,如persist(),merge()或remove()
em.getTransaction()。commit();
} catch(Exception e){
em.getTransaction()。rollback();
}
em.close();
更多资讯:
< a href =http://en.wikibooks.org/wiki/Java_Persistence/Transactions =nofollow> http://en.wikibooks.org/wiki/Java_Persistence/Transactions
< a href =http://www.developerscrappad.com/547/java/java-ee/ejb3-x-jpa-when-to-use-rollback-and-setrollbackonly/#sthash.jx3XlK5m.dpuf = nofollow的> http://www.developerscrappad.com/547/java/java-ee/ejb3-x-jpa-when-to-use-rollback-and-setrollbackonly/#sthash.jx3XlK5m.dpuf
I have an EntityManager
object maintained by the Spring framework and I inject it in whatever DAO class I want using the @PersistenceContext
annotation like this..
@PersistenceContext(unitName="entityManager")
private EntityManager em;
I use those DAO classes to save in the database something like this..
class MyClass
{
@Resource(name="myDao")
private MyDao dao;
@Resource(name="myAnotherDao")
private MyAnotherDao anotherDao;
public void save(String s1,String s2)
{
try
{
MyEntity m=new MyEntity();
m.setName(s1);
// .. and so on ..
XYZ x=new XYZ();
x.setDEF(s2);
anotherDao.save(x);
m.setXYZ(x);
// .. some other stuff .. //
dao.saveEntity(m);
}
catch(Exception e)
{
// I would like to rollback the transaction
}
}
}
Now, both the daos here use the same EntityManager
injected through @PersistenceContext(unitName="entityManager")
. Now, if an exception occurs after setXYZ()
, then I would like to rollback even the saved XYZ
entity. But, how do I get the EntityManager
from that?
If all the daos hold the same object, then can I just call the getTransaction().rollback()
method of the EntityManager
class? Does the getTransaction()
return a new transaction or any transaction that is currently associated with EntityManager
?
If you used Spring AOP to manage transaction, and the configuration and annotation is used right, the default effect is the transaction would be rolled back when the runtime exception occurs.
If you managed transaction manually, you can roll back transaction like this:
EntityManager em = createEntityManager(); try { em.getTransaction().begin(); // Do something with the EntityManager such as persist(), merge() or remove() em.getTransaction().commit(); } catch(Exception e) { em.getTransaction().rollback(); } em.close();
See more at: http://en.wikibooks.org/wiki/Java_Persistence/Transactions http://www.developerscrappad.com/547/java/java-ee/ejb3-x-jpa-when-to-use-rollback-and-setrollbackonly/#sthash.jx3XlK5m.dpuf
这篇关于如何在JPA中回滚事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!