shardingsphere: An error occurs after use shadingsphere-jdbc

Question

For English only, other languages will not accept.

Before asking a question, make sure you have:

Please pay attention on issues you submitted, because we maybe need more details. If no response anymore and we cannot reproduce it on current information, we will close it.

Using ShardingJDBC for table partitioning,2 datasources:db_test,db_demo.

The tables that need to be split are under db_test

Environment

ShardingJDBC version 5.2.1 Spring 4.3.16 Mybatis 3.2.7 MySQL

Problem

the SQL is:

<select id="queryTaskListByTaskStatus" resultMap="BaseResultMap" parameterType="java.util.Map">
    SELECT
    <include refid="Base_Column_List"/>
    FROM db_demo.t_act_notify_task
    WHERE task_status = #{task_status}
</select>

when execute this SQL,An error occur,error message details:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error preparing statement.  Cause: org.apache.shardingsphere.dialect.exception.syntax.database.UnknownDatabaseException
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
	at com.sun.proxy.$Proxy45.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:198)
	at com.user.dao.AcctNotifyTaskDao.queryTaskListByTaskStatus(AcctNotifyTaskDao.java:133)
	at com.user.facade.impl.AcctNotifyTaskFacadeImpl.queryTaskListByTaskStatus(AcctNotifyTaskFacadeImpl.java:86)
	at org.apache.dubbo.common.bytecode.Wrapper102.invokeMethod(Wrapper102.java)
	at org.apache.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:47)
	at org.apache.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:84)
	at org.apache.dubbo.config.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:56)
	at org.apache.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)
	at org.apache.dubbo.rpc.protocol.FilterNode.invoke(FilterNode.java:61)
	at org.apache.dubbo.rpc.protocol.FilterNode.invoke(FilterNode.java:61)
	at org.apache.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:77)
	at org.apache.dubbo.rpc.protocol.FilterNode.invoke(FilterNode.java:61)
	at org.apache.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:46)
	at org.apache.dubbo.rpc.protocol.FilterNode.invoke(FilterNode.java:61)
	at org.apache.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:91)
	at org.apache.dubbo.rpc.protocol.FilterNode.invoke(FilterNode.java:61)
	at com.user.framework.core.dubbo.filter.ExceptionFilter.invoke(ExceptionFilter.java:36)
	at org.apache.dubbo.rpc.protocol.FilterNode.invoke(FilterNode.java:61)
	at org.apache.dubbo.rpc.protocol.FilterNode.invoke(FilterNode.java:61)
	at org.apache.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:192)
	at org.apache.dubbo.rpc.protocol.FilterNode.invoke(FilterNode.java:61)
	at org.apache.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)
	at org.apache.dubbo.rpc.protocol.FilterNode.invoke(FilterNode.java:61)
	at org.apache.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:41)
	at org.apache.dubbo.rpc.protocol.FilterNode.invoke(FilterNode.java:61)
	at com.alibaba.dubbo.rpc.Invoker$CompatibleInvoker.invoke(Invoker.java:55)
	at com.dynatrace.oneagent.sdk.dubbo.DynatraceProviderFilter.invoke(DynatraceProviderFilter.java:74)
	at com.alibaba.dubbo.rpc.Filter.invoke(Filter.java:29)
	at org.apache.dubbo.rpc.protocol.FilterNode.invoke(FilterNode.java:61)
	at org.apache.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:129)
	at org.apache.dubbo.rpc.protocol.FilterNode.invoke(FilterNode.java:61)
	at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:148)
	at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:100)
	at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:175)
	at org.apache.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51)
	at org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:57)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.dubbo.common.threadlocal.InternalRunnable.run(InternalRunnable.java:41)
	at java.lang.Thread.run(Thread.java:750)
Caused by: org.apache.ibatis.executor.ExecutorException: Error preparing statement.  Cause: org.apache.shardingsphere.dialect.exception.syntax.database.UnknownDatabaseException
	at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:94)
	at org.apache.ibatis.executor.statement.RoutingStatementHandler.prepare(RoutingStatementHandler.java:57)
	at org.apache.ibatis.executor.BatchExecutor.doQuery(BatchExecutor.java:85)
	at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)
	at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:137)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:96)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:77)
	at sun.reflect.GeneratedMethodAccessor337.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.ibatis.plugin.Invocation.proceed(Invocation.java:49)
	at com.core.ibatis.PageHelper.intercept(PageHelper.java:92)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:60)
	at com.sun.proxy.$Proxy58.query(Unknown Source)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:108)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)
	at sun.reflect.GeneratedMethodAccessor338.invoke(Unknown Source)
	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:358)
	... 44 common frames omitted
