概述
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'criticalService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'criticalDao' defined in URL [jar:file:/opt/ylbs/bigdata-his-api/bigdata-his-api-1.0.jar!/com/unionx/api/dao/CriticalDao.class]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [jar:file:/opt/ylbs/bigdata-his-api/bigdata-his-api-1.0.jar!/ApplicationContext-mysql.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in URL [jar:file:/opt/ylbs/bigdata-his-api/bigdata-his-api-1.0.jar!/ApplicationContext-mysql.xml]: Invocation of init method failed; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'criticalDao' defined in URL [jar:file:/opt/ylbs/bigdata-his-api/bigdata-his-api-1.0.jar!/com/unionx/api/dao/CriticalDao.class]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [jar:file:/opt/ylbs/bigdata-his-api/bigdata-his-api-1.0.jar!/ApplicationContext-mysql.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in URL [jar:file:/opt/ylbs/bigdata-his-api/bigdata-his-api-1.0.jar!/ApplicationContext-mysql.xml]: Invocation of init method failed; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [jar:file:/opt/ylbs/bigdata-his-api/bigdata-his-api-1.0.jar!/ApplicationContext-mysql.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in URL [jar:file:/opt/ylbs/bigdata-his-api/bigdata-his-api-1.0.jar!/ApplicationContext-mysql.xml]: Invocation of init method failed; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in URL [jar:file:/opt/ylbs/bigdata-his-api/bigdata-his-api-1.0.jar!/ApplicationContext-mysql.xml]: Invocation of init method failed; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Caused by: 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.
Caused by: java.net.ConnectException: 连接超时
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
原因分析1
当数据库重启或数据库空闲连接超过设置的最大timemout时间,数据库会强行断开已有的链接,最大timeout时间可以通过命令show global variables like "wait_timeout";
查询:
mysql> show global variables like "wait_timeout";
+---------------+-------+
| VARIABLE_NAME | VALUE |
+---------------+-------+
| wait_timeout | 28800 |
+---------------+-------+
1 row in set (0.00 sec)
解决办法1
为了解决这个异常,我们在配置数据库连接池的时候需要做一些检查连接有效性的配置,这里以Druid为例,相关配置如下(更多配置):
| 字段名 | 默认值 | 说明 |
| ----------------------------- | ----------- | ---------------------------------------- |
| validationQuery | | 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。 |
| validationQueryTimeout | | 单位:秒,检测连接是否有效的超时时间。底层调用jdbc Statement对象的void setQueryTimeout(int seconds)方法 |
| testOnBorrow | true | 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 |
| testOnReturn | false | 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 |
| testWhileIdle | false | 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 |
| timeBetweenEvictionRunsMillis | 1分钟(1.0.14) | 有两个含义:1) Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明 |
为了避免空闲时间过长超过最大空闲时间而被断开,我们设置三个配置:
validationQuery: SELECT 1
testWhileIdle: true
timeBetweenEvictionRunsMillis: 28000
其中timeBetweenEvictionRunsMillis
需要小于mysql的wait_timeout
。
但是这种方法无法避免重启的情况,不过一般数据库不会频繁重启,影响不大,如果非得频繁重启,可以通过设置testOnBorrow
,即申请连接的时候先试一试连接是否可用,不过带来的影响就是性能降低,需要根据实际需求合理取舍。
原因分析2
是由mysql5数据库的配置引起的。mysql5将其连接的等待时间(wait_timeout)缺省为8小时。在其客户程序中可以这样来查看其值:mysql﹥
mysql﹥ show global variables like 'wait_timeout';
+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| wait_timeout | 28800 |
+---------------+---------+
1 row in set (0.00 sec)
28800 seconds,也就是8小时。
如果在wait_timeout秒期间内,数据库连接(java.sql.Connection)一直处于等待状态,mysql5就将该连接关闭。这时,你的Java应用的连接池仍然合法地持有该连接的引用。当用该连接来进行数据库操作时,就碰到上述错误。这解释了为什么我的程序第二天不能登录 的问题。
你可能会想到在tomcat的数据源配置中有没有办法解决?的确,在jdbc连接url的配置中,你可以附上“autoReconnect=true”,但这仅对mysql5以前的版本起作用。增加“validation query”似乎也无济于事。
本人觉得最简单的办法,就是对症下药:既然问题是由mysql5的全局变量wait_timeout的缺省值太小引起的,我们将其改大就好了。
解决办法2
查看mysql5的手册,发现对wait_timeout的最大值分别是24天/365天(windows/linux)。以windows为 例,假设我们要将其设为21天,我们只要修改mysql5的配置文件“my.ini”(mysql5 installation dir),增加一行:wait_timeout=1814400
需要重新启动mysql5。
linux系统配置文件:/etc/my.cnf
测试显示问题解决了。
也可以直接设置
mysql修改wait_timeout
mysql mysql> show global variables like 'wait_timeout';
其默认值为8小时
mysql的一个connection空闲时间超过8小时,mysql会自动断开该连接。
1.修改配置
vi /etc/my.cnf
[mysqld] wait_timeout=10
# /etc/init.d/mysql restart
2.直接用sql命令行修改 mysql> set global wait_timeout=604800;
除了wait_timeout,还有一个'interactive_timeout'
同样可以执行SHOW GLOBAL VARIABLES LIKE 'interactive_timeout'来查询
执行set global interactive_timeout=604800;来设置
3.重启 service mysqld restart
原因分析3
Error creating bean with name XXX
解决办法3
最后
以上就是幸福航空为你收集整理的Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure的全部内容,希望文章能够帮你解决Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复