Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sharding-jdbc-example-mybatis例子修改成algorithm-expression就报错了 #116

Closed
yangl opened this issue Jul 1, 2016 · 4 comments
Closed

Comments

@yangl
Copy link

yangl commented Jul 1, 2016

修改shardingContext.xml配置如下:

<rdb:strategy id="databaseShardingStrategy" sharding-columns="user_id" algorithm-expression="ds_{user_id.longValue() % 2}"/>
<!--algorithm-class="com.dangdang.ddframe.rdb.sharding.example.jdbc.algorithm.SingleKeyModuloTableShardingAlgorithm"-->
<rdb:strategy id="tableShardingStrategy" sharding-columns="order_id" algorithm-expression="t_order_{order_id.longValue() % 2}"/>

其它内容没任何改动,运行到insert就开始报错了,发现ShardingConnection.getConnectionInternal()方法中的dataSourceName还没被替换为正式的ds_0 ds_1而是ds_{user_id.longValue() % 2},从而导致获取不到datasource!
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at java.util.Collections$UnmodifiableList.get(Collections.java:1309)
at com.dangdang.ddframe.rdb.sharding.executor.PreparedStatementExecutor.execute(PreparedStatementExecutor.java:171)
at com.dangdang.ddframe.rdb.sharding.jdbc.ShardingPreparedStatement.execute(ShardingPreparedStatement.java:109)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:44)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:69)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:48)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:105)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:71)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:152)
... 23 more

@haocao
Copy link
Member

haocao commented Jul 1, 2016

请检查一下表达式,ds_{user_id.longValue() % 2}应该为ds_${user_id.longValue() % 2}

@yangl
Copy link
Author

yangl commented Jul 1, 2016

@haocao 3Q,已关闭!我的错

@yangl yangl closed this as completed Jul 1, 2016
@yangl
Copy link
Author

yangl commented Jul 1, 2016

@haocao 还咨询一下,想按uid最后第三位来分库,表达式怎么写呢?

ds_${user_id.longValue() % 1000 / 100}

这样子不支持呢!

@yangl yangl reopened this Jul 1, 2016
@yangl
Copy link
Author

yangl commented Jul 1, 2016

das_${(uid.longValue() % 1000).intdiv(100)}
原来是这样子哈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants