Tag Archives: QuartzQuartz

Quartz: add transaction rollback error [How to Solve]

automatic task class:

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class ReCodeBack implements Job {

    private static final Logger LOGGER = LoggerFactory.getLogger(ReCodeBack.class);

    @Autowired
    ReCodeBackTag reCodeBackTag;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        reCodeBackTag.backupsReCode();
    }


}

Automatic task annotation:

@PersistJobDataAfterExecution

@DisallowConcurrentExecution

task requirements are transactions that need to be added

use spring annotation @ transactional annotation

    /**
     * Adding (deleting) table data
     * @param object
     * @param modular
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int tableOperation (Object object,String modular){
    int count = 0;
    try{
        .......
    }catch (Exception e) {
            LOGGER.info("[Add (delete) table data] ERROR : {}", e.getMessage());
            //transaction rollback
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //Task is abnormally interrupted and immediately re-run
            JobExecutionException e2 = new JobExecutionException(e);
            e2.setRefireImmediately(true);
            throw e2;
        }finally {
            return count;
        }
    }

Note: when the author uses @transactional annotation in the automatic task class, the program will report an error (too lazy… The reason is not clear, and it will be supplemented later). Therefore, the author is divided into two classes to write, one-timing call class and one business class, as shown in the above code