异常信息
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
#### The error may exist in file [/root/tomcat8/apache-tomcat-8.5.31/webapps/TimingTask_war/WEB-INF/classes/mapper/GdeDictDao.xml]
虽然日志告诉我了,是数据库连接异常,但是具体是什么原因,却没说清楚。总之就是数据库连接异常。
我用Navicat去连接数据库,却是可以的。
而且,这个项目跑在本地,也是正常的。但是部署到服务器,却会出现数据库连接异常。
百度
看到有网友这么说:
在错误中查看并无找不到mysql jar包问题,故分析认为c3p0出了问题,一直在c3p0上排错,浪费了一定的时间。最终错误定位在tomcat的lib下未有mysql-connector-java-5.1.24.jar包,添加即可
我进tomcat的lib下一看:
[root@VM_16_8_redhat lib]# ls
annotations-api.jar jasper.jar tomcat-i18n-ja.jar
catalina-ant.jar jaspic-api.jar tomcat-jdbc.jar
catalina-ha.jar jsp-api.jar tomcat-jni.jar
catalina.jar servlet-api.jar tomcat-util.jar
catalina-storeconfig.jar tomcat-api.jar tomcat-util-scan.jar
catalina-tribes.jar tomcat-coyote.jar tomcat-websocket.jar
ecj-4.6.3.jar tomcat-dbcp.jar websocket-api.jar
el-api.jar tomcat-i18n-es.jar
jasper-el.jar tomcat-i18n-fr.jar
确实是没有呢。但是我不是通过maven导入了mysql驱动了嘛:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
<scope>runtime</scope>
</dependency>
试了一下,加jar包到tomcat的lib下,还是不行。
报错信息:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!
### The error may exist in file [/root/tomcat8/apache-tomcat-8.5.31/webapps/TimingTask_war/WEB-INF/classes/mapper/GdeDictDao.xml]
### The error may involve gst.dao.GdeDictDao.queryByDictcode
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!
找到问题所在了
事实证明,不是驱动问题,mysql驱动我已经通过maven添加了依赖。
最后参考着【云南ETC】的项目,对照着jdbc.properties文件,发现,我的配置信息是这么写的:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://xxxxxxxxxxxx:3306/gst_gxcz?useUnicode=true&characterEncoding=utf8
user=xxxxxxxxxxxxxxx
password=xxxxxxxxxxx
而【云南ETC】的是这么写的:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://xxxxxxxxxxxx:3306/yunnangst?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&allowMultiQueries=true
jdbc.username=xxxxxxxxxxxxxxx
jdbc.password=xxxxxxxxxxx
也就是说我少了jdbc
前缀!!
解决方法
给jdbc.properties文件的各个属性加上前缀,再改下spring-dao.xml中的各个引用处。然后打war包,部署,启动tomcat。
结论
应该是在windows系统下,jdbc.properties文件的各个属性可以不写jdbc前缀,而linux系统下却必须加上jdbc前缀!!