mysql - 如何顯式修改數(shù)據(jù)庫(kù)連接超時(shí)時(shí)間
問(wèn)題描述
spring + mybatis
redis.incr(key); //redis計(jì)數(shù)加一try{ dbOPerHere(); //數(shù)據(jù)庫(kù)操作}catch (Exception e){ //若數(shù)據(jù)庫(kù)操作失敗 減一 redis.decr(key); }
想模擬網(wǎng)路異常 數(shù)據(jù)庫(kù)連接超時(shí)異常 通過(guò)如下方式模擬數(shù)據(jù)庫(kù)連接異常
sudo iptables -A INPUT -p tcp --dport 3306 -j DROP
但發(fā)現(xiàn)要等差不多三分鐘 才會(huì)有超時(shí)異常另外一個(gè)奇怪現(xiàn)象 同樣的數(shù)據(jù)源配置 若換成測(cè)試服務(wù)器 超時(shí)時(shí)間為6分鐘 不知何故
Caused by: java.net.ConnectException: Connection timed out
如何顯式設(shè)置數(shù)據(jù)庫(kù)連接超時(shí)時(shí)間(如超時(shí)時(shí)間5秒), 不管是配置數(shù)據(jù)源 還是 修改mysql服務(wù)器的變量?使用的數(shù)據(jù)源如下
<bean destroy-method='close'>
或修改mysql服務(wù)器變量
>show variables like ’%timeout%’;+-----------------------------+----------+| Variable_name | Value |+-----------------------------+----------+| connect_timeout | 10 || delayed_insert_timeout | 300 || innodb_flush_log_at_timeout | 1|| innodb_lock_wait_timeout | 50 || innodb_rollback_on_timeout | OFF || interactive_timeout | 28800 || lock_wait_timeout | 31536000 || net_read_timeout | 30 || net_write_timeout | 60 || rpl_stop_slave_timeout | 31536000 || slave_net_timeout | 3600 || wait_timeout| 28800 |+-----------------------------+----------+
問(wèn)題解答
回答1:/** * <strong>BasicDataSource does NOT support this method. </strong> * * <p>Set the login timeout (in seconds) for connecting to the * database.</p> * <p> * Calls {@link #createDataSource()}, so has the side effect * of initializing the connection pool.</p> * * @param loginTimeout The new login timeout, or zero for no timeout * @throws UnsupportedOperationException If the DataSource implementation * does not support the login timeout feature. * @throws SQLException if a database access error occurs */ public void setLoginTimeout(int loginTimeout) throws SQLException {// This method isn’t supported by the PoolingDataSource returned by// the createDataSourcethrow new UnsupportedOperationException('Not supported by BasicDataSource');//createDataSource().setLoginTimeout(loginTimeout); }
* <strong>BasicDataSource does NOT support this method. </strong> *
以上是dbcp連接池的源代碼,不知道這個(gè)LoginTimeout()具體是做什么的,僅供參考,并期待問(wèn)題后續(xù)。
相關(guān)文章:
1. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問(wèn)題2. 關(guān)于docker下的nginx壓力測(cè)試3. docker - 如何修改運(yùn)行中容器的配置4. angular.js - angularjs的自定義過(guò)濾器如何給文字加顏色?5. docker鏡像push報(bào)錯(cuò)6. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””7. java - 如何點(diǎn)擊按鈕,重新運(yùn)行(我是初學(xué)者)?8. 前端 - @media query 使用出現(xiàn)的問(wèn)題?9. html5和Flash對(duì)抗是什么情況?10. html - css氣泡,實(shí)現(xiàn)“倒三角(不知道算不算三角了)”可透明的。

網(wǎng)公網(wǎng)安備