java.sql.SQLException: interrupt Druid
今天看線上一直報(bào) java.sql.SQLException: interrupt
因?yàn)榈谝淮斡龅揭苍诰W(wǎng)上找了一些資料,其中網(wǎng)上的大部分內(nèi)容如下:
- 我也遇到了這個(gè)問(wèn)題,您這邊解決了嗎缩膝? 我這邊是雙數(shù)據(jù)源攀痊,我懷疑是多數(shù)據(jù)源導(dǎo)致的
- 這個(gè)錯(cuò)誤是什么引起的,我使用ThreadPoolExecutor運(yùn)行多線程棺滞,調(diào)用shutdownNow關(guān)閉線程的時(shí)候出現(xiàn)的
- 先調(diào)用了 cacel 方法裁蚁,再調(diào)用查詢方法導(dǎo)致
- 因?yàn)樗接性凭W(wǎng)卡和阿里云的不匹配導(dǎo)致了 只要是請(qǐng)求超過(guò)1k的就會(huì)響應(yīng)不了,能連接上數(shù)據(jù)庫(kù)但是沒(méi)有響應(yīng)
- 我也碰到了類似的問(wèn)題继准,在中斷某一個(gè)線程后枉证,我緊跟著需要執(zhí)行的數(shù)據(jù)庫(kù)操作并沒(méi)有執(zhí)行。報(bào)錯(cuò)為 Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: interrupt
異常具體內(nèi)容如下:
2021-05-10 at 16:25:05.262 CST ERROR java.sql.SQLException: interrupt
at com.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:1201) ~[skyrim.engine.jar:?]
at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:1086) ~[skyrim.engine.jar:?]
at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:4544) ~[skyrim.engine.jar:?]
at com.alibaba.druid.filter.stat.StatFilter.dataSource_getConnection(StatFilter.java:670) ~[skyrim.engine.jar:?]
查看具體的錯(cuò)誤位置代碼為:
private DruidPooledConnection getConnectionInternal(long maxWait) throws SQLException {
...
try {
lock.lockInterruptibly();
} catch (InterruptedException e) {
connectErrorCount.incrementAndGet();
throw new SQLException("interrupt", e);
}
...
其中 lock 為 ReentrantLock 也就是當(dāng)獲取連接時(shí)移必,如果線程被中斷就會(huì)拋出 interrupt
在我們的項(xiàng)目代碼中室谚,為了控制響應(yīng)時(shí)間使用了
List<Callable<Record>> callableList = new ArrayList<>();
...
List<Future<Record>> futures =
executorService.invokeAll(callableList, 300, TimeUnit.MILLISECONDS);
其中當(dāng) 300 毫秒內(nèi) callableList 還沒(méi)有執(zhí)行完成,就會(huì)到線程進(jìn)行中斷崔泵,導(dǎo)致 Druid 在 getConnectionInternal 響應(yīng)線程中斷秒赤,拋出異常。