繁体   English   中英

Spring @Transactional注释行为

[英]Spring @Transactional annotation behaviour

我正在构建一个工作流系统,其中的服务层WorkflowServiceImpl处理文档并向用户发送通知。 还有另一个服务DocumentServiceImpl ,它具有方法post()方法,该方法内部调用WorkflowServiceImpl.process()方法。

@Service 
public class WorkflowServiceImpl implements WorkflowService{

    @Transactional(propagation=Propagation.REQUIRES_NEW, noRollbackFor=WorkflowException.class)
    public void process(WorkflowDocument document) throws WorkflowException {

        Boolean result = process(document);
        if(!result){
            throw new WorkflowException();
        }
    }

    private Boolean process(WorkflowDocument document){
        //some processing on document
        updateDocument();
        sendNotifications();
    }

    private void updateDocument(WorkflowDocument document){
        //some update operation
    }

    private void sendNotifications(WorkflowDocument document){
        //send notifications - insertion operation
    }
}

@Service 
public class DocumentServiceImpl implements DocumentService{

    @Autowired private WorkflowService workflowService;

    @Transactional
    public void post(){

        //some operations

        workflowService.process(document);

        //some other operations
    }
}

如您所见,我已标记

DocumentServiceImpl.post() as @Transactional  
WorkflowServiceImpl.process() as @Transactional(propagation=Propagation.REQUIRES_NEW, noRollbackFor=WorkflowException.class)

我正在努力实现这一目标:

1. WorkflowServiceImpl.process() method should commit always(update document and send notifications) - whether a WorkflowException is thrown or not
2. DocumentServiceImpl.post() method should rollback, when WorkflowException is thrown 

当我尝试使用上述交易配置时

1. When WorkflowException is not thrown, the code worked as expected - committed both WorkflowServiceImpl.process() and DocumentServiceImpl.post() methods
2. When WorkflowException is thrown, the request processing is not completed (I can see the request processing symbol in the browser) 

我找不到代码有什么问题。 我正在使用Spring 3.1.4版本

您需要在@Transactional批注中有一个rollbackFor以用于WorkflowException并以REQUIRES_NEW传播

@Transactional(rollbackFor = {WorkflowException.class}, propagation = Propagation.REQUIRES_NEW)
public void post(){

    //some operations

    workflowService.process(document);

    //some other operations
}

这将使新事务以DocumentServiceImpl的post方法开始

暂无
暂无

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

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