apache / shardingsphere

Distributed SQL transaction & query engine for data sharding, scaling, encryption, and more - on any database.
Apache License 2.0
19.79k stars 6.7k forks source link

shardingshpere 5.4: Prompt table does not exist for non database allocation #32778

Closed cedar0822 closed 2 weeks ago

cedar0822 commented 2 weeks ago

Hi, I configured the data source and database and table splitting strategy through the JAVA API, Currently, there is no problem with single data sources for table queries that do not participate in database partitioning. When there are multiple data sources, it is prompted that the table does not exist.

this is sphere config: pom:

org.apache.shardingsphere shardingsphere-jdbc-core 5.4.1

private Map<String, DataSource> initShardingDataSources(Environment env) throws SQLException { String dataname = "sharding"; Map<String, DataSource> dataSourceMap = new HashMap<>(); HikariDataSource dataSourceWrite = new HikariDataSource(); dataSourceWrite.setDriverClassName(env.getProperty("spring.datasource.druid.driver-class-name")); dataSourceWrite.setJdbcUrl(env.getProperty("spring.datasource.url")); dataSourceWrite.setUsername(env.getProperty("spring.datasource.username")); dataSourceWrite.setPassword(env.getProperty("spring.datasource.password")); dataSourceMap.put(dataname, dataSourceWrite); slaveDataSources.put(dataname, shardingDataSource(dataSourceMap)); return dataSourceMap; } DataSource shardingDataSource(Map<String, DataSource> dataSourceMap) throws SQLException { Properties props = new Properties(); props.setProperty("sql-show", Boolean.TRUE.toString()); Collection ruleConfigs = new ArrayList<>(); ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); shardingRuleConfig.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("nabd", ".")); shardingRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("create_time", "tb-sharding-algorithm")); shardingRuleConfig.getTables().add(getGiftSendRecordTableRuleConfiguration()); shardingRuleConfig.setShardingAlgorithms(getShardingAlgorithms()); DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, ruleConfigs, props); return dataSource; }

ShardingTableRuleConfiguration getGiftSendRecordTableRuleConfiguration() {
    ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("gift_send_record", "nabd.gift_send_record_$->{[2024]}_Q$->{1..4}");
    result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("create_time","tb-sharding-algorithm"));
    return result;
}
private Map<String, AlgorithmConfiguration> getShardingAlgorithms() {
    Map<String, AlgorithmConfiguration> shardingAlgorithms = new LinkedHashMap<>();
    Properties tableAlgorithms = new Properties();
    tableAlgorithms.setProperty("strategy","standard");
    tableAlgorithms.setProperty("algorithmClassName", TableShardingAlgorithmByQuarter.class.getName());
    shardingAlgorithms.put("tb-sharding-algorithm", new AlgorithmConfiguration("CLASS_BASED", tableAlgorithms));

    return shardingAlgorithms;
}

there is an error when query table of no separate database:

Caused by: org.apache.ibatis.executor.ExecutorException: Error preparing statement. Cause: org.apache.shardingsphere.infra.exception.TableNotExistsException: Table or view gift_send_record 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 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.Invocation.proceed(Invocation.java:49) at com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor.intercept(PaginationInterceptor.java:174) at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61) at com.sun.proxy.$Proxy715.prepare(Unknown Source) at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.prepareStatement(MybatisSimpleExecutor.java:92) at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doUpdate(MybatisSimpleExecutor.java:53) at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117) at com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor.update(MybatisCachingExecutor.java:83) 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.$Proxy714.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) ... 100 common frames omitted Caused by: org.apache.shardingsphere.infra.exception.TableNotExistsException: Table or view gift_send_record does not exist. at org.apache.shardingsphere.infra.binder.segment.from.impl.SimpleTableSegmentBinder.lambda$checkTableExists$4(SimpleTableSegmentBinder.java:146) at org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions.checkState(ShardingSpherePreconditions.java:41) at org.apache.shardingsphere.infra.binder.segment.from.impl.SimpleTableSegmentBinder.checkTableExists(SimpleTableSegmentBinder.java:143) at org.apache.shardingsphere.infra.binder.segment.from.impl.SimpleTableSegmentBinder.bind(SimpleTableSegmentBinder.java:82) at org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder.lambda$bind$0(InsertStatementBinder.java:58) at java.util.Optional.ifPresent(Optional.java:159) at org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder.bind(InsertStatementBinder.java:58) at org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder.bind(InsertStatementBinder.java:48) at org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bindDMLStatement(SQLBindEngine.java:103) at org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bind(SQLBindEngine.java:80) at org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bind(SQLBindEngine.java:71) at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.(ShardingSpherePreparedStatement.java:202) at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.(ShardingSpherePreparedStatement.java:167) at org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection.prepareStatement(ShardingSphereConnection.java:83) at org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:86) at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:88) ... 126 common frames omitted

