linux学习路之7——数据库连接异常可能是jdbc配置文件少了jdbc前缀

异常信息

### 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前缀!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值