`@Transactional` 不适用于 Spring 2 控制器
`@Transactional` not working for Spring 2 controller
我的应用程序中有一个旧控制器,它在 xml 中定义为 spring bean,并使用 Spring 的 SimpleFormController. I've tried to make the processes within the onSubmit
method of the controller transactional by adding the @Transactional
annotation but it doesn't work. According to this guide 调用注解必须发生 "outside of the bean",这是否意味着注解不能在像我这样的旧 Spring 控制器中使用?有任何替代方案或解决方法吗?
我知道它不起作用的原因是 1) 对数据库的更改不会因错误而回滚(尽管我已经定义了 rollbackFor = Exception.class
,甚至在某些情况下使用 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
,在这种情况下,它尝试使用后者,它会抛出一个错误,说明不存在事务。2) 我在 Spring 和 [=] 中实例化 @Transactional
的位置添加了断点其中 25=] 被击中。
编辑:所以人们要求可重现的代码示例。问题不在业务逻辑代码中,我正在寻找 Spring 2 控制器中注释用法的清晰度。所以我有这样的例子:
public class ImportController extends SimpleFormController {
@Override
@Transactional(rollbackFor = Exception.class)
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
...
}
}
你是对的。 @Transactional
在这里不起作用,因为 onSubmit
被同一个 bean 调用。
在这种情况下,调用是直接完成的,默认的 spring 事务处理不起作用。
查看 this question 中的答案,详细了解您的选项
我的应用程序中有一个旧控制器,它在 xml 中定义为 spring bean,并使用 Spring 的 SimpleFormController. I've tried to make the processes within the onSubmit
method of the controller transactional by adding the @Transactional
annotation but it doesn't work. According to this guide 调用注解必须发生 "outside of the bean",这是否意味着注解不能在像我这样的旧 Spring 控制器中使用?有任何替代方案或解决方法吗?
我知道它不起作用的原因是 1) 对数据库的更改不会因错误而回滚(尽管我已经定义了 rollbackFor = Exception.class
,甚至在某些情况下使用 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
,在这种情况下,它尝试使用后者,它会抛出一个错误,说明不存在事务。2) 我在 Spring 和 [=] 中实例化 @Transactional
的位置添加了断点其中 25=] 被击中。
编辑:所以人们要求可重现的代码示例。问题不在业务逻辑代码中,我正在寻找 Spring 2 控制器中注释用法的清晰度。所以我有这样的例子:
public class ImportController extends SimpleFormController {
@Override
@Transactional(rollbackFor = Exception.class)
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
...
}
}
你是对的。 @Transactional
在这里不起作用,因为 onSubmit
被同一个 bean 调用。
在这种情况下,调用是直接完成的,默认的 spring 事务处理不起作用。
查看 this question 中的答案,详细了解您的选项