1. 问题描述:
在学习Servlet过程中,由于使用了Druid数据库连接池和MySQL,在运行的过程中控制台报错Unknown system variable 'tx_isolation',花了很多时间在网上找解决办法,升级mysql-connector-java驱动jar包和升级druidjar包都尝试了,结果都无法解决问题,最终通过修改druid配置文件druid.properties中的驱动类名driverClassName解决了问题。
2. 问题原因:
1、 安装的数据库版本是8.x而mysql-connector-java驱动jar包则是5.x版本,而8.x版本的MySQL废弃了tx_isolation而改用transaction_isolation
- Important Change: Following the changes in MySQL Server 8.0.3, the system variables
tx_isolationandtx_read_onlyhave been replaced withtransaction_isolationandtransaction_read_onlyin the code of Connector/J. Users should update Connector/J to this latest release in order to connect to MySQL 8.0.3. They should also make the same adjustments to their own applications if they use the old variables in their codes. (Bug #26440544)
引用自:dev.mysql.com/doc/relnote…
druidjar包版本太旧,需要升级- 升级
mysql-connector-java驱动jar包后未修改配置中的driverClassName
3. 解决办法
-
mysql-connector-java驱动jar包,推荐jar包选择与安装的MySQL版本差不多,可以低一些
最新下载地址:jar-download.com/artifacts/m… -
升级
druidjar包
最新下载地址:github.com/alibaba/dru… -
修改
druid.properties
driverClassName=com.mysql.cj.jdbc.Driver // 注意8.x的版本使用com.mysql.jdbc.Driver仍会报错
url=jdbc:mysql:///javaee
username=root
password=123456
initialSize=5
maxActive=10
maxWait=3000
我的jar包版本参考如下
4. 其他注意点
- 使用jsp或者Servlet,加载页面时,需要注意编码问题,如果页面编码和MySQL编码不一致,后端在接收前端传来的数据,并从数据库获取数据进行对比时,涉及中问容易出现对比失败的情况