[问题解决]MySQL8.x Unknown system variable 'tx_isolation'

1,731 阅读1分钟

1. 问题描述:

在学习Servlet过程中,由于使用了Druid数据库连接池和MySQL,在运行的过程中控制台报错Unknown system variable 'tx_isolation',花了很多时间在网上找解决办法,升级mysql-connector-java驱动jar包和升级druidjar包都尝试了,结果都无法解决问题,最终通过修改druid配置文件druid.properties中的驱动类名driverClassName解决了问题。

image.png

2. 问题原因:

1、 安装的数据库版本是8.xmysql-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_isolation and tx_read_only have been replaced with transaction_isolation and transaction_read_only in 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…
  1. druidjar包版本太旧,需要升级
  2. 升级mysql-connector-java驱动jar包后未修改配置中的driverClassName

3. 解决办法

  1. mysql-connector-java驱动jar包,推荐jar包选择与安装的MySQL版本差不多,可以低一些
    最新下载地址:jar-download.com/artifacts/m…

  2. 升级druidjar包
    最新下载地址:github.com/alibaba/dru…

  3. 修改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包版本参考如下

image.png

4. 其他注意点

  • 使用jsp或者Servlet,加载页面时,需要注意编码问题,如果页面编码和MySQL编码不一致,后端在接收前端传来的数据,并从数据库获取数据进行对比时,涉及中问容易出现对比失败的情况