apache / shardingsphere

Empowering Data Intelligence with Distributed SQL for Sharding, Scalability, and Security Across All Databases.
Apache License 2.0
19.99k stars 6.75k forks source link

-! SHARDING table error Table or view 'order_details' does not exist #33709

Open zxh-im opened 1 week ago

zxh-im commented 1 week ago

The SINGLE table is normal, while the SHARDING table reports an error version:shardingsphere-jdbc 5.5.1 sharding.yml:

dataSources:
  db0:
    dataSourceClassName:  com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.0.39:3306/ry
    username: root
    password: 123456

rules:
  - !SINGLE
    tables:
      - "*.*"
  - !SHARDING
    tables:
      order_details:
        actualDataNodes: db0.order_details_$->{com.hehui.system.config.sharding.YearMonthShardingAlgorithm.list()}
        tableStrategy:
          standard:
            shardingColumn: createTime
            shardingAlgorithmName: test1
    shardingAlgorithms:
      test1:
        type: CLASS_BASED
        props:
          strategy: standard
          algorithmClassName: com.hehui.system.config.sharding.OrderDetailsShardingAlgorithm
props:
  sql-show: true

createDataSource:

    @Bean
    public DataSource masterDataSource(DruidProperties druidProperties) throws SQLException, IOException {
         DataSource dataSource =YamlShardingSphereDataSourceFactory.createDataSource(ShardingConfig.getShardingYAMLFile());
         return dataSource;
    }

    @Bean
    @ConfigurationProperties("spring.datasource.druid.slave")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
    public DataSource slaveDataSource(DruidProperties druidProperties)
    {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    @Bean(name = "dynamicDataSource")
    @Primary
    public DynamicDataSource dataSource(DataSource masterDataSource)
    {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
        setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
        return new DynamicDataSource(masterDataSource, targetDataSources);
   }

this is the stacktrace:

Caused by: org.apache.ibatis.executor.ExecutorException: Error preparing statement.  Cause: org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException: Table or view 'order_details' does not exist.
    at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:97)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.prepare(RoutingStatementHandler.java:59)
    at org.apache.ibatis.executor.ReuseExecutor.prepareStatement(ReuseExecutor.java:89)
    at org.apache.ibatis.executor.ReuseExecutor.doUpdate(ReuseExecutor.java:51)
    at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
    at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
    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.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
    at com.sun.proxy.$Proxy478.update(Unknown Source)
    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)
    ... 167 common frames omitted
Caused by: org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException: Table or view 'order_details' does not exist.
    at org.apache.shardingsphere.infra.binder.engine.segment.from.type.SimpleTableSegmentBinder.lambda$checkTableExists$3(SimpleTableSegmentBinder.java:130)
    at org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions.checkState(ShardingSpherePreconditions.java:44)
    at org.apache.shardingsphere.infra.binder.engine.segment.from.type.SimpleTableSegmentBinder.checkTableExists(SimpleTableSegmentBinder.java:127)
    at org.apache.shardingsphere.infra.binder.engine.segment.from.type.SimpleTableSegmentBinder.bind(SimpleTableSegmentBinder.java:84)
    at org.apache.shardingsphere.infra.binder.engine.statement.dml.InsertStatementBinder.lambda$bind$0(InsertStatementBinder.java:46)
    at java.util.Optional.ifPresent(Optional.java:159)
    at org.apache.shardingsphere.infra.binder.engine.statement.dml.InsertStatementBinder.bind(InsertStatementBinder.java:46)
    at org.apache.shardingsphere.infra.binder.engine.type.DMLStatementBindEngine.bind(DMLStatementBindEngine.java:55)
    at org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bindSQLStatement(SQLBindEngine.java:63)
    at org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bind(SQLBindEngine.java:53)
    at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.<init>(ShardingSpherePreparedStatement.java:146)
    at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.<init>(ShardingSpherePreparedStatement.java:126)
    at org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection.prepareStatement(ShardingSphereConnection.java:126)
    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.apache.ibatis.logging.jdbc.ConnectionLogger.invoke(ConnectionLogger.java:55)
    at com.sun.proxy.$Proxy128.prepareStatement(Unknown Source)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:81)
    at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:88)
    ... 185 common frames omitted
terrymanu commented 5 days ago

The markdown format in your issue is incorrect. To facilitate reading, could you please revise it?

zxh-im commented 5 days ago

The markdown format in your issue is incorrect. To facilitate reading, could you please revise it?

I've already revised the Markdown format in my issue.

terrymanu commented 5 days ago

The issue involves other third-party dependencies, but our focus is solely on ShardingSphere itself. Since ShardingSphere implements the JDBC interface, standard applications should be functional. We wish to allocate more effort towards enhancing the current version, and therefore will no longer handle such issues. Please read the documentation or provide more effective information when submitting an issue.

zxh-im commented 4 days ago

The issue involves other third-party dependencies, but our focus is solely on ShardingSphere itself. Since ShardingSphere implements the JDBC interface, standard applications should be functional. We wish to allocate more effort towards enhancing the current version, and therefore will no longer handle such issues. Please read the documentation or provide more effective information when submitting an issue.

I see the error occurs in the SimpleTableSegmentBinder.checkTableExists method. The binderContext.getMetaData().getDatabase(databaseName).getSchema(schemaName).containsTable(tableName) method does not contain the current shard table. I debugged and found that this data should only have a single table. Can you tell me what to check?

terrymanu commented 4 days ago

The best way is to provide an operation that can be reproduced, such as executing reproducible SQL through JDBC or Proxy.