问题:
在使用springboot时mybaits链接数据库,出现了报错:
2022-04-06 15:14:13.250 ERROR 1404 --- [io-10001-exec-1] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
com.mysql.cj.jdbc.exceptions.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.
### The error may exist in com/lizhenxin/gulimarket/product/dao/CategoryDao.java (best guess)
### The error may involve com.lizhenxin.gulimarket.product.dao.CategoryDao.selectList
### The error occurred while executing a query
## Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
查找问题:
1.首先查看mysql数据库是否异常,于是使用navicat链接数据库,结果正常,再使用本地数据库替换,结果正常。
2.于是查看数据库配置,为url配置后缀:
spring: datasource: username: root password: root url: jdbc:mysql://114.55.116.159:3306/gulimail_pms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=true driver-class-name: com.mysql.cj.jdbc.Driver
3.以上操作无用,我想到可能是版本问题,于是查看了mybatisplus和springboot版本:
4.在版本确认无误之后,我觉得是mvn的缓存导致的,于是我执行了以下操作:
1. reload项目maven。
2.清空idea缓存。
3.找到maven库重新加载对应的包。
5.以上操作无用之后,我想可能与idea有关系,于是使用了vscode和打包成jar包使用命令行的形式启动服务。
神奇的是使用jar包的形式使用没有问题。
于是我对连接池进行了更换,换成了duird的:
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.13</version> </dependency>
配置:
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.sql.SQLException; import javax.sql.DataSource; @Configuration public class DataConfig { @Bean("dataSource") public DataSource dataSource() throws SQLException { DruidDataSource dataSource = new DruidDataSource(); // 数据库连接URL dataSource.setUrl("jdbc:mysql://114.55.116.159:3306/gulimail_pms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=true"); dataSource.setUsername("root"); // 数据库用户名 dataSource.setPassword("root"); // 用户密码 dataSource.setInitialSize(3); // 初始化时建立物理连接的个数 dataSource.setMaxActive(20); // 最大连接池数量 dataSource.setMinIdle(2); // 最小连接池数量 dataSource.setMaxWait(2000); // 获取连接时最大等待时间,单位毫秒 // 是否缓存preparedStatement dataSource.setPoolPreparedStatements(true); dataSource.setMaxOpenPreparedStatements(100); // 监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall dataSource.setFilters("stat,wall"); return dataSource; } }
这里报错变成了连续性质的,并且出现了数据库链接码:state 08S01
2022-04-06 16:13:36.772 ERROR 13180 --- [reate-767750806] com.alibaba.druid.pool.DruidDataSource :
create connection SQLException, url: jdbc:mysql://114.55.116.159:3306/gulimail_pms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=true, errorCode 0, state 08S01 com.mysql.cj.jdbc.exceptions.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.
最后,参考了博客:https://blog.csdn.net/sunzxhqq/article/details/116458586
其中useSSL在高版本中需要指定值,否则,就会出错。于是修改url:
url: jdbc:mysql://114.55.116.159:3306/gulimail_pms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
这样即使不使用德鲁伊连接池也可以连接了。
在一开始的报错中也提到了ssl的异常:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.] with root cause javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate) at sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:171) ~[na:1.8.0_311] at sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:106) ~[na:1.8.0_311] at sun.security.ssl.TransportContext.kickstart(TransportContext.java:238) ~[na:1.8.0_311] at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:394) ~[na:1.8.0_311] at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:373) ~[na:1.8.0_311]
疑问:
为什么转用jar包运行的程序没有这个问题?