<h2 dir="auto">First of all</h2>
<p dir="auto">my English is suck</p>
<h2 dir="auto">Question</h2>
<p dir="auto">when I only configured the default-database-strategy ( no specific table rule are configured ) , My defaul-database-strategy didn't work. (I use hintManager.setDatabaseShardingValue() )</p>
<h2 dir="auto">Version</h2>
<p dir="auto">4.0.0</p>
<h2 dir="auto">Detail</h2>
<p dir="auto">my configuration as follow :</p>
<div class="highlight highlight-source-ini notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="spring.shardingsphere.datasource.names=psghz,psgother
# psghz库配置
spring.shardingsphere.datasource.psghz.url=jdbc:postgresql://*******************:****/*****?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false
spring.shardingsphere.datasource.psghz.username=****
spring.shardingsphere.datasource.psghz.password=********
spring.shardingsphere.datasource.psghz.driverClassName=org.postgresql.Driver
spring.shardingsphere.datasource.psghz.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.psghz.max-active=10
# psgother库配置
spring.shardingsphere.datasource.psgother.url=jdbc:postgresql://*******************:****/*****?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false
spring.shardingsphere.datasource.psgother.username=****
spring.shardingsphere.datasource.psgother.password=********
spring.shardingsphere.datasource.psgother.driverClassName=org.postgresql.Driver
spring.shardingsphere.datasource.psgother.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.psgother.max-active=10
# 默认hint配置
spring.shardingsphere.sharding.default-database-strategy.hint.algorithm-class-name=com.ibuscloud.pubtrans.analysis.plugin.sharding.HintShardingStrategy
#起初打开default-data-srouce-name,结果走了DefaultDatabaseRoutingEngine,关闭则走了UnicastRoutingEngine
#spring.shardingsphere.sharding.default-data-source-name=psghz
spring.shardingsphere.props.sql.show=true"><pre class="notranslate"><span class="pl-k">spring.shardingsphere.datasource.names</span>=psghz,psgother
<span class="pl-c"><span class="pl-c">#</span> psghz库配置</span>
<span class="pl-k">spring.shardingsphere.datasource.psghz.url</span>=jdbc:postgresql://*******************:****/*****?<span class="pl-k">useUnicode</span>=true&amp;<span class="pl-k">characterEncoding</span>=UTF-8&amp;<span class="pl-k">useSSL</span>=false
<span class="pl-k">spring.shardingsphere.datasource.psghz.username</span>=****
<span class="pl-k">spring.shardingsphere.datasource.psghz.password</span>=********
<span class="pl-k">spring.shardingsphere.datasource.psghz.driverClassName</span>=org.postgresql.Driver
<span class="pl-k">spring.shardingsphere.datasource.psghz.type</span>=com.alibaba.druid.pool.DruidDataSource
<span class="pl-k">spring.shardingsphere.datasource.psghz.max-active</span>=10
<span class="pl-c"><span class="pl-c">#</span> psgother库配置</span>
<span class="pl-k">spring.shardingsphere.datasource.psgother.url</span>=jdbc:postgresql://*******************:****/*****?<span class="pl-k">useUnicode</span>=true&amp;<span class="pl-k">characterEncoding</span>=UTF-8&amp;<span class="pl-k">useSSL</span>=false
<span class="pl-k">spring.shardingsphere.datasource.psgother.username</span>=****
<span class="pl-k">spring.shardingsphere.datasource.psgother.password</span>=********
<span class="pl-k">spring.shardingsphere.datasource.psgother.driverClassName</span>=org.postgresql.Driver
<span class="pl-k">spring.shardingsphere.datasource.psgother.type</span>=com.alibaba.druid.pool.DruidDataSource
<span class="pl-k">spring.shardingsphere.datasource.psgother.max-active</span>=10
<span class="pl-c"><span class="pl-c">#</span> 默认hint配置</span>
<span class="pl-k">spring.shardingsphere.sharding.default-database-strategy.hint.algorithm-class-name</span>=com.ibuscloud.pubtrans.analysis.plugin.sharding.HintShardingStrategy
<span class="pl-c"><span class="pl-c">#</span>起初打开default-data-srouce-name,结果走了DefaultDatabaseRoutingEngine,关闭则走了UnicastRoutingEngine</span>
<span class="pl-c"><span class="pl-c">#</span>spring.shardingsphere.sharding.default-data-source-name=psghz</span>
<span class="pl-k">spring.shardingsphere.props.sql.show</span>=true</pre></div>
<p dir="auto">I follow  the code and see the RoutingEngineFactory.java create UnicastRoutingEngine.java</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="         if (sqlStatementContext.getSqlStatement() instanceof DMLStatement &amp;&amp; tableNames.isEmpty() &amp;&amp; shardingRule.hasDefaultDataSourceName()) {
            return new DefaultDatabaseRoutingEngine(shardingRule, tableNames);
        }
        if (sqlStatementContext.getSqlStatement() instanceof DMLStatement &amp;&amp; shardingConditions.isAlwaysFalse() || tableNames.isEmpty() || !shardingRule.tableRuleExists(tableNames)) {
            return new UnicastRoutingEngine(shardingRule, tableNames);
        }"><pre class="notranslate">         <span class="pl-k">if</span> (<span class="pl-s1">sqlStatementContext</span>.<span class="pl-en">getSqlStatement</span>() <span class="pl-k">instanceof</span> <span class="pl-smi">DMLStatement</span> &amp;&amp; <span class="pl-s1">tableNames</span>.<span class="pl-en">isEmpty</span>() &amp;&amp; <span class="pl-s1">shardingRule</span>.<span class="pl-en">hasDefaultDataSourceName</span>()) {
            <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">DefaultDatabaseRoutingEngine</span>(<span class="pl-s1">shardingRule</span>, <span class="pl-s1">tableNames</span>);
        }
        <span class="pl-k">if</span> (<span class="pl-s1">sqlStatementContext</span>.<span class="pl-en">getSqlStatement</span>() <span class="pl-k">instanceof</span> <span class="pl-smi">DMLStatement</span> &amp;&amp; <span class="pl-s1">shardingConditions</span>.<span class="pl-en">isAlwaysFalse</span>() || <span class="pl-s1">tableNames</span>.<span class="pl-en">isEmpty</span>() || !<span class="pl-s1">shardingRule</span>.<span class="pl-en">tableRuleExists</span>(<span class="pl-s1">tableNames</span>)) {
            <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">UnicastRoutingEngine</span>(<span class="pl-s1">shardingRule</span>, <span class="pl-s1">tableNames</span>);
        }</pre></div>
