简体   繁体   English

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

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

First of all, I've been looking for an answer to this but I still can't find an answer even after 5 hours of of searching. 首先,我一直在寻找答案,但是即使经过5个小时的搜索,我仍然找不到答案。

Summary: My Transactional class does not rollback when it is called from my Controller class but when it is called in my JUnit test class, it executes the rollback. 简介:从我的Controller类调用它时,我的Transactional类不会回滚, 但是在我的JUnit测试类中调用它时,它将执行回滚。

Controller 控制者

@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 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();
    }
    ...

}

Please note that it successfully rolls back when it is called in my Test class. 请注意,在我的Test类中调用它时,它会成功回滚。 It just doesn't work when called from RestController. 从RestController调用时,它只是不起作用。

Apparently, my tx-annotation statement was inside my db-context.xml that's why it was not working in the actual build but was working in my test class because in my test class, I load specifically the my configurations. 显然,我的tx-annotation语句位于db-context.xml中,这就是为什么它在实际构建中不起作用,但在我的测试类中起作用的原因,因为在我的测试类中,我专门加载了我的配置。 So I put my tx-annotation statement in my dispatcherServlet(servlet-context.xml) and it worked. 因此,我将tx-annotation语句放入了dispatcherServlet(servlet-context.xml)中,它开始工作了。

暂无
暂无

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

相关问题 由另一个 class 调用的同一 class 中的方法调用的事务方法 - Transactional method called by method in same class called from another class 从服务类中调用时,Spring @Transactional不适用于带注释的方法 - Spring @Transactional doesn't work for an annotated method when called from within service class 从测试类调用doInBackground()不会执行 - doInBackground() not getting executed when called from test class 从另一个类调用时,Java PropertyChangeListener不起作用 - Java PropertyChangeListener not working when called from another class 从不同的类调用时,Java repaint()无法正常工作 - Java repaint() not working when called from a different class 当从同一个 class 调用方法时,有效不工作 spring 启动 - valid not working spring boot when method is called from same class 从Spring Boot测试调用的@Caching方法[注有@Transactional]无法正常工作 - @Caching method called from spring boot test [annotated with @Transactional] not working 方法仅在主要 class 中有效,但在从其他 class 调用时无效 - method works only in main class but not when called from its other class 从 Java 类调用时,Kotlin 数据类复制功能不起作用 - Kotlin data class copy function not working when called from java class 试图从“主菜单”类调用方法但它被卡住了,但是当从方法类调用时它工作完美 - Trying to call a method from a "main-menu" class but it is stuck, however when called from the methods class it works perfect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM