kum*_*425 0 java hibernate transactions
我刚开始休眠
在我的项目中,我需要处理交易.如何在两个类中处理声明式事务
例子:
//class 1
class A{
createA()
{
insert(A);
}
}
//class 2
class B
{
createB()
{
insert(B);
}
}
//class 3
@Transaction(Exception.class)
class C
{
test()
{
create(A);
create(B);
}
}
Run Code Online (Sandbox Code Playgroud)
根据上面的代码,有可能处理事务,这样如果classA中的insert成功并且classB中的insert失败,那么事务应该回滚并删除插入表A中对应于A类的记录
请使用声明式交易帮我解决这个问题....
在adavace中感谢....
小智 6
Hibernate就像其他任何东西都支持事务.所以你只需要在事务中包含对update()和save()的调用.
例:
Session sess = factory.openSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();
// your updates to the database
create(A);
create(B);
tx.commit();
}
catch (RuntimeException e) {
if (tx != null) tx.rollback();
throw e; // or display error message
}
finally {
sess.close();
}
Run Code Online (Sandbox Code Playgroud)
你明白了.如果beginTransaction()和commit()之间出现任何问题,则回滚所有内容.
您可能对会话处理有疑问,但这是一个不同的问题.
| 归档时间: |
|
| 查看次数: |
1361 次 |
| 最近记录: |