druid connection-init-sqls 配置中如果有逗号造成SQL执行失败的问题,

lqfhib0f  于 2021-11-27  发布在  Java
关注(0)|答案(0)|浏览(1020)

spring boot 版本 1.5.9
druid-spring-boot-starter 版本 1.1.21
jdk 版本 :
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

因业务要求,但是数据库配置不能修改,所以需要在初始化连接时修改一下当前连接的sql_mode配置
具体配置:

此时启动程序会有报错,经调试发现如上的配置会被拆分为三条sql

  • set @@session.sql_mode='STRICT_TRANS_TABLES
  • NO_AUTO_CREATE_USER
  • NO_ENGINE_SUBSTITUTION

具体原因是在初始化数据库时对于connection-init-sqls这个配置被org.springframework.util.StringUtils的commaDelimitedListToStringArray(String str)这个方法按逗号拆分为了List。
如下图

报错信息:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'NO_AUTO_CREATE_USER' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:942)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2503)
at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:839)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:739)
at com.alibaba.druid.filter.FilterChainImpl.statement_execute(FilterChainImpl.java:3010)
at com.alibaba.druid.filter.FilterAdapter.statement_execute(FilterAdapter.java:2484)
at com.alibaba.druid.filter.FilterChainImpl.statement_execute(FilterChainImpl.java:3008)
at com.alibaba.druid.proxy.jdbc.StatementProxyImpl.execute(StatementProxyImpl.java:147)
at com.alibaba.druid.pool.DruidAbstractDataSource.initPhysicalConnection(DruidAbstractDataSource.java:1842)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1717)
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:939)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1758)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1695)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 118 common frames omitted

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题