今天在测试环境出现了个批量更新的问题。以前都好好的,为啥就出现了该问题呢?莫名其妙。
问题
我打开日志看到以下内容。
Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3ef1acda]
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@4b37f2d3] will be managed by Spring
==> Preparing: update xd_sms_record a SET a.phone_number=?, a.send_status=?, a.send_date=DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s'), a.failure_count=? WHERE a.id=? ; update xd_sms_record a SET a.phone_number=?, a.send_status=?, a.send_date=DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s'), a.failure_count=? WHERE a.id=? ; update xd_sms_record a SET a.phone_number=?, a.send_status=?, a.send_date=DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s'), a.failure_count=? WHERE a.id=? ; update xd_sms_record a SET a.phone_number=?, a.send_status=?, a.send_date=DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s'), a.failure_count=? WHERE a.id=? ; update xd_sms_record a SET a.phone_number=?, a.send_status=?, a.send_date=DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s'), a.failure_count=? WHERE a.id=?
==> Parameters: Fea9WtG8vKiuVccjg6wVyQ==(String), DXFSZT_ERR(String), 1(Integer), eb9eeabe98d811f4bc6b0050569cda6c(String), Fea9WtG8vKiuVccjg6wVyQ==(String), DXFSZT_ERR(String), 1(Integer), eb9f0f9398d811f4bc6b0050569cda6c(String), Fea9WtG8vKiuVccjg6wVyQ==(String), DXFSZT_ERR(String), 1(Integer), eba0535998d811f4bc6b0050569cda6c(String), Fea9WtG8vKiuVccjg6wVyQ==(String), DXFSZT_ERR(String), 1(Integer), eba06f2498d811f4bc6b0050569cda6c(String), Fea9WtG8vKiuVccjg6wVyQ==(String), DXFSZT_ERR(String), 1(Integer), eba07f6098d811f4bc6b0050569cda6c(String)
2029-04-19 10:50:01.973 [async-service-3] ERROR druid.sql.Statement - {conn-10003, pstmt-20013} execute error. update xd_sms_record a
SET a.phone_number=?,
a.send_status=?,
a.send_date=DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s'),
a.failure_count=?
WHERE a.id=?
;
update xd_sms_record a
SET a.phone_number=?,
a.send_status=?,
a.send_date=DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s'),
a.failure_count=?
WHERE a.id=?
;
update xd_sms_record a
SET a.phone_number=?,
a.send_status=?,
a.send_date=DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s'),
a.failure_count=?
WHERE a.id=?
;
update xd_sms_record a
SET a.phone_number=?,
a.send_status=?,
a.send_date=DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s'),
a.failure_count=?
WHERE a.id=?
;
update xd_sms_record a
SET a.phone_number=?,
a.send_status=?,
a.send_date=DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s'),
a.failure_count=?
WHERE a.id=?
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update xd_sms_record a
SET a.phone_number='Fea9WtG8vKiuVccjg6wVyQ==' at line 8
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:974)
at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:391)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3409)
解决思路
SQL语法错误
最先开始以为是SQL语法错误,但是当我把SQL语句拼接起来,在navicat运行时,就会发现该语句并没有问题。
远程调试查看问题
在navicat下执行没有问题,接下来我通过远程连测试服务器调试,在启动脚本中增加以下内容。
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5006
增加后变为:
java -jar -Dspring.profiles.active=sit -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5006 $JAR_NAME > ./logs/log-$time.out &
先修改启动配置,重启本地服务,链接到远程。
再重启远程服务,打断点调试发现还是报相同的批量更新错误。当你走投无路时,就应该想起度娘。
解决方案
一直报错提示"You have an error in your SQL syntax"
检查多次确认SQL没写错,查资料后发现是连接字符串问题
如果需要执行批量更新,必须在连接字符串加上allowMultiQueries=true
如下所示:
spring:
datasource:
url: jdbc:mysql://10.0.xx.xx:3306/credit?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8&allowMultiQueries=true
username: ENC(uPY7kuB/4vZw==)
password: ENC(ni/oUY0Q==)