superclass中抛出异常时如何回滚subclass中的事务
How to rollback a transaction in subclass when an exception is thrown in super class
考虑 Spring MVC 4 中的以下场景:有子class RoleDao
继承了 GenericDao
。 RoleDao
中的方法 createRole()
使用程序化事务管理调用 GenericDao
中的两个方法 method1
、method2
。 method1
和 method2
都在下面的 right.See 代码中捕获异常。我的问题是如何在超级 class GenericDao
[=25= 的 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 注释。 @Transactional over RoleDao class 可能会在这里帮助你。
移除超类方法中的所有异常,它们应该由子类方法处理。
考虑 Spring MVC 4 中的以下场景:有子class RoleDao
继承了 GenericDao
。 RoleDao
中的方法 createRole()
使用程序化事务管理调用 GenericDao
中的两个方法 method1
、method2
。 method1
和 method2
都在下面的 right.See 代码中捕获异常。我的问题是如何在超级 class GenericDao
[=25= 的 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 注释。 @Transactional over RoleDao class 可能会在这里帮助你。
移除超类方法中的所有异常,它们应该由子类方法处理。