首先,我一直在寻找这个问题的答案,但即使经过 5 个小时的搜索,我仍然找不到答案。
总结:我的 Transactional 类在从我的 Controller 类中调用时不会回滚,但是在我的 JUnit 测试类中调用时,它会执行回滚。
控制器
@RestController
public class NotifyServerController extends BaseController {
...
@RequestMapping(value = PathConstants.PATH_NOTIFY_SERVER, method = RequestMethod.POST)
public WiremoRoot notifyServer(// parameters //) throws SQLException {
... authentication and other if statements
// State monitoring request (control box information)
else if(content instanceof SpecificRequest){
response.getContents().setContent(serviceObj.processRequest(request));
} else
throw new InputErrorException();
return response;
}
}
服务对象实现
@Service(// Qualifier //)
@Transactional
public class ServiceClassImplementation implements ServiceClass {
// -- Dao objects here
/**
* {@inheritDoc}
*/
public Response processRequest(// parameters //) throws SQLException {
...
methodA();
}
public void methodA(){
...
throw new RuntimeException();
}
...
}
请注意,它在我的 Test 类中被调用时成功回滚。从 RestController 调用它时它不起作用。
显然,我的 tx-annotation 语句在我的 db-context.xml 中,这就是为什么它在实际构建中不起作用,但在我的测试类中起作用,因为在我的测试类中,我专门加载了我的配置。所以我把我的 tx-annotation 语句放在我的 dispatcherServlet(servlet-context.xml) 中并且它起作用了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句