Caused by: org.apache.shardingsphere.dialect.exception.syntax.database.UnknownDatabaseException: null
	at org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext.lambda$getSchemas$0(SelectStatementContext.java:138)
	at org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions.checkNotNull(ShardingSpherePreconditions.java:55)
	at org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext.getSchemas(SelectStatementContext.java:138)
	at org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext.<init>(SelectStatementContext.java:116)
	at org.apache.shardingsphere.infra.binder.SQLStatementContextFactory.getDMLStatementContext(SQLStatementContextFactory.java:163)
	at org.apache.shardingsphere.infra.binder.SQLStatementContextFactory.newInstance(SQLStatementContextFactory.java:146)
	at org.apache.shardingsphere.infra.binder.SQLStatementContextFactory.newInstance(SQLStatementContextFactory.java:131)
	at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.<init>(ShardingSpherePreparedStatement.java:196)
	at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.<init>(ShardingSpherePreparedStatement.java:160)
	at org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection.prepareStatement(ShardingSphereConnection.java:91)
	at org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:75)
	at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:85)
	... 63 common frames omitted

but when I remove the database name db_demo,the SQL can execute normally, such as:

<select id="queryTaskListByTaskStatus" resultMap="BaseResultMap" parameterType="java.util.Map">
    SELECT
    <include refid="Base_Column_List"/>
    FROM t_act_notify_task
    WHERE task_status = #{task_status}
</select>

Because the SQL writing in the project specifies the database name, it is not convenient to modify it. Is there any other solution

Configuration

<sharding:sharding-algorithm id="userLogShardingAlgorithm" type="complex">
    <props>
        <prop key="algorithm-class-name">com.user.sharding.algorithm.UserLogShardingAlgorithm</prop>
    </props>
</sharding:sharding-algorithm>

<sharding:complex-strategy id="userLogSharding" sharding-columns="create_time,login_no" algorithm-ref="userLogShardingAlgorithm"/>

<sharding:rule id="shardingRule">
    <sharding:table-rules>
        <sharding:table-rule logic-table="t_user_log" actual-data-nodes="db_test.t_use_log_${2024..2030}" table-strategy-ref="userLogSharding"/>
    </sharding:table-rules>
</sharding:rule>


<shardingsphere:data-source id="shardingDataSourceMaster" data-source-names="testDataSource, demoDataSource" rule-refs="shardingRule">
    <props>
        <prop key="sql-show">true</prop>
        <prop key="check.table.metadata.enabled">false</prop>
    </props>
</shardingsphere:data-source>


jdbc.username=dev
jdbc.password=
jdbc.driver=com.p6spy.engine.spy.P6SpyDriver
jdbc.url=jdbc:p6spy:mysql://127.0.0.1:3306/db_test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8

task.jdbc.username=dev
task.jdbc.password=
task.jdbc.driver=com.p6spy.engine.spy.P6SpyDriver
task.jdbc.url=jdbc:p6spy:mysql://127.0.0.1:3306/db_demo?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 28 (28 by maintainers)

Most upvoted comments

The answer is affirmative. It is only possible to obtain a List of target database names through a custom algorithm implemented via SPI.

  • What I mean is that the driverClassName property will not be assigned a value! The condition isSkippedProperty (fieldName) is false, so it will not be assigned,the unit tests is simple:
 @Test
    public void test() {
        DataSource source = new HikariDataSource();
        DataSourcePoolReflection dataSourcePoolReflection = new DataSourcePoolReflection(source);

        dataSourcePoolReflection.setField("jdbcUrl", "jdbc:mysql://localhost:3306/test");
        dataSourcePoolReflection.setField("driverClassName", "com.mysql.cj.jdbc.Driver");

        System.out.println(dataSourcePoolReflection);
    }
  • Breakpoint observation, you can see that jdbcUrl can be assigned normally, but driverClassName does not