解決@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默認不開啟事物 等等就是想使事物進行回滾 可是造化弄人呀
廢話不多說,直接上最關(guān)鍵的!!
// 在連接數(shù)據(jù)庫也就是date jpa 工程的啟動類中 配置
@Bean(name="transactionManager")
public PlatformTransactuibManager configurationTm(EntityManagerFactory factory){
return new JpaTransactionManager(factory);
}關(guān)于Scheduled定時任務(wù)
認識定時器
https://cron.qqe2.com/
定時器表達式生成器
定時器場景使用
/*
使用定時任務(wù)關(guān)閉超期未支付訂單,會存在的弊端
1、會有時間差,導(dǎo)致程序不嚴謹
2、不支持集群:單機沒毛病,使用集群后,就會有多個定時任務(wù)
解決方案:只使用一臺計算機節(jié)點,單獨用來運行所有的定時任務(wù)
3、會全表檢索數(shù)據(jù)庫,對數(shù)據(jù)庫的性能有極大的影響:數(shù)據(jù)量大的情況
定時任務(wù),僅僅只適用于小型輕量級項目,傳統(tǒng)項目
最好使用消息隊列 :MQ:RabbitMQ、RockerMQ、Kafka、ZeroMQ...
延時任務(wù)(隊列)
*/定時器的使用
1、需要定時執(zhí)行的方法上加上@Scheduled注解,這個注解中可以指定定時執(zhí)行的規(guī)則,稍后詳細介紹。
2、Spring容器中使用@EnableScheduling開啟定時任務(wù)的執(zhí)行,此時spring容器才可以識別@Scheduled標注的方法,然后自動定時執(zhí)行。
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: 定時任務(wù)
* <p>
* https://cron.qqe2.com/ cron表達式網(wǎng)站
*/
@Component
public class OrderJob {
@Autowired
private OrderService orderService;
/*
使用定時任務(wù)關(guān)閉超期未支付訂單,會存在的弊端
1、會有時間差,導(dǎo)致程序不嚴謹
2、不支持集群:單機沒毛病,使用集群后,就會有多個定時任務(wù)
解決方案:只使用一臺計算機節(jié)點,單獨用來運行所有的定時任務(wù)
3、會全表檢索數(shù)據(jù)庫,對數(shù)據(jù)庫的性能有極大的影響:數(shù)據(jù)量大的情況
定時任務(wù),僅僅只適用于小型輕量級項目,傳統(tǒng)項目
最好使用消息隊列 :MQ:RabbitMQ、RockerMQ、Kafka、ZeroMQ...
延時任務(wù)(隊列)
*/
/*定時檢索未支付的訂單,并將其關(guān)閉*/
//@Scheduled(cron = "0/3 * * * * ?")
@Scheduled(cron = "* * 0/1 * * ?")
public void autoCloseOrder() {
orderService.closeOrder();
// System.out.println("執(zhí)行定時任務(wù),當前時間為:" + 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 /*開啟事務(wù)管理 - 但是因為springboot自動裝配(事務(wù)的自動裝配)的關(guān)系我們不去使用這個注釋*/
@MapperScan(basePackages = "com.laity.mapper") /*掃描 mybatis 通用 mapper 所在的包*/
// 掃描所有包 以及 相關(guān)組件包 idworker => 主鍵生成
@ComponentScan(basePackages = {"com.laity", "org.n3r.idworker"})
@EnableScheduling /*開啟定時任務(wù)*/
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
log.info(run.toString());
}
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java微信公眾平臺開發(fā)(15) 微信JSSDK的使用
這篇文章主要為大家詳細介紹了Java微信公眾平臺開發(fā)第十五步,微信JSSDK的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
springboot調(diào)用webservice-soap接口的實現(xiàn)
接口協(xié)議目前廣泛使用的有http協(xié)議和RPC協(xié)議和webservice,本文主要介紹了springboot調(diào)用webservice-soap接口的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-03-03
PowerJob的DesignateServer工作流程源碼解讀
這篇文章主要介紹了PowerJob的DesignateServer工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
使用?Spring?AI?+?Ollama?構(gòu)建生成式?AI?應(yīng)用的方法
通過集成SpringBoot和Ollama,本文詳細介紹了如何構(gòu)建生成式AI應(yīng)用,首先,介紹了AI大模型服務(wù)的兩種實現(xiàn)方式,選擇使用ollama進行部署,隨后,通過SpringBoot+SpringAI來實現(xiàn)應(yīng)用構(gòu)建,本文為開發(fā)者提供了一個實用的指南,幫助他們快速入門生成式AI應(yīng)用的開發(fā)2024-11-11
詳解在spring中使用JdbcTemplate操作數(shù)據(jù)庫的幾種方式
這篇文章主要介紹了詳解在spring中使用JdbcTemplate操作數(shù)據(jù)庫的幾種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Java實現(xiàn)bmp和jpeg圖片格式互轉(zhuǎn)
本文主要介紹了Java實現(xiàn)bmp和jpeg圖片格式互轉(zhuǎn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
SpringBoot thymeleaf eclipse熱部署方案操作步驟
今天小編就為大家分享一篇關(guān)于SpringBoot thymeleaf eclipse熱部署方案操作步驟,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Spring?Data?JPA實現(xiàn)審計功能過程詳解
Spring?Data?JPA為跟蹤持久性層的變化提供了很好的支持。通過使用審核,我們可以存儲或記錄有關(guān)實體更改的信息,例如誰創(chuàng)建或更改了實體以及何時進行更改2023-02-02