B

I don't see configure single table rule with java API, can you give me some java API configure demo?

comecny commented 2 weeks ago

A single table does not require a configuration rule, or you can set a fixed rule instead of a dynamic one. I see that your rule uses the creation time for sharding.

cedar0822 commented 2 weeks ago

image multiple tables select is not union all,What's the problem?

cedar0822 commented 2 weeks ago

您好,这个问题我降到5.2.0版本解决了。但是又有一个新问题,我是springboot项目,本地启动是正常的,但是打成war包放到服务器Tomcat上后启动报错:

Failed to get driver instance for jdbcUrl=jdbc:h2:mem:config;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL

private void initShardingDataSources(Environment env) throws SQLException { String dataname = "sharding"; Map<String, DataSource> dataSourceMap = new HashMap<>(); DruidDataSource dataSourceWrite = new DruidDataSource(); dataSourceWrite.setDriverClassName(env.getProperty("spring.datasource.druid.driver-class-name")); dataSourceWrite.setUrl(env.getProperty("spring.datasource.url")); dataSourceWrite.setUsername(env.getProperty("spring.datasource.username")); dataSourceWrite.setPassword(env.getProperty("spring.datasource.password")); dataSourceMap.put(dataname, dataSourceWrite); slaveDataSources.put(dataname, shardingDataSource(dataSourceMap)); } DataSource shardingDataSource(Map<String, DataSource> dataSourceMap) throws SQLException { Properties props = new Properties(); props.setProperty("sql-show", Boolean.TRUE.toString()); props.setProperty("provider","MySQL"); DruidDataSource sharding = (DruidDataSource) dataSourceMap.get("sharding"); props.setProperty("jdbc_url", sharding.getDriverClassName()); props.setProperty("username", sharding.getUsername()); props.setProperty("password", sharding.getPassword()); Collection ruleConfigs = new ArrayList<>(); ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); shardingRuleConfig.getBindingTableGroups().add("gift_send_record");

shardingRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("create_time", "tb-sharding-algorithm")); shardingRuleConfig.getTables().add(getGiftSendRecordTableRuleConfiguration()); shardingRuleConfig.getTables().add(getGiftTableRuleConfiguration()); shardingRuleConfig.setShardingAlgorithms(getShardingAlgorithms()); // 内置Snowflake分布式序列算法配置 Properties snowflakeProp = new Properties(); snowflakeProp.setProperty("worker-id", "1"); shardingRuleConfig.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", snowflakeProp));

ruleConfigs.add(shardingRuleConfig); ModeConfiguration modeConfiguration = new ModeConfiguration("Standalone", new StandalonePersistRepositoryConfiguration("JDBC", props),false); DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(modeConfiguration,dataSourceMap, ruleConfigs, props); return dataSource; }

At 2024-09-03 08:23:30, "comecny" @.***> wrote:

A single table does not require a configuration rule, or you can set a fixed rule instead of a dynamic one. I see that your rule uses the creation time for sharding.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>