开发者社区> 问答> 正文

Sharding-JDBC单库分表配置问题 -java报错

"

Sharding-JDBC(V1.5.0)单库分表除了配置需要拆分的表之外其他的不拆分的表是否也需要配置,只是不需要配置规则即可?

目前我是没有配置不需要拆分的数据表,在系统查询过程中一些存在外键关联的表查询出现了“Unable to find com.**.Module with id 0; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.**.Module with id 0”错误

以下是我的配置之根据demo简单配置了需要拆分的表(applicationContext-sharding-JDBC.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.dangdang.com/schema/ddframe/rdb
                        http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd
                        ">
    <!--分表规则-->
    <rdb:strategy id="pcardIndexStrategy" sharding-columns="wyidh" algorithm-class="com.****.algorithm.SingleKeyModuloTableShardingAlgorithm"/>

    <rdb:data-source id="dataSource">
        <rdb:sharding-rule data-sources="db-node-0" default-data-source="db-node-0">
            <rdb:table-rules>
                <rdb:table-rule logic-table="tb_pcard_index" actual-tables="tb_pcard_index,tb_pcard_index1,tb_pcard_index2,tb_pcard_index3" table-strategy="pcardIndexStrategy"/>
            </rdb:table-rules>
            <rdb:binding-table-rules>
                <rdb:binding-table-rule logic-tables="tb_pcard_index"/>
            </rdb:binding-table-rules>
            <rdb:default-database-strategy sharding-columns="none" algorithm-class="com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm"/>
        </rdb:sharding-rule>
        <rdb:props>
            <prop key="metrics.enable">true</prop>
        </rdb:props>
    </rdb:data-source>
</beans>
" ![image.png](https://ucc.alicdn.com/pic/developer-ecology/1bb4615d611f4b37818a148ca8ee9a04.png)

展开
收起
montos 2020-05-29 22:14:47 2935 0
1 条回答
写回答
取消 提交回答
  • "

    题中出现的问题,我将数据表中关联外键为空的设置为“0”,然后从代码实现上做了处理解决了如上问题,另外如大家对上边问题有更好的解决办法欢指教,配置依旧未变化再查询其它表的时候出现了“Cannot find table rule and default data source with logic tables: '[security_user, security_user_role]"看到论坛有遇到类似问题的同学大家都是推荐配置多个数据源一个里面放需要拆分的数据表,另外一个放置不需要拆分的数据表,但是这样是不是就达不到我想做的单库分表了

    ######

    引用Sharding-Jdbc作者回答

     10. 如果只有部分数据库分库分表,是否需要将不分库分表的表也配置在分片规则中?

    回答:

    是的。因为Sharding-JDBC是将多个数据源合并为一个统一的逻辑数据源。因此即使不分库分表的部分,不配置分片规则Sharding-JDBC即无法精确的断定应该路由至哪个数据源。
    但是Sharding-JDBC提供了两种变通的方式,有助于简化配置。

    方法1:配置default-data-source,凡是在默认数据源中的表可以无需配置在分片规则中,Sharding-JDBC将在找不到分片数据源的情况下将表路由至默认数据源。

    方法2:将不参与分库分表的数据源独立于Sharding-JDBC之外,在应用中使用多个数据源分别处理分片和不分片的情况。

    ######<div class=""ref"">

    引用来自“AN安分分”的评论

    引用Sharding-Jdbc作者回答

     10. 如果只有部分数据库分库分表,是否需要将不分库分表的表也配置在分片规则中?

    回答:

    是的。因为Sharding-JDBC是将多个数据源合并为一个统一的逻辑数据源。因此即使不分库分表的部分,不配置分片规则Sharding-JDBC即无法精确的断定应该路由至哪个数据源。
    但是Sharding-JDBC提供了两种变通的方式,有助于简化配置。

    方法1:配置default-data-source,凡是在默认数据源中的表可以无需配置在分片规则中,Sharding-JDBC将在找不到分片数据源的情况下将表路由至默认数据源。

    方法2:将不参与分库分表的数据源独立于Sharding-JDBC之外,在应用中使用多个数据源分别处理分片和不分片的情况。

    以上回答是在讨论群sharding-jdbc作者张亮哥的回答,怕我理解有误就将原话搬了过来

    ######

    方法1:配置default-data-source,凡是在默认数据源中的表可以无需配置在分片规则中,Sharding-JDBC将在找不到分片数据源的情况下将表路由至默认数据源。

    re: 版本:1.5.4

    单表查询可以,多表关联查询就不行,会报错:Cannot find table rule and default data source with logic tables: '%s'。

    " ![image.png](https://ucc.alicdn.com/pic/developer-ecology/1e5650462f1542e2b32e840d541a6814.png)
    2020-05-29 22:14:58
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载