@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());
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论