1. 程式人生 > >org.springframework.jdbc.BadSqlGrammarException

org.springframework.jdbc.BadSqlGrammarException

【SQL問題】SQLSyntaxErrorException

故障解決方式

在DaoImpl層加上@DataSource註釋

@DataSource(DatasourceConstant.PAYMENT)

故障環境介紹

專案 描述
資料庫型別 mysql
框架 spring boot
問題範圍 多資料來源問題

故障發生現象及報錯資訊

org.springframework.jdbc.BadSqlGrammarException:
### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: Table 'credittrans.tp_trade_order' doesn't exist
### The error may exist in file [D:\develophangying\hahu\evidence\target\classes\mybatis\mapper\payment\apper.xml]
### The error may involve com.hf.innet.cot.evice.maper.payment.TpTreOrderMapper.selectByExample-Inline
### The error occurred while setting parameters
### SQL: select                        bank_id, biz_type, biz_value, biz_ext_data, amount, actual_amount, source_type, channel,      uuid, start_time, current_action,   resp_code, resp_desc, cnaps_msgid         from tp_trade_order                         WHERE (  loan_order_id = ?                                                                and source_type = ? )
### Cause: java.sql.SQLSyntaxErrorException: Table 'credittrans.tp_trade_order' doesn't exist
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Table 'credittrans.tp_trade_order' doesn't exist
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:234)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
    at com.sun.proxy.$Proxy84.selectList(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
    at com.sun.proxy.$Proxy93.selectByExample(Unknown Source)
    at com.hyxf.internet.court.evidence.dal.dao.payment.impl.TpTradeOrderDaoImpl.findLoanSerialByLoanInvoiceId(TpTradeOrderDaoImpl.java:43)
    at com.hyxf.internet.court.evidence.dal.dao.payment.impl.TpTradeOrderDaoImpl$$FastClassBySpringCGLIB$$9c693bea.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at com.hyxf.internet.court.evidence.dal.dao.payment.impl.TpTradeOrderDaoImpl$$EnhancerBySpringCGLIB$$be08bf81.findLoanSerialByLoanInvoiceId(<generated>)
    at com.hyxf.internet.court.evidence.service.product.EnjoyLoanService.createLoanSerial(EnjoyLoanService.java:292)
    at com.hyxf.internet.court.evidence.service.AbstractTemplateProduct$ParallelTask$6.compute(AbstractTemplateProduct.java:182)
    at com.hyxf.internet.court.evidence.service.AbstractTemplateProduct$ParallelTask$6.compute(AbstractTemplateProduct.java:178)
    at java.util.concurrent.RecursiveTask.exec(RecursiveTask.java:94)
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: java.sql.SQLSyntaxErrorException: Table 'credittrans.tp_trade_order' doesn't exist
    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:955)
    at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:372)
    at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3051)
    at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
    at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3049)
    at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:167)
    at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:498)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

故障分析及解決過程

問題的關鍵是:"Table 'credittrans.tp_trade_order' doesn't exist" ,查詢資料庫會發現,[credittrans]庫下面沒有[tp_trade_order]這張表,從而排查到是資料來源配置錯誤,使用的預設資料來源進行的查詢

關於多資料來源可以檢視:
尋找風口的豬:
mybatis用spring的動態資料來源實現讀寫分離
以及
使用springboot + druid + mybatisplus完成多資料來源配置
如何配置Springboot多資料來源

總結

遇到這樣的問題,還是需要理解清楚,多資料來源的原理.大致實現過程,才更容易排查問題

life is beautiful,我是Alon,如果你有問題,歡迎給我留言。