日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁(yè) > 科技  > 軟件

Spring事務(wù)超時(shí)到底是怎么回事?

來(lái)源: 責(zé)編: 時(shí)間:2023-10-13 14:36:35 307觀看
導(dǎo)讀環(huán)境:Spring5.3.23 Spring事務(wù)超時(shí)是指一個(gè)事務(wù)在執(zhí)行中最長(zhǎng)的允許時(shí)間。如果事務(wù)在超時(shí)時(shí)間內(nèi)未能完成,則會(huì)自動(dòng)回滾。超時(shí)時(shí)間可以通過(guò)設(shè)置來(lái)控制,以確保事務(wù)在規(guī)定的時(shí)間內(nèi)完成或回滾,避免數(shù)據(jù)一致性問(wèn)題。在工作中你

環(huán)境:Spring5.3.23Frf28資訊網(wǎng)——每日最新資訊28at.com

Spring事務(wù)超時(shí)是指一個(gè)事務(wù)在執(zhí)行中最長(zhǎng)的允許時(shí)間。如果事務(wù)在超時(shí)時(shí)間內(nèi)未能完成,則會(huì)自動(dòng)回滾。超時(shí)時(shí)間可以通過(guò)設(shè)置來(lái)控制,以確保事務(wù)在規(guī)定的時(shí)間內(nèi)完成或回滾,避免數(shù)據(jù)一致性問(wèn)題。Frf28資訊網(wǎng)——每日最新資訊28at.com

在工作中你有配置事務(wù)的超時(shí)時(shí)間嗎?如何進(jìn)行配置事務(wù)超時(shí)時(shí)間?Frf28資訊網(wǎng)——每日最新資訊28at.com

1. 配置事務(wù)超時(shí)時(shí)間

注解方式:Frf28資訊網(wǎng)——每日最新資訊28at.com

// 這里單位是s@Transactional(timeout = 2)public void save() {}

編程方式1:Frf28資訊網(wǎng)——每日最新資訊28at.com

@Resourceprivate PlatformTransactionManager tm ;DefaultTransactionDefinition definition = new DefaultTransactionDefinition();definition.setTimeout(2) ;

編程方式2:Frf28資訊網(wǎng)——每日最新資訊28at.com

