开发者社区 问答 正文

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

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>

展开
收起
huc_逆天 2020-05-28 09:22:28 778 分享 版权
阿里云 AI 助理回答

在使用Sharding-JDBC进行分库分表时,对于未配置分片规则的表,默认情况下它们将被视为非分片表,即这些表的数据操作不会经过Sharding-JDBC的分片逻辑处理。因此,理论上讲,除了需要分片的表之外,其他不拆分的表确实不需要特别配置规则,仅配置需要分片的表即可。

但是,根据您遇到的问题“Unable to find com..Module with id 0; nested exception is javax.persistence.EntityNotFoundException: Unable to find com..Module with id 0”,这似乎是一个关联查询的问题,可能是因为在执行关联查询时,Sharding-JDBC未能正确解析或路由到正确的数据表上,导致找不到预期的数据记录。

Sharding-JDBC支持关联查询,但需要注意的是,关联查询的策略和配置需要正确设置,特别是当涉及到分片表与非分片表之间的关联时。如果您的应用中存在对外键关联表的查询,并且这些表没有被正确地识别或处理,就可能会出现上述错误。

为了解决这个问题,您可以考虑以下几点:

  1. 检查关联查询的SQL语句:确保SQL语句能够被Sharding-JDBC正确解析并映射到正确的数据表上。有时候,特定的SQL语法或函数可能会影响Sharding-JDBC的解析逻辑。

  2. 配置关联查询策略:Sharding-JDBC提供了对关联查询的支持,包括广播表、笛卡尔积关联等策略。如果您有表是作为其他分片表的关联表(例如,一个配置为不分片的表经常与分片表一起查询),可能需要将其配置为广播表(Broadcast Table)或者明确指定关联查询的策略。

  3. 日志分析:开启Sharding-JDBC的日志,详细查看SQL解析、路由以及执行的全过程,这有助于定位问题所在。

  4. 版本兼容性:确认使用的Sharding-JDBC版本与您的数据库、JDBC驱动以及其他相关技术栈兼容,有时问题可能来源于版本不匹配。

  5. 实体关系映射(ORM)配置:如果使用了如Hibernate等ORM框架,确保关联关系的映射配置正确无误,包括外键关联的处理方式。

综上所述,虽然未分片的表通常不需要特殊配置规则,但在实际应用中,尤其是涉及复杂关联查询时,确保整体的配置和策略正确无误是非常重要的。建议您根据上述提示进一步排查和调整配置。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答