- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- 解決Druid動(dòng)態(tài)數據源配置重復刷錯誤日志的問(wèn)題
Druid動(dòng)態(tài)數據源配置 主要是繼承AbstractRoutingDataSource再通過(guò)AOP來(lái)實(shí)現動(dòng)態(tài)數據源切換.
下面給大家介紹Druid動(dòng)態(tài)配置數據源重復刷錯誤日志問(wèn)題,具體內容如下所示:
功能需求:
使用druid數據庫連接池實(shí)現 動(dòng)態(tài)的配置數據源功能:IP、端口、用戶(hù)名、密碼都是用戶(hù)頁(yè)面手動(dòng)輸入,可以測試連接,保存數據源。
問(wèn)題說(shuō)明:
既然是用戶(hù)自己配置的數據源,就無(wú)法避免輸入錯誤,連接失敗等情況。
預期情況:用戶(hù)輸入的配置錯誤,測試連接時(shí),會(huì )返回連接失敗的信息。
實(shí)際情況:數據源測試連接,連接失敗后:
后臺一直打印錯誤信息,一直自動(dòng)重連
方法被阻塞無(wú)返回信息,導致前端頁(yè)面一直處于等待狀態(tài)
【錯誤信息】:
com.alibaba.druid.pool.DruidDataSource-create connection SQLException, , errorCode 0, state 08S01
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
【原始代碼】:
public static void getDataSource(DataConfig dataConfig) throws Exception{ try { Properties properties = new Properties(); properties.setProperty("driverClassName",dataConfig.getDriverClassName()); properties.setProperty("url",dataConfig.getUrl()); properties.setProperty("username",dataConfig.getUserName()); properties.setProperty("password",dataConfig.getPassWord()); DataSource ds = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } }
【參數說(shuō)明】
public static void getDataSource(DataConfig dataConfig) throws Exception{ try { Properties properties = new Properties(); properties.setProperty("driverClassName",dataConfig.getDriverClassName()); properties.setProperty("url",dataConfig.getUrl()); properties.setProperty("username",dataConfig.getUserName()); properties.setProperty("password",dataConfig.getPassWord()); properties.setProperty("maxWait","500");//如果失敗,當前的請求可以返回 DruidDataSource druidDataSource = (DruidDataSource)DruidDataSourceFactory.createDataSource(properties); druidDataSource.setConnectionErrorRetryAttempts(0);// 失敗后重連的次數 druidDataSource.setBreakAfterAcquireFailure(true);//請求失敗之后中斷 DataSource ds = druidDataSource;//如果有需要使用javax.sql.DataSource的話(huà) } catch (Exception e) { e.printStackTrace(); } }
不要在properties中配置connectionErrorRetryAttempts和breakAfterAcquireFailure,沒(méi)有效果
連接失敗的具體錯誤信息,catch不到,源碼中已經(jīng)catch了異常信息,做了相關(guān)處理
到此這篇關(guān)于解決Druid動(dòng)態(tài)數據源配置重復刷錯誤日志的問(wèn)題的文章就介紹到這了,更多相關(guān)Druid動(dòng)態(tài)數據源配置內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站