繁体   English   中英

从Controller类调用时,事务性回滚不起作用,但从Test类调用时,则起作用

[英]Transactional rollback not working when called from Controller class but works when called from Test Class

首先,我一直在寻找答案,但是即使经过5个小时的搜索,我仍然找不到答案。

简介:从我的Controller类调用它时,我的Transactional类不会回滚, 但是在我的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;
}

}

ServiceObjectImplementation

@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)中,它开始工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM