修复DruidDataSource close后CreateConnectionThread没有退出的问题 #3881
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
问题描述:
mysql主从,当主库hang住以后获取链接超时,此时进行主从切换,调用DruidDataSource的close方法并重新创建新的DruidDataSource。新的DruidDataSource正确链接了正确的数据库,但是原来的DruidDataSource中CreateConnectionThread并没有退出,还在持续尝试创建链接,进而报错。
分析:
DruidDataSource中的CreateConnectionThread和DestroyConnectionThread是成对出现的,调用close方法会采用中断方式关闭它们。而目前观测到的情况是DestroyConnectionThread退出,但是CreateConnectionThread没有。对比二者逻辑,DestroyConnectionThread包含Thread.isInterrupted检测,但是CreateConnectionThread中调用createPhysicalConnection方法时没有处理interruptedException。mysql hang住时,超时时间一般都是秒级别的,导致close时基本100%停留在此处,故catch了SQLException后没有退出。
变更:
CreateConnectionThread中增加closed closing判断,如果觉得不妥,也可以使用Thread.isInterrupted
类似问题 #2291