开发者社区> 问答> 正文

Druid获得连接把程序挂死了,求解答!?报错

@wenshao 你好,遇到druid 获取连接循环重试的问题。

自己做测试,忘了启动mysqld,因此在初始化dataSource的时候报错:

[DEBUG]22:23:50.451[defaultEventExecutorGroup-5-1][o.s.j.datasource.DataSourceUtils] -- Fetching JDBC Connection from DataSource
[ERROR]22:23:52.892[defaultEventExecutorGroup-5-1][c.a.druid.pool.DruidDataSource] -- init datasource error, url: jdbc:mysql://localhost/test
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_25]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_25]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_25]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_25]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2237) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_25]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_25]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_25]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_25]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1375) ~[druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1431) ~[druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:632) [druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:934) [druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:930) [druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:102) [druid-1.0.13.jar:1.0.13]
	at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:630) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:909) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:933) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:313) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:318) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at com.sitech.crmpd.idmm2.broker.repository.MessageRepositoryJdbcImpl.save(MessageRepositoryJdbcImpl.java:71) ~[classes/:na]
	at com.sitech.crmpd.idmm2.broker.repository.MessageRepositoryJdbcImpl.save(MessageRepositoryJdbcImpl.java:1) ~[classes/:na]
	at com.sitech.crmpd.idmm2.broker.handler.SendMessageHandler.handleAndAnswer(SendMessageHandler.java:62) ~[classes/:na]
	at com.sitech.crmpd.idmm2.broker.handler.MessageHandler.handle(MessageHandler.java:45) ~[classes/:na]
	at com.sitech.crmpd.idmm2.broker.LogicHandler.channelRead(LogicHandler.java:55) ~[classes/:na]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:339) ~[netty-transport-4.0.27.Final.jar:4.0.27.Final]
	at io.netty.channel.AbstractChannelHandlerContext.access$600(AbstractChannelHandlerContext.java:32) ~[netty-transport-4.0.27.Final.jar:4.0.27.Final]
	at io.netty.channel.AbstractChannelHandlerContext$7.run(AbstractChannelHandlerContext.java:329) ~[netty-transport-4.0.27.Final.jar:4.0.27.Final]
	at io.netty.util.concurrent.DefaultEventExecutor.run(DefaultEventExecutor.java:36) ~[netty-common-4.0.27.Final.jar:4.0.27.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111) ~[netty-common-4.0.27.Final.jar:4.0.27.Final]
	at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) ~[netty-common-4.0.27.Final.jar:4.0.27.Final]
	at java.lang.Thread.run(Thread.java:724) ~[na:1.7.0_25]
Caused by: java.net.ConnectException: Connection refused: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.7.0_25]
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) ~[na:1.7.0_25]
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) ~[na:1.7.0_25]
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) ~[na:1.7.0_25]
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) ~[na:1.7.0_25]
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.7.0_25]
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.7.0_25]
	at java.net.Socket.connect(Socket.java:579) ~[na:1.7.0_25]
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	... 37 common frames omitted
[ERROR]22:23:52.932[defaultEventExecutorGroup-5-1][c.a.druid.pool.DruidDataSource] -- dataSource init error
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_25]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_25]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_25]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_25]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2237) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_25]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_25]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_25]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_25]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1375) ~[druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1431) ~[druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:632) [druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:934) [druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:930) [druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:102) [druid-1.0.13.jar:1.0.13]
	at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:630) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:909) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:933) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:313) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:318) ~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
	at com.sitech.crmpd.idmm2.broker.repository.MessageRepositoryJdbcImpl.save(MessageRepositoryJdbcImpl.java:71) ~[classes/:na]
	at com.sitech.crmpd.idmm2.broker.repository.MessageRepositoryJdbcImpl.save(MessageRepositoryJdbcImpl.java:1) ~[classes/:na]
	at com.sitech.crmpd.idmm2.broker.handler.SendMessageHandler.handleAndAnswer(SendMessageHandler.java:62) ~[classes/:na]
	at com.sitech.crmpd.idmm2.broker.handler.MessageHandler.handle(MessageHandler.java:45) ~[classes/:na]
	at com.sitech.crmpd.idmm2.broker.LogicHandler.channelRead(LogicHandler.java:55) ~[classes/:na]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:339) ~[netty-transport-4.0.27.Final.jar:4.0.27.Final]
	at io.netty.channel.AbstractChannelHandlerContext.access$600(AbstractChannelHandlerContext.java:32) ~[netty-transport-4.0.27.Final.jar:4.0.27.Final]
	at io.netty.channel.AbstractChannelHandlerContext$7.run(AbstractChannelHandlerContext.java:329) ~[netty-transport-4.0.27.Final.jar:4.0.27.Final]
	at io.netty.util.concurrent.DefaultEventExecutor.run(DefaultEventExecutor.java:36) ~[netty-common-4.0.27.Final.jar:4.0.27.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111) ~[netty-common-4.0.27.Final.jar:4.0.27.Final]
	at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) ~[netty-common-4.0.27.Final.jar:4.0.27.Final]
	at java.lang.Thread.run(Thread.java:724) ~[na:1.7.0_25]
Caused by: java.net.ConnectException: Connection refused: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.7.0_25]
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) ~[na:1.7.0_25]
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) ~[na:1.7.0_25]
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) ~[na:1.7.0_25]
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) ~[na:1.7.0_25]
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.7.0_25]
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.7.0_25]
	at java.net.Socket.connect(Socket.java:579) ~[na:1.7.0_25]
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	... 37 common frames omitted
[ INFO]22:23:52.932[defaultEventExecutorGroup-5-1][c.a.druid.pool.DruidDataSource] -- {dataSource-1} inited
这个时候程序还能正常运行,我又模拟了一次客户端请求,然后就开始一直重试:



