@Scheduled 增加 @Transactional 事务

Updated on in Java是世界上最好的语言 with 0 views and 0 comments

  

定时任务与事务合理搭配结构为:

@Autowired
Y y;

@ Scheduled()
void xxx(){
   y.x();
}


class Y {
   @Transactional
   x(){
   }
}

错误示例:

@Scheduled(fixedDelay = 5201314)   
 @Autowired   
 public void storeHuaweiData(){   
 iAgentQueryService.delHwSkillGroups();   
 int i = 0/1;   
 int i2 = 1/0;   
 iAgentQueryService.addHwSkillGroups();   
 }   

生效示例:

    @Scheduled(fixedDelay = 5201314)
    public void storeHuaweiData(){
        SkillGroupsTransactional skillGroupsTransactional = new SkillGroupsTransactional();
        skillGroupsTransactional.transactional(hwSkillGroups);
    }
package com.ulane.spring.job;

import com.ulane.spring.bean.agent.HwSkillGroup;
import com.ulane.spring.service.IAgentQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Xu Yuntong
 * @date 2019/11/30 15:21
 */
public class SkillGroupsTransactional {

    @Autowired
    protected IAgentQueryService iAgentQueryService;

    @Transactional
    public void transactional(List<HwSkillGroup> hwSkillGroups){
        iAgentQueryService.delHwSkillGroups();
        int i = 0/1;
        int i2 = 1/0;
        iAgentQueryService.addHwSkillGroups(hwSkillGroups);
    }
}


标题:@Scheduled 增加 @Transactional 事务
作者:MaidongAndYida
地址:https://cuijianzhe.github.io/articles/2019/11/30/1575099482173.html