当前位置:首页 > 通信资讯 > 正文

sharding jdbc使用(Sharding-jdbc)

目录
  • 使用限制
    • JDBC未支持列表
      • DataSource接口
      • Connection接口
      • Statement和PreparedStatement接口
      • 对于ResultSet接口
      • JDBC 4.1
    • SQL语句限制
    • shardingjdbc使用及踩坑内容
      • 1.使用shardingjdbc做分库分表
        • 2.踩坑内容

        使用限制

        JDBC未支持列表

        • Sharding-JDBC暂时未支持不常用的JDBC方法。

        DataSource接口

        • 不支持timeout相关操作

        Connection接口

        • 不支持存储过程,函数,游标的操作
        • 不支持执行native的SQL
        • 不支持savepoint相关操作
        • 不支持Schema/Catalog的操作
        • 不支持自定义类型映射

        Statement和PreparedStatement接口

        • 不支持返回多结果集的语句(即存储过程,非SELECT多条数据)
        • 不支持国际化字符的操作

        对于ResultSet接口

        • 不支持对于结果集指针位置判断
        • 不支持通过非next方法改变结果指针位置
        • 不支持修改结果集内容
        • 不支持获取国际化字符
        • 不支持获取Array

        JDBC 4.1

        • 不支持JDBC 4.1接口新功能
        • 查询所有未支持方法,请阅读com.dangdang.ddframe.rdb.sharding.jdbc.unsupported包。

        SQL语句限制

        • 不支持DDL语句
        • 不支持子语句
        • 不支持UNION 和 UNION ALL
        • 不支持特殊INSERT
        • 每条INSERT语句只能插入一条数据,不支持VALUES后有多行数据的语句
        • 不支持DISTINCT聚合

        shardingjdbc使用及踩坑内容

        1.使用shardingjdbc做分库分表

        最近公司由于业务需要,对日益增加的数据量越来越无法容忍,遂作出分库分表的决定,考察了几个技术方案后,决定使用shardingsphere做分表中间件。

        使用maven拉取jar包:

        ?
        1 2 3 4 5 6 7 8 9 10 <dependency> <groupId>io.shardingsphere</groupId> <artifactId>sharding-jdbc</artifactId> <version>3.0.0.M3</version> </dependency> <dependency> <groupId>io.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-namespace</artifactId> <version>3.0.0.M3</version> </dependency>

        分表配置:

        ?
        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <?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:tx="http://www.springframework.org/schema/tx" xmlns:sharding="http://shardingsphere.io/schema/shardingsphere/sharding" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://shardingsphere.io/schema/shardingsphere/sharding http://shardingsphere.io/schema/shardingsphere/sharding/sharding.xsd" default-autowire="byName"> <!-- 分表算法 --> <bean id="tableShardingAlgorithm" class="pxf.commom.support.sharding.tableShardingAlgorithm" /> <sharding:complex-strategy id="tableStrategy" sharding-columns="uid" algorithm-ref="tableShardingAlgorithm" /> <!-- ds_0为数据源,如果做分库,可配置多个数据源;不分表的表不用在此做配置--> <sharding:data-source id="dataSource"> <sharding:sharding-rule data-source-names="ds_0" default-data-source-name="ds_0" > <sharding:table-rules> <sharding:table-rule logic-table="test_table" actual-data-nodes="ds_0.test_table_$->{0..128}" table-strategy-ref="tableStrategy" /> </sharding:table-rules> </sharding:sharding-rule> </sharding:data-source> </beans>

        2.踩坑内容

        1). 用于分表的列在sql中不能为空,所以像insert之类的语句需要做下非空判断;

        2). sqlmap中LONGVARCHER字段不能使用,会报序列化异常,可改为VARCHAR类型;

        3). union语法不支持,可改为OR查询(shardingjdbc连OR也不支持,所以建议使用shardingsphere)。

        以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

        原文链接:https://blog.csdn.net/Farrell_zeng/article/details/52958181

        如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

        为您推荐:

        发表评论

        ◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。