維護(hù)一個(gè)toB智能制造項(xiàng)目的時(shí)候垮兑,發(fā)現(xiàn)mysql偶爾會(huì)報(bào)錯(cuò):
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.
Cause: org.apache.ibatis.transaction.TransactionException: Error configuring AutoCommit. Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 31,056,941 milliseconds ago. The last packet sent successfully to the server was 31,056,941 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
### The error may exist in org/activiti/db/mapping/entity/Group.xml
### The error may involve org.activiti.engine.impl.persistence.entity.GroupEntityImpl.selectGroupsByUserId
### The error occurred while executing a query
這個(gè)錯(cuò)誤發(fā)生時(shí),重新調(diào)用接口即可重新連接上漱挎,倒是沒有特別大的阻礙系枪,但是偶爾跳出還是煩人。所以我花了點(diǎn)時(shí)間研究了下磕谅。
這個(gè)問題很明顯私爷,就是mysql連接斷了雾棺。
網(wǎng)上查到的解決方案無非以下幾種:
1. 按照報(bào)錯(cuò)內(nèi)容上說的,mysql連接url上需要加autoReconnect=true
試過了衬浑,但是后來發(fā)現(xiàn)捌浩,這個(gè)參數(shù)僅針對mysql4.x有效果,現(xiàn)如今的mysql穩(wěn)定版大多是5.7工秩,還有很多同行直接使用mysql8.x尸饺,所以這個(gè)方法沒有效果。
2. 添加連接池配置
這個(gè)都不用試助币,標(biāo)準(zhǔn)的spring項(xiàng)目誰會(huì)沒事只開一個(gè)連接??浪听,所以也沒啥用。
3. 如果是本地?cái)?shù)據(jù)庫眉菱,把ip換成localhost
這個(gè)默認(rèn)就是換了的迹栓,因?yàn)橥饩W(wǎng)ip不穩(wěn)定,自己內(nèi)部鏈接還少些資源俭缓。但是并不能從根本性上解決這個(gè)問題克伊。
4. 直接換掉連接池
這是我現(xiàn)在使用的方法。等我測試幾天华坦,再回來在評論區(qū)告訴大家答案愿吹。原理很簡單,spring的連接池默認(rèn)走的是dbcp惜姐,據(jù)說這個(gè)玩意內(nèi)部有硬傷犁跪,長期空閑連接mysql是會(huì)有問題的,所以換成阿里的druid試一試载弄。
還有一點(diǎn)需要各位明白的是耘拇,連接池配置是全局有效的,除非你引用的第三方包自己寫死了宇攻,但是一般正常的第三方包或者開源包這些配置都是跟著主系統(tǒng)的惫叛,或者說主系統(tǒng)的配置是能夠覆蓋的。我這個(gè)系統(tǒng)使用了activiti逞刷,作為工作流引擎嘉涌,引入其他的連接池也沒啥問題,照常使用夸浅。
綜上仑最,等結(jié)果吧~
在此感謝這篇博客:
https://blog.csdn.net/qq_27471405/article/details/80921846
它總結(jié)的很全了。