簡體   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