文章 96
评论 76
浏览 89992
@Scheduled 增加 @Transactional 事务

@Scheduled 增加 @Transactional 事务

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

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



记录精彩的坎坷人生,经营属于自己的世界!