<p dir="auto">and <strong>!shardingRule.tableRuleExists(tableNames)</strong> is true .<br>
so back to the question , how does the configuration <strong>default-database-strategy</strong> work.</p>
<p dir="auto">Anomalies:</p>
<ol dir="auto">
<li>duplicate database ID production</li>
<li>abnormal query data</li>
</ol>
<h3 dir="auto">Which version of ShardingSphere did you use?</h3>
<p dir="auto">org.apache.shardingsphere:sharding-jdbc-spring-boot-starter:4.1.1</p>
<h3 dir="auto">Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.</h3>
<ol dir="auto">
<li>Please use the code repository I provided  <a href="https://github.com/huifer/sharding-jdbc-test">github</a></li>
<li>Execute <code class="notranslate">com.example.demo.rest.AcontrollerTest#gen</code> method passing in parameter 2023，The data is presented in the database as follows</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="818114806320992257	device_1	2023-02-01 00:00:00	6	89	290	5851	60390
818114806320992258	device_2	2023-02-01 00:00:00	1	14	301	0018	21050
818114806320992259	device_3	2023-02-01 00:00:00	9	77	919	5078	00798"><pre class="notranslate"><code class="notranslate">818114806320992257	device_1	2023-02-01 00:00:00	6	89	290	5851	60390
818114806320992258	device_2	2023-02-01 00:00:00	1	14	301	0018	21050
818114806320992259	device_3	2023-02-01 00:00:00	9	77	919	5078	00798
</code></pre></div>
<ol start="3" dir="auto">
<li>Execute <code class="notranslate">com.example.demo.rest.AcontrollerTest#gen</code> method passing in parameter 2024，The data is presented in the database as follows</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="818114806320992257	device_1	2024-02-01 00:00:00	5	30	077	9618	52059
818114806320992258	device_2	2024-02-01 00:00:00	7	85	481	9165	91450
818114806320992259	device_3	2024-02-01 00:00:00	6	16	941	3355	62767"><pre class="notranslate"><code class="notranslate">818114806320992257	device_1	2024-02-01 00:00:00	5	30	077	9618	52059
818114806320992258	device_2	2024-02-01 00:00:00	7	85	481	9165	91450
818114806320992259	device_3	2024-02-01 00:00:00	6	16	941	3355	62767
</code></pre></div>
<p dir="auto"><strong>The above operation shows that the ID production results are duplicated</strong></p>
<ol start="4" dir="auto">
<li>Execute the <code class="notranslate">com.example.demo.rest.AcontrollerTest#data</code> method in this case，The returned data results in an exception。The data for 2023 was repeated 2 times, and the data for 2024 was not successfully queried</li>
</ol>
<ul dir="auto">
<li>
<p dir="auto">The result of the function execution is shown in the figure<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/26766909/211129522-bd3eca36-ae35-4ffe-b1e2-82909e5787eb.png"><img src="https://user-images.githubusercontent.com/26766909/211129522-bd3eca36-ae35-4ffe-b1e2-82909e5787eb.png" alt="image" style="max-width: 100%;"></a></p>
</li>
<li>
<p dir="auto">The execution log is as follows</p>
</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="2023-01-07 11:33:50.291  INFO 10316 --- [    Test worker] ShardingSphere-SQL                       : Actual SQL: ds ::: select sampledata0_.id as id1_0_, sampledata0_.device_id as device_i2_0_, sampledata0_.time as time3_0_, sampledata0_.v1 as v4_0_, sampledata0_.v2 as v5_0_, sampledata0_.v3 as v6_0_, sampledata0_.v4 as v7_0_, sampledata0_.v5 as v8_0_ from sample_data_2023_2 sampledata0_ where sampledata0_.time&lt;? ::: [2077-01-01 00:00:00.0]
2023-01-07 11:33:50.291  INFO 10316 --- [    Test worker] ShardingSphere-SQL                       : Actual SQL: ds ::: select sampledata0_.id as id1_0_, sampledata0_.device_id as device_i2_0_, sampledata0_.time as time3_0_, sampledata0_.v1 as v4_0_, sampledata0_.v2 as v5_0_, sampledata0_.v3 as v6_0_, sampledata0_.v4 as v7_0_, sampledata0_.v5 as v8_0_ from sample_data_2024_2 sampledata0_ where sampledata0_.time&lt;? ::: [2077-01-01 00:00:00.0]"><pre class="notranslate"><code class="notranslate">2023-01-07 11:33:50.291  INFO 10316 --- [    Test worker] ShardingSphere-SQL                       : Actual SQL: ds ::: select sampledata0_.id as id1_0_, sampledata0_.device_id as device_i2_0_, sampledata0_.time as time3_0_, sampledata0_.v1 as v4_0_, sampledata0_.v2 as v5_0_, sampledata0_.v3 as v6_0_, sampledata0_.v4 as v7_0_, sampledata0_.v5 as v8_0_ from sample_data_2023_2 sampledata0_ where sampledata0_.time&lt;? ::: [2077-01-01 00:00:00.0]
2023-01-07 11:33:50.291  INFO 10316 --- [    Test worker] ShardingSphere-SQL                       : Actual SQL: ds ::: select sampledata0_.id as id1_0_, sampledata0_.device_id as device_i2_0_, sampledata0_.time as time3_0_, sampledata0_.v1 as v4_0_, sampledata0_.v2 as v5_0_, sampledata0_.v3 as v6_0_, sampledata0_.v4 as v7_0_, sampledata0_.v5 as v8_0_ from sample_data_2024_2 sampledata0_ where sampledata0_.time&lt;? ::: [2077-01-01 00:00:00.0]
</code></pre></div>