EJB里的事务的问题【80分】

来源:百度知道 编辑:UC知道 时间:2024/06/25 09:59:53
第一个问题:
EJB里一个事务其实就是一个方法要执行的东西对吗?
比如你们看我这样说对不对:
void insert(int i) throws Exception
{
em.persist(i);
}
我说“在这里的事务就是insert要执行的东西”对吗?
第二个问题:
它一抛出异常的话是不是这个方法的整个事务都会回滚?就像这个方法没被调用过
第三个问题:
假如用try-catch块来捕捉异常也会整个事务回滚对吗?

应该对吧;
自动回滚的话,要求你那个异常是RunTimeException的子类。

1 事务是一组操作,要保证完整性,也就是要么全部成功完成,要么全部不做。不会出现只完成了一部分的情况。这个是数据库层保证的。

当然,你的操作必须在一个事务里面才行。

比如你的insert是最简单的事务,如果你insert后,还需要同时更新多个数据,同样可以在这个事务里完成。

2 如果事务里某个操作发生异常,而且在你不能修正的时候,比如取款不能透支,取款额度大于余额,此时只能回滚整个事务了。

3 回滚使用 rollback ,他会把你整个事务做过的所有数据操作全部取消。
如果你不自己rollback,而是把异常抛给了容器,则默认是回滚的。
不建议你这么做。还是自己控制比较稳妥、

this is a good question.

ejb3:
a ejb method is automatically in a transaction and an SQL statement would be committed as a transaction since the default commit mode would normally be auto-commit.

there are two types of transactions for ejb: container managed transactions(CMT) and bean managed transactions(BMT).

only message driven beans and session beans can manage transactions by themselves, ie, BMT. entity beans can only have CMT.

when system exceptions occur, transaction would be rolled back automatically. but this is not the case for