赞
踩
关注微信公众号:CodingTechWork,一起学习进步。
在开发过程中,服务程序报错Caused by: java.sql.SQLException: Unknown system variable 'transaction_isolation'
看着sql字样,应该是数据库的问题。
遇到这种问题,我们首先要看数据库是否正常,也可以通过查看连接数据库的其他服务是否正常。其次,我们是要查看自己开发的服务程序是否连接数据库异常,如驱动版本号是否和数据库一致?是否连接配置有问题?
mysql> select version();
mysql> show variables like 't%_isolation';
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId></dependency>
点击mysql-connector-java
查看版本号:
我们也可以通过查看jar包的方式核对。
由于业务需要将Spring Boot的版本号提高到了2.x,而jdbc版本号未指定,随之带来的mysql-connector-java
版本号也跟着提高到了,导致驱动和数据库版本不匹配。
private void checkTransactionIsolationLevel() throws SQLException { String txIsolationName = null; if (this.versionMeetsMinimum(4, 0, 3)) { txIsolationName = "tx_isolation"; } else { txIsolationName = "transaction_isolation"; } String s = (String)this.serverVariables.get(txIsolationName); if (s != null) { Integer intTI = (Integer)mapTransIsolationNameToValue.get(s); if (intTI != null) { this.isolationLevel = intTI; } } }
private void checkTransactionIsolationLevel() throws SQLException {
String s = (String)this.serverVariables.get("transaction_isolation");
if (s == null) {
s = (String)this.serverVariables.get("tx_isolation");
}
if (s != null) {
Integer intTI = (Integer)mapTransIsolationNameToValue.get(s);
if (intTI != null) {
this.isolationLevel = intTI;
}
}
}
当通过数据库查看show variables like 't%_isolation';
没有出现transaction_isolation,要么升级数据库,要么降低连接数据库的服务程序jdbc版本(不超过5.1.43
)。
在数据库通用的情况下,我们一般还是更改服务的jdbc版本比较稳妥,如下方式更改依赖:
依赖代码:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
更改完依赖后,重新import一下依赖,最后打包,重启服务发现不再报错即可。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。