Mysql中的超時(shí)時(shí)間設(shè)置方式
1、建立數(shù)據(jù)庫(kù)連接超時(shí)設(shè)置
jdbc:mysql://localhost:3306/gjm?connectTimeout=3000
connectTimeout
:表示的是數(shù)據(jù)庫(kù)驅(qū)動(dòng)(mysql-connector-java) 與 mysql服務(wù)器建立TCP連接的超時(shí)時(shí)間。屬于TCP層面的超時(shí)。
假設(shè)正常與數(shù)據(jù)建立連接需要3ms,我們可以通過(guò)設(shè)置 connectTimeout=1ms 來(lái)模擬。
將會(huì)出現(xiàn)如下錯(cuò)誤:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failureThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
...
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
...
當(dāng)拋出異常后,調(diào)用 conn.isClosed() 返回 true。
2、socket 讀取超時(shí)設(shè)置
jdbc:mysql://localhost:3306/gjm?connectTimeout=1000&socketTimeout=60000
socketTimeout
:是通過(guò)TCP連接發(fā)送數(shù)據(jù)(要執(zhí)行的sql)后,等待響應(yīng)的超時(shí)時(shí)間。屬于TCP層面的超時(shí)。
我們可以通過(guò)在查詢中設(shè)置 sleep(n) 來(lái)進(jìn)行模擬。
將會(huì)出現(xiàn)如下錯(cuò)誤:
Last packet sent to the server was 60000 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2871)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3414)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1383)
...
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2428)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2882)
... 9 more
當(dāng)拋出異常后,調(diào)用 conn.isClosed() 返回 true。
3、單個(gè) sql 執(zhí)行超時(shí)設(shè)置
Connection conn = datasource.getConnection(); PreparedStatement ps = conn.prepareStatement("select sleep(5)"); // 設(shè)置服務(wù)端休眠5秒再返回 ps.setQueryTimeout(1); // 設(shè)置語(yǔ)句執(zhí)行超時(shí)時(shí)間為1秒
將出現(xiàn)如下錯(cuò)誤:
com.mysql.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1881)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1962)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy6.executeQuery(Unknown Source)
...
通過(guò)提示信息,可以看到 "Statement cancelled due to timeout or client request",表示 sql 由于執(zhí)行超時(shí)而被取消了。
通過(guò)statement timeout,我們可以更加靈活的為不同的sql設(shè)置不同的超時(shí)時(shí)間。
但是,需要注意的是,盡管statement timeout很靈活,但是在高并發(fā)的情況下,會(huì)創(chuàng)建大量的線程,一些場(chǎng)景下筆者并不建議使用。
原因在于,mysql-connector-java底層是通過(guò)定時(shí)器Timer來(lái)實(shí)現(xiàn)statement timeout的功能,也就是說(shuō),對(duì)于設(shè)置了statement timeout的sql,將會(huì)導(dǎo)致mysql創(chuàng)建定時(shí)Timer來(lái)執(zhí)行sql,意味著高并發(fā)的情況下,mysql驅(qū)動(dòng)可能會(huì)創(chuàng)建大量線程。
通過(guò)源碼可以看到,在指定statement timeout的情況下,mysql內(nèi)部會(huì)將sql執(zhí)行操作包裝成一個(gè)CancelTask,然后通過(guò)定時(shí)器Timer來(lái)運(yùn)行。
Timer實(shí)際上是與ConnectionImpl綁定的,同一個(gè)ConnectionImpl執(zhí)行的多個(gè)sql,會(huì)共用這個(gè)Timer。
默認(rèn)情況下,這個(gè)Timer是不會(huì)創(chuàng)建的,一旦某個(gè)ConnectionImpl上執(zhí)行的一個(gè)sql,指定了statement timeout,此時(shí)這個(gè)Timer才創(chuàng)建,一直到這個(gè)ConnectionImpl被銷(xiāo)毀時(shí),Timer才會(huì)取消。
在一些場(chǎng)景下,如分庫(kù)分表、讀寫(xiě)分離,如果使用的數(shù)據(jù)庫(kù)中間件是基于smart-client方式實(shí)現(xiàn)的,會(huì)與很多庫(kù)建立連接,由于其底層最終也是通過(guò)mysql-connector-java創(chuàng)建連接,這種場(chǎng)景下,如果指定了statement timeout,那么應(yīng)用中將會(huì)存在大量的Timer線程,在這種場(chǎng)景下,并不建議設(shè)置。
最后,需要提醒的是,socket timeout是TCP層面的超時(shí),是操作系統(tǒng)層面進(jìn)行的控制,statement timeout是驅(qū)動(dòng)層面實(shí)現(xiàn)的超時(shí),是應(yīng)用層面進(jìn)行的控制,如果同時(shí)設(shè)置了二者,那么后者必須比前者大,否則statement timeout無(wú)法生效。
當(dāng)拋出異常后,調(diào)用 conn.isClosed() 返回 false。
4、事務(wù)執(zhí)行超時(shí)設(shè)置
前面提到的的socket timeout、statement timeout,都是限制單個(gè)sql的最大執(zhí)行超時(shí)。
在事務(wù)的情況下,可能需要執(zhí)行多個(gè)sql,我們想針對(duì)整個(gè)事務(wù)設(shè)置一個(gè)最大的超時(shí)時(shí)間。
例如,我們?cè)诓捎胹pring配置事務(wù)管理器的時(shí)候,可以指定一個(gè)defaultTimeout屬性,單位是秒,指定所有事務(wù)的默認(rèn)超時(shí)時(shí)間。
在 spring 中,通過(guò)在 @Transactional 注解上針對(duì)某個(gè)事務(wù),指定超時(shí)時(shí)間,如:
@Transactional(timeout = 3)
transaction timeout的實(shí)現(xiàn)原理可以用以下流程進(jìn)行描述,假設(shè)事務(wù)超時(shí)為5秒,需要執(zhí)行3個(gè)sql:
?start transaction ? ?#事務(wù)超時(shí)為5秒 ? ?sql1 ? ?#statement timeout設(shè)置為5秒 ?#執(zhí)行耗時(shí)1s,那么整個(gè)事務(wù)超時(shí)還剩4秒 ? ?sql2 ?#設(shè)置statement timeout設(shè)置為4秒 ?#執(zhí)行耗時(shí)2秒,整個(gè)事務(wù)超時(shí)還是2秒 ? ?sql3 ? ?#設(shè)置statement timeout設(shè)置為2秒 ?#假設(shè)執(zhí)行耗時(shí)超過(guò)2s,那么整個(gè)事務(wù)超時(shí),拋出異常 ?
這里只是一個(gè)簡(jiǎn)化的流程,但是可以幫助我們了解spring事務(wù)超時(shí)的原理。
從這個(gè)流程中,我們可以看到,spring事務(wù)的超時(shí)機(jī)制,實(shí)際上是還是通過(guò)Statement.setQueryTimeout進(jìn)行設(shè)置,每次都是把當(dāng)前事務(wù)的剩余時(shí)間,設(shè)置到下一個(gè)要執(zhí)行的sql中。
事實(shí)上,spring的事務(wù)超時(shí)機(jī)制,需要ORM框架進(jìn)行支持,例如mybatis-spring提供了一個(gè)SpringManagedTransaction,里面有一個(gè)getTimeout方法,就是通過(guò)從spring中獲取事務(wù)的剩余時(shí)間。
當(dāng)拋出異常后,調(diào)用 conn.isClosed() 返回 false。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MySQL創(chuàng)建和刪除表操作命令實(shí)例講解
這篇文章主要介紹了MySQL創(chuàng)建和刪除表操作命令實(shí)例講解,本文講解了創(chuàng)建表、創(chuàng)建臨時(shí)表、查看已經(jīng)創(chuàng)建的mysql表等內(nèi)容,需要的朋友可以參考下2014-12-12MySQL約束和事務(wù)知識(shí)點(diǎn)詳細(xì)歸納
在關(guān)系型數(shù)據(jù)庫(kù)中,事務(wù)的重要性不言而喻,只要對(duì)數(shù)據(jù)庫(kù)稍有了解的人都知道事務(wù),下面這篇文章主要給大家介紹了關(guān)于MySQL約束和事務(wù)知識(shí)點(diǎn)歸納的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04MySQL去重中distinct和group?by的區(qū)別淺析
今天無(wú)意中聽(tīng)到有同事在討論,distinct和group by有什么區(qū)別,下面這篇文章主要給大家介紹了關(guān)于MySQL去重中distinct和group?by區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-11-11MySQL外鍵級(jí)聯(lián)的實(shí)現(xiàn)
本文主要介紹了MySQL外鍵級(jí)聯(lián)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07MySQL通過(guò)show status查看、explain分析優(yōu)化數(shù)據(jù)庫(kù)性能
這篇文章介紹了MySQL通過(guò)show status查看、explain分析優(yōu)化數(shù)據(jù)庫(kù)性能的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04