长时间运行的Spring Service正在锁定数据库表

时间:2018-03-02 10:16:14

标签: spring jpa

我有一个Spring Service,它通过列表中的多个项目,每个项目都会对外部服务进行额外的WS调用。 Job以固定的时间间隔调用该服务。 作为第一步,服务在JOB_CONTROL表中保存作业的状态(STARTED),然后它遍历列表,最后将其保存到(完成)。

有两个问题:

  1. JOB_CONTROL表并没有逐渐保存 - 只有 " FINISHED"价值被保存,永远不会被开始"
  2. 如果使用flush方法强制提交,表格会被锁定,例如。在服务完成之前,不能对其进行任何其他选择

            @Service
    public class PromotionSchedulerService implements Runnable {
    
        @Autowired
        GeofencingAreaDAO storeDao;
    
        @Autowired
        promotionsWSClient promotionsWSClient;
    
        @Autowired
        private JobControlDAO jobControlDAO;
    
        public void run() {
            JobControl job = jobControlDAO.findByClassName(this.getClass().getSimpleName());
    
            job.setState(JobControlStateTypes.RUNNING.getStateType());
            job.setLastRunDate(new Date());
            // LINE BELLOW DOES NOT GET COMMITED IN DB
            jobControlDAO.save(job);
    
            List < GeofencingArea > stores = storeDao.findAllStores();
            for (GeofencingArea store: stores) {
                /** Call WS **/
                GetActivePromotionsResponse rsp = null;
                try {
                    rsp = promotionsWSClient.getpromotions();
                } catch (Exception e) {
                    e.printStackTrace();
                    job.setState(JobControlStateTypes.FAILED.getStateType());
                    job.setLastRunStatus("There was an error calling promagic promotions");
                    jobControlDAO.save(job);
                    return;
                }
    
                List < PromotionBean > promos = rsp.getReturn();
                for (PromotionBean promo: promos) {
                    BackendPromotionPOJO backendPromotionsPOJO = new BackendPromotionPOJO();
                    backendPromotionsPOJO.setDescription(promo.getDescription());
                }
            }
    
    
            // ONLY THIS JOB STATE GOES TO DB. IT ACTUALLY SEEM TO OVERWRITE PREVIOUS SET VALUE ("RUNNING") from line 16
            job.setLastRunStatus("COMPLETED");
            job.setState(JobControlStateTypes.SUCCESS.getStateType());
            jobControlDAO.save(job);
        }
    }
    
  3. 我想在更改作业状态后强制提交,而不是在执行此操作时锁定表。

0 个答案:

没有答案