【发布时间】:2018-07-08 08:30:43
【问题描述】:
考虑 Spring MVC 4 中的以下场景:有一个继承 GenericDao 的子类 RoleDao。 RoleDao 中的方法 createRole() 使用程序化事务管理调用 GenericDao 中的两个方法 method1,method2。 method1 和 method2 都可以自行捕获异常。请参阅下面的代码。我的问题是如何在超类GenericDao 的method1 或method2 中抛出异常时在方法createRole 中回滚事务
public class RoleDao extends GenericDao {
public int createRole() {
try
{
TransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus status = transactionManager.getTransaction(def);
this.methhod1();
this.methhod2();
transactionManager.commit(status);
}
catch (InvalidResultSetAccessException e)
{
transactionManager.rollback(status);
throw new RuntimeException(e);
}
catch (DataAccessException e)
{
transactionManager.rollback(status);
throw new RuntimeException(e);
}
}
public class GenericDao{
public int method1() {
try
{
..........
.........
}
catch (InvalidResultSetAccessException e)
{
throw new RuntimeException(e);
}
catch (DataAccessException e)
{
throw new RuntimeException(e);
}
}
public int method2() {
try
{
.........
.........
}
catch (InvalidResultSetAccessException e)
{
throw new RuntimeException(e);
}
catch (DataAccessException e)
{
throw new RuntimeException(e);
}
}
}
【问题讨论】:
-
当您说回滚时,您可能是指前滚?
标签: spring model-view-controller exception-handling transactions