解决@Scheduled定时器使用@Thransactional事物问题
作者:墨止戈
这篇文章主要介绍了解决@Scheduled定时器使用@Thransactional事物问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
@Scheduled定时器使用@Thransactional问题
最近遇到一个非常棘手的问题
当jpa使用update/delete时如果不添加@Thransactional会报
Executing an update/delete query;
nested exception is javax.persistence.TransactionRequiredException: Executing anupdate/delete query异常
可是我添加@Thransactional还是会报这个异常
问了好多同行,他们都是告诉我定时器不能使用事物,实际并不是这样的,我查看了很多资料是可以使用的,@Scheduled和@Thransaction不能在同一个类中。
并且实体类要加上@EnableTransactionManagement注解,使我们这个工程支持事物
后来我试了很多
@Thransactional(rolbackFor=ThrowAble.class) @Thransactional(rolbackFor=Exception.class) @Thransactional(ReadOnly=false)//jpa默认不开启事物 等等就是想使事物进行回滚 可是造化弄人呀
废话不多说,直接上最关键的!!
// 在连接数据库也就是date jpa 工程的启动类中 配置
@Bean(name="transactionManager")
public PlatformTransactuibManager configurationTm(EntityManagerFactory factory){
return new JpaTransactionManager(factory);
}关于Scheduled定时任务
认识定时器
https://cron.qqe2.com/
定时器表达式生成器
定时器场景使用
/*
使用定时任务关闭超期未支付订单,会存在的弊端
1、会有时间差,导致程序不严谨
2、不支持集群:单机没毛病,使用集群后,就会有多个定时任务
解决方案:只使用一台计算机节点,单独用来运行所有的定时任务
3、会全表检索数据库,对数据库的性能有极大的影响:数据量大的情况
定时任务,仅仅只适用于小型轻量级项目,传统项目
最好使用消息队列 :MQ:RabbitMQ、RockerMQ、Kafka、ZeroMQ...
延时任务(队列)
*/定时器的使用
1、需要定时执行的方法上加上@Scheduled注解,这个注解中可以指定定时执行的规则,稍后详细介绍。
2、Spring容器中使用@EnableScheduling开启定时任务的执行,此时spring容器才可以识别@Scheduled标注的方法,然后自动定时执行。
Component
package com.laity.config;
import com.laity.service.OrderService;
import com.laity.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author: Laity
* @Project: JavaLaity
* @Package: com.laity.config.OrderJob
* @Date: 2022年08月12日 11:55
* @Description: 定时任务
* <p>
* https://cron.qqe2.com/ cron表达式网站
*/
@Component
public class OrderJob {
@Autowired
private OrderService orderService;
/*
使用定时任务关闭超期未支付订单,会存在的弊端
1、会有时间差,导致程序不严谨
2、不支持集群:单机没毛病,使用集群后,就会有多个定时任务
解决方案:只使用一台计算机节点,单独用来运行所有的定时任务
3、会全表检索数据库,对数据库的性能有极大的影响:数据量大的情况
定时任务,仅仅只适用于小型轻量级项目,传统项目
最好使用消息队列 :MQ:RabbitMQ、RockerMQ、Kafka、ZeroMQ...
延时任务(队列)
*/
/*定时检索未支付的订单,并将其关闭*/
//@Scheduled(cron = "0/3 * * * * ?")
@Scheduled(cron = "* * 0/1 * * ?")
public void autoCloseOrder() {
orderService.closeOrder();
// System.out.println("执行定时任务,当前时间为:" + DateUtil.getCurrentDateString(DateUtil.DATETIME_PATTERN));
}
}启动类
在启动类中开启定时器
package com.laity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.annotation.MapperScan;
/**
* @author: Laity
* @Project: JavaLaity
* @Package: com.laity.Application
* @Date: 2022年07月19日 15:55
* @Description: 自动装配注解
* @EnableAutoConfiguration
*/
@Slf4j
@ServletComponentScan
@SpringBootApplication
//@EnableTransactionManagement /*开启事务管理 - 但是因为springboot自动装配(事务的自动装配)的关系我们不去使用这个注释*/
@MapperScan(basePackages = "com.laity.mapper") /*扫描 mybatis 通用 mapper 所在的包*/
// 扫描所有包 以及 相关组件包 idworker => 主键生成
@ComponentScan(basePackages = {"com.laity", "org.n3r.idworker"})
@EnableScheduling /*开启定时任务*/
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
log.info(run.toString());
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
