乐趣区

关于shardingsphere:shardingsphere500beta-SQL异常不打印的问题

最近因为业务须要引入了 shardingsphere 框架

<!--shardingsphere-->
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core</artifactId>
    <version>5.0.0-beta</version>
</dependency>

引入之后发现 SQL 谬误日志都不打印了,例如执行 MyBatis insert 语句:
int result = mapper.batchInsert(entity)
因为 entity 不满足入库要求,有字段未设置值,失常会打印异样:

java.sql.SQLException: Field 'from' doesn't have a default value

然而引入 shardingsphere 框架后,mapper.batchInsert(entity) 失常返回了 -1,没有工作异样日志, 于是 DEBUG 找到产生异样的地位,定位到一下代码:

...
at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement$2.executeSQL(ShardingSpherePreparedStatement.java:303)
at org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback.execute(JDBCExecutorCallback.java:85)
...


if (saneResult.isPresent()) { // if 始终为 true, 因为 saneResult 必定存在值 false 或 true
     return saneResult.get();}
// 后边的异样日志输入始终没法执行
sqlExecutionHook.finishFailure(ex);
SQLExecutorExceptionHandler.handleException(ex);
return null;

解决方案

降级到 5.0.0

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core</artifactId>
    <version>5.0.0</version>
 </dependency>

ShardingSpherePreparedStatement 类中曾经做了批改:

protected Optional<Integer> getSaneResult(SQLStatement sqlStatement) {return Optional.empty(); // 这里返回 saneResult.isPresent() 始终为 false}



附 debug 进去的残缺的异样堆栈 (5.0.0-beta 版本):

java.sql.SQLException: Field 'from' doesn't have a default value
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    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:953)
    at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:370)
    at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:497)
    at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement$2.executeSQL(ShardingSpherePreparedStatement.java:307)
    at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement$2.executeSQL(ShardingSpherePreparedStatement.java:303)
    at org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback.execute(JDBCExecutorCallback.java:85)
    at org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback.execute(JDBCExecutorCallback.java:64)
    at org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine.syncExecute(ExecutorEngine.java:101)
    at org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine.parallelExecute(ExecutorEngine.java:97)
    at org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine.execute(ExecutorEngine.java:82)
    at org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor.execute(JDBCExecutor.java:65)
    at org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor.execute(JDBCExecutor.java:49)
    at org.apache.shardingsphere.driver.executor.JDBCLockEngine.doExecute(JDBCLockEngine.java:114)
    at org.apache.shardingsphere.driver.executor.JDBCLockEngine.execute(JDBCLockEngine.java:91)
    at org.apache.shardingsphere.driver.executor.DriverJDBCExecutor.execute(DriverJDBCExecutor.java:122)
    at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.execute(ShardingSpherePreparedStatement.java:283)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:47)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
    at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
    at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
    at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184)
    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.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426)
    at com.sun.proxy.$Proxy106.insert(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:271)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:62)
    at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:152)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
    at com.sun.proxy.$Proxy216.batchInsert(Unknown Source)
    at com.facebook.common.service.BaseService.batchInsert(BaseService.java:159)
    at com.facebook.common.service.BaseService$$FastClassBySpringCGLIB$$13311683.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
    at com.hw.spy.task.services.AdsLibKeywordTaskAdsService$$EnhancerBySpringCGLIB$$23d1258a.batchInsert(<generated>)
    at com.hw.spy.task.services.AdsLibKeywordTaskAdsSaveService.createTaskByLevels(AdsLibKeywordTaskAdsSaveService.java:114)
    at com.hw.spy.task.services.AdsLibKeywordTaskAdsSaveService.lambda$createTask$0(AdsLibKeywordTaskAdsSaveService.java:62)
    at com.facebook.common.utils.log.MDCHelper.tagCallback(MDCHelper.java:114)
    at com.facebook.common.utils.log.MDCHelper.callback(MDCHelper.java:97)
    at com.hw.spy.task.services.AdsLibKeywordTaskAdsSaveService.createTask(AdsLibKeywordTaskAdsSaveService.java:45)
    at com.hw.spy.task.services.AdsLibKeywordTaskAdsService.loadTask(AdsLibKeywordTaskAdsService.java:96)
    at com.hw.spy.common.service.queue.RedisQueueComponent.lambda$loadDataToQueue$0(RedisQueueComponent.java:77)
    at com.hw.spy.common.service.queue.OnceExecutionService.run(OnceExecutionService.java:150)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
    at java.util.concurrent.FutureTask.run(FutureTask.java)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at com.facebook.common.utils.NamedThreadFactory.lambda$newThread$0(NamedThreadFactory.java:31)
    at java.lang.Thread.run(Thread.java:748)
退出移动版