[DEBUG]22:29:32.295[defaultEventExecutorGroup-7-1][o.s.j.datasource.DataSourceUtils] -- Fetching JDBC Connection from DataSource
[ERROR]22:29:34.304[Druid-ConnectionPool-Create-7939182][c.a.druid.pool.DruidDataSource] -- create connection error, url: jdbc:mysql://localhost/test
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_25]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_25]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_25]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_25]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2237) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_25]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_25]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_25]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_25]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1375) ~[druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1431) ~[druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:1861) ~[druid-1.0.13.jar:1.0.13]
Caused by: java.net.ConnectException: Connection refused: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.7.0_25]
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) ~[na:1.7.0_25]
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) ~[na:1.7.0_25]
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) ~[na:1.7.0_25]
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) ~[na:1.7.0_25]
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.7.0_25]
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.7.0_25]
	at java.net.Socket.connect(Socket.java:579) ~[na:1.7.0_25]
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	... 15 common frames omitted
[ERROR]22:29:36.324[Druid-ConnectionPool-Create-7939182][c.a.druid.pool.DruidDataSource] -- create connection error, url: jdbc:mysql://localhost/test
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_25]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_25]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_25]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_25]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2237) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_25]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_25]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_25]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_25]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1375) ~[druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1431) ~[druid-1.0.13.jar:1.0.13]
	at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:1861) ~[druid-1.0.13.jar:1.0.13]
Caused by: java.net.ConnectException: Connection refused: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.7.0_25]
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) ~[na:1.7.0_25]
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) ~[na:1.7.0_25]
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) ~[na:1.7.0_25]
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) ~[na:1.7.0_25]
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.7.0_25]
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.7.0_25]
	at java.net.Socket.connect(Socket.java:579) ~[na:1.7.0_25]
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297) ~[mysql-connector-java-5.1.35.jar:5.1.35]
	... 15 common frames omitted
看了下代码,发现之所以一直循环是因为错误尝试没到下面的条件,源码位置在at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:1861) ~[druid-1.0.13.jar:1.0.13]:



errorCount++;

                    if (errorCount > connectionErrorRetryAttempts && timeBetweenConnectErrorMillis > 0) {
                        if (breakAfterAcquireFailure) {
                            break;
                        }

                        try {
                            Thread.sleep(timeBetweenConnectErrorMillis);
                        } catch (InterruptedException interruptEx) {
                            break;
                        }
                    }

没到条件,然后会continue循环这段代码。没找到 connectionErrorRetryAttempts 这个参数的说明,不知道默认会尝试多少次,试着spring中配成了1,连续输出2次同样的ERROR信息后,停止了,但是上层调用端还是没能捕捉到这个异常。

目前不希望出现数据库down掉的情况下,我们的服务端应用也不能正常响应客户端的应答。

因此我把数据库连接池换成了tomcat-jdbc后做了下测试,测试结果符合预期,获取不到连接时上层调用能捕捉到异常,从而让我们的服务端应用能正常给客户端返回。

不知道druid 如何配置才能符合预期的在没有数据库的情况下抛出创建连接失败的异常?请 @wenshao 抽空解答下

展开
收起
爱吃鱼的程序员 2020-06-14 16:56:31 1420 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    GetConnectionTimeoutException的Cause就是这个

    请问一下,这个问题已经解决了吗?

    我也查看了一下代码,发现如果创建数据库连接失败的话,通过Druid获取连接的方法会一直阻塞:

    这是我用jstack看到的:

    <preclass="brush:java;toolbar:true;auto-links:false;">"AWT-EventQueue-0"prio=6tid=0x05068c00nid=0x151cwaitingoncondition[0x056ae000]java.lang.Thread.State:WAITING(parking)atsun.misc.Unsafe.park(NativeMethod)-parkingtowaitfor<0x2a48e958>(ajava.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)atjava.util.concurrent.locks.LockSupport.park(LockSupport.java:186)atjava.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)atcom.alibaba.druid.pool.DruidDataSource.takeLast(DruidDataSource.java:1376)atcom.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:1051)atcom.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:916)atcom.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:896)atcom.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:886)atcom.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:96)atorg.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)



    <divclass='ref'>

    引用来自“losthu1998”的评论

    请问一下,这个问题已经解决了吗?

    我也查看了一下代码,发现如果创建数据库连接失败的话,通过Druid获取连接的方法会一直阻塞:

    这是我用jstack看到的:

    <preclass="brush:java;toolbar:true;auto-links:false;">"AWT-EventQueue-0"prio=6tid=0x05068c00nid=0x151cwaitingoncondition[0x056ae000]java.lang.Thread.State:WAITING(parking)atsun.misc.Unsafe.park(NativeMethod)-parkingtowaitfor<0x2a48e958>(ajava.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)atjava.util.concurrent.locks.LockSupport.park(LockSupport.java:186)atjava.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)atcom.alibaba.druid.pool.DruidDataSource.takeLast(DruidDataSource.java:1376)atcom.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:1051)atcom.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:916)atcom.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:896)atcom.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:886)atcom.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:96)atorg.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)



    谢谢,我也没什么好办法,只能换成DBCP如果数据库down掉,一般给客户端返回什么?SQLException之类的吧,你可以使用dbcp,tomcat-jdbc测试下,都可以返回异常

    我也遇到这个问题,楼主咋搞定的

    尝试了几种方案都没能解决,加之我们当时对sql监控的需求不大,后来就放弃druid改用tomcat-jdbc了。

    看这篇博客:

    <arel="nofollow">https://www.cnblogs.com/xchendevelop/articles/9291044.html

    2020-06-14 16:56:49
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载