@Resourceprivate PlatformTransactionManager tm ;public void update() {  TransactionTemplate template = new TransactionTemplate(tm) ;  template.setTimeout(2) ;  template.execute(new TransactionCallback<Object>() {    @Override    public Object doInTransaction(TransactionStatus status) {      // ...      return null ;    }  }) ;}

以上3種方式讀可以進(jìn)行事務(wù)超時(shí)的設(shè)置,什么情況下才能算是事務(wù)超時(shí)呢?Frf28資訊網(wǎng)——每日最新資訊28at.com

2. 準(zhǔn)備環(huán)境

準(zhǔn)備一張2000w數(shù)據(jù)的表Frf28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片Frf28資訊網(wǎng)——每日最新資訊28at.com

表字段信息如下:Frf28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片Frf28資訊網(wǎng)——每日最新資訊28at.com

此表除主鍵外沒(méi)有任何的索引。Frf28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片Frf28資訊網(wǎng)——每日最新資訊28at.com

3. 模擬事務(wù)超時(shí)

  • 測(cè)試1

統(tǒng)計(jì)查詢表的數(shù)據(jù)量,將該操作放到一個(gè)事務(wù)中,同時(shí)設(shè)置事務(wù)的超時(shí)時(shí)間。Frf28資訊網(wǎng)——每日最新資訊28at.com

// 將超時(shí)時(shí)間設(shè)置為20s@Transactional(timeout = 20)public void query() {  long start = System.currentTimeMillis() ;  jdbcTemplate.execute("select count(*) from p_user") ;  System.out.println("耗時(shí):" + (System.currentTimeMillis() - start) + "毫秒") ;}

執(zhí)行結(jié)果:Frf28資訊網(wǎng)——每日最新資訊28at.com

耗時(shí):3198毫秒

接下來(lái)將超時(shí)時(shí)間改成3sFrf28資訊網(wǎng)——每日最新資訊28at.com

執(zhí)行結(jié)果如下:Frf28資訊網(wǎng)——每日最新資訊28at.com

13:56:01.425 [main] WARN  c.zaxxer.hikari.pool.ProxyConnection - HikariPool-1 - Connection com.mysql.cj.jdbc.ConnectionImpl@504ecd marked as broken because of SQLSTATE(null), ErrorCode(0)com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request  at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:113)  at com.mysql.cj.jdbc.StatementImpl.checkCancelTimeout(StatementImpl.java:2167)

從異常信息看到拋出了超時(shí)異常。這里的超時(shí)是sql執(zhí)行的超時(shí),是由我們的驅(qū)動(dòng)程序拋出的異常。Frf28資訊網(wǎng)——每日最新資訊28at.com

  • 測(cè)試2

模擬其它非數(shù)據(jù)庫(kù)操作耗時(shí),而這個(gè)是在執(zhí)行數(shù)據(jù)庫(kù)操作之前Frf28資訊網(wǎng)——每日最新資訊28at.com

@Transactional(timeout = 2)public void query() {  long start = System.currentTimeMillis() ;  try {    TimeUnit.SECONDS.sleep(3) ;  } catch (InterruptedException e) {    e.printStackTrace();  }  // 執(zhí)行非常簡(jiǎn)單的操作  jdbcTemplate.execute("select 1") ;  System.out.println("耗時(shí):" + (System.currentTimeMillis() - start) + "毫秒") ;}

執(zhí)行結(jié)果:Frf28資訊網(wǎng)——每日最新資訊28at.com

14:08:44.000 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing SQL statement [select 1]Exception in thread "main" org.springframework.transaction.TransactionTimedOutException: Transaction timed out: deadline was Wed Oct 11 14:08:42 CST 2023  at org.springframework.transaction.support.ResourceHolderSupport.checkTransactionTimeout(ResourceHolderSupport.java:155)

拋出了超時(shí)異常,而這個(gè)異常是由Spring框架拋出的。Frf28資訊網(wǎng)——每日最新資訊28at.com

  • 測(cè)試3

模擬其它非數(shù)據(jù)庫(kù)操作耗時(shí),而這個(gè)是在執(zhí)行數(shù)據(jù)庫(kù)操作之后,適當(dāng)調(diào)整上面的代碼順序Frf28資訊網(wǎng)——每日最新資訊28at.com

@Transactional(timeout = 2)public void query() {  long start = System.currentTimeMillis() ;  jdbcTemplate.execute("select 1") ;  try {    TimeUnit.SECONDS.sleep(3) ;  } catch (InterruptedException e) {    e.printStackTrace();  }  System.out.println("耗時(shí):" + (System.currentTimeMillis() - start) + "毫秒") ;}

執(zhí)行結(jié)果:Frf28資訊網(wǎng)——每日最新資訊28at.com

耗時(shí):3015毫秒

程序正常運(yùn)行Frf28資訊網(wǎng)——每日最新資訊28at.com

  • 測(cè)試4

在測(cè)試3的基礎(chǔ)上,最后再次執(zhí)行數(shù)據(jù)庫(kù)相關(guān)的操作Frf28資訊網(wǎng)——每日最新資訊28at.com

@Transactional(timeout = 2)public void query() {  long start = System.currentTimeMillis() ;  jdbcTemplate.execute("select 1") ;  try {    TimeUnit.SECONDS.sleep(3) ;  } catch (InterruptedException e) {    e.printStackTrace();  }  System.out.println("耗時(shí):" + (System.currentTimeMillis() - start) + "毫秒") ;  // 再次執(zhí)行數(shù)據(jù)庫(kù)相關(guān)操作  jdbcTemplate.execute("select 1") ;}

執(zhí)行結(jié)果:Frf28資訊網(wǎng)——每日最新資訊28at.com

耗時(shí):3024毫秒14:14:38.257 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing SQL statement [select 1]Exception in thread "main" org.springframework.transaction.TransactionTimedOutException: Transaction timed out: deadline was Wed Oct 11 14:14:37 CST 2023  at org.springframework.transaction.support.ResourceHolderSupport.checkTransactionTimeout(ResourceHolderSupport.java:155)

第一個(gè)數(shù)據(jù)庫(kù)操作,沒(méi)有拋出異常,直到第二個(gè)執(zhí)行時(shí)拋出了異常。Frf28資訊網(wǎng)——每日最新資訊28at.com

總結(jié):      事務(wù)方法開(kāi)始執(zhí)行時(shí)就開(kāi)始計(jì)時(shí),在執(zhí)行到數(shù)據(jù)庫(kù)操作時(shí)判斷當(dāng)前的執(zhí)行時(shí)間點(diǎn)是否已經(jīng)超過(guò)了設(shè)置的超時(shí)時(shí)間,如果是則拋出Timeout異常。Frf28資訊網(wǎng)——每日最新資訊28at.com

4. 事務(wù)超時(shí)原理

在開(kāi)始一個(gè)事務(wù)時(shí)會(huì)在DataSourceTransactionManager#doBegin方法中設(shè)置超時(shí)時(shí)間Frf28資訊網(wǎng)——每日最新資訊28at.com

protected void doBegin(Object transaction, TransactionDefinition definition) {  int timeout = determineTimeout(definition);  if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {    // 如果注解@Transactional中設(shè)置了timeout,則設(shè)置超時(shí)時(shí)間    txObject.getConnectionHolder().setTimeoutInSeconds(timeout);  }}protected int determineTimeout(TransactionDefinition definition) {  if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {    return definition.getTimeout();  }  return getDefaultTimeout();}

當(dāng)通過(guò)JdbcTemplate操作數(shù)據(jù)庫(kù)時(shí),還會(huì)執(zhí)行如下操作Frf28資訊網(wǎng)——每日最新資訊28at.com

public class JdbcTemplate {  private <T> T execute(StatementCallback<T> action, boolean closeResources) {    Statement stmt = null;    try {      stmt = con.createStatement();      // 配置Statement對(duì)象,這其中會(huì)設(shè)置超時(shí)時(shí)間      applyStatementSettings(stmt);      // ...      return result;    }  }    protected void applyStatementSettings(Statement stmt) throws SQLException {    // ...    // getQueryTimeout方法返回的是當(dāng)前JdbcTemplate對(duì)象中設(shè)置d餓超時(shí)時(shí)間    DataSourceUtils.applyTimeout(stmt, getDataSource(), getQueryTimeout());  }}

DataSourceUtils工具類Frf28資訊網(wǎng)——每日最新資訊28at.com

public abstract class DataSourceUtils {  public static void applyTimeout(Statement stmt, @Nullable DataSource dataSource, int timeout) throws SQLException {    ConnectionHolder holder = null;    if (dataSource != null) {      holder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);    }    // 如果當(dāng)前事務(wù)執(zhí)行配置了超時(shí)時(shí)間    if (holder != null && holder.hasTimeout()) {      // 剩余事務(wù)超時(shí)將覆蓋指定的值。      stmt.setQueryTimeout(holder.getTimeToLiveInSeconds());    }    else if (timeout >= 0) {      // No current transaction timeout -> apply specified value.      stmt.setQueryTimeout(timeout);    }  }  }

ResourceHolderSupport類Frf28資訊網(wǎng)——每日最新資訊28at.com

public abstract class ResourceHolderSupport implements ResourceHolder {   public int getTimeToLiveInSeconds() {    double diff = ((double) getTimeToLiveInMillis()) / 1000;    int secs = (int) Math.ceil(diff);    // 檢查超時(shí)時(shí)間    checkTransactionTimeout(secs <= 0);    return secs;  }  private void checkTransactionTimeout(boolean deadlineReached) throws TransactionTimedOutException {    if (deadlineReached) {      // 設(shè)置事務(wù)回滾      setRollbackOnly();      // 拋出異常      throw new TransactionTimedOutException("Transaction timed out: deadline was " + this.deadline);    }  }}

完畢!!!Frf28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-13520-0.htmlSpring事務(wù)超時(shí)到底是怎么回事?

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 如何搭建高性能廣告技術(shù)需求方平臺(tái)

下一篇: 程序員必會(huì)之最詳細(xì)的ThreadPoolExecutor 線程池七大參數(shù)含義

標(biāo)簽:
  • 熱門(mén)焦點(diǎn)
  • 對(duì)標(biāo)蘋(píng)果的靈動(dòng)島 華為帶來(lái)實(shí)況窗功能

    繼蘋(píng)果的靈動(dòng)島之后,華為也在今天正式推出了“實(shí)況窗”功能。據(jù)今天鴻蒙OS 4.0的現(xiàn)場(chǎng)演示顯示,華為的實(shí)況窗可以更高效的展現(xiàn)出實(shí)時(shí)通知,比如鎖屏上就能看到外賣、打車、銀行
  • 太卷!Redmi MAX 100英寸電視便宜了:12999元買Redmi史上最大屏

    8月5日消息,從小米商城了解到,Redmi MAX 100英寸巨屏電視日前迎來(lái)官方優(yōu)惠,到手價(jià)12999元,比發(fā)布價(jià)便宜了7000元,在大屏電視市場(chǎng)開(kāi)卷。據(jù)了解,Redmi MAX 100
  • SpringBoot中使用Cache提升接口性能詳解

    環(huán)境:springboot2.3.12.RELEASE + JSR107 + Ehcache + JPASpring 框架從 3.1 開(kāi)始,對(duì) Spring 應(yīng)用程序提供了透明式添加緩存的支持。和事務(wù)支持一樣,抽象緩存允許一致地使用各
  • 分享六款相見(jiàn)恨晚的PPT模版網(wǎng)站, 祝你做出精美的PPT!

    1、OfficePLUSOfficePLUS網(wǎng)站旨在為全球Office用戶提供豐富的高品質(zhì)原創(chuàng)PPT模板、實(shí)用文檔、數(shù)據(jù)圖表及個(gè)性化定制服務(wù)。優(yōu)點(diǎn):OfficePLUS是微軟官方網(wǎng)站,囊括PPT模板、Word模
  • 為什么你不應(yīng)該使用Div作為可點(diǎn)擊元素

    按鈕是為任何網(wǎng)絡(luò)應(yīng)用程序提供交互性的最常見(jiàn)方式。但我們經(jīng)常傾向于使用其他HTML元素,如 div span 等作為 clickable 元素。但通過(guò)這樣做,我們錯(cuò)過(guò)了許多內(nèi)置瀏覽器的功能。
  • 慕巖炮轟抖音,百合網(wǎng)今何在?

    來(lái)源:價(jià)值研究所 作者:Hernanderz&ldquo;難道就因?yàn)樽约旱囊粋€(gè)產(chǎn)品牛逼了,從客服到總裁,都不愿意正視自己產(chǎn)品和運(yùn)營(yíng)上的問(wèn)題,選擇逃避了嗎?&rdquo;這一番話,出自百合網(wǎng)聯(lián)合創(chuàng)
  • 本地生活這塊肥肉,拼多多也想吃一口

    出品/壹覽商業(yè) 作者/李彥編輯/木魚(yú)拼多多也看上本地生活這塊蛋糕了。近期,拼多多在App首頁(yè)&ldquo;充值中心&rdquo;入口上線了本機(jī)生活界面。壹覽商業(yè)發(fā)現(xiàn),該界面目前主要
  • ESG的面子與里子

    來(lái)源 | 光子星球撰文 | 吳坤諺編輯 | 吳先之三伏大幕拉起,各地高溫預(yù)警不絕,但處于厄爾尼諾大&ldquo;烤&rdquo;之下的除了眾生,還有各大企業(yè)發(fā)布的ESG報(bào)告。ESG是&ldquo;環(huán)境保
  • 上海舉辦人工智能大會(huì)活動(dòng),建設(shè)人工智能新高地

    人工智能大會(huì)在上海浦江兩岸隆重拉開(kāi)帷幕,人工智能新技術(shù)、新產(chǎn)品、新應(yīng)用、新理念集中亮相。8月30日晚,作為大會(huì)的特色活動(dòng)之一的上海人工智能發(fā)展盛典人工
Top 主站蜘蛛池模板: 霍邱县| 同德县| 中超| 罗定市| 尚志市| 四平市| 安岳县| 鸡泽县| 台东市| 镇江市| 抚顺县| 静宁县| 贡嘎县| 丹寨县| 阳曲县| 芦溪县| 宿州市| 纳雍县| 顺昌县| 威远县| 宣威市| 会同县| 苗栗县| 梅州市| 万盛区| 富宁县| 化隆| 巴中市| 四平市| 沁阳市| 章丘市| 新田县| 屏东市| 平远县| 深泽县| 毕节市| 大余县| 嘉义市| 永寿县| 钦州市| 连云港市|