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

分页查询,路由多个分片,只有一个分片有数据,返回结果集不正确 #287

Closed
along101 opened this issue Jul 12, 2017 · 1 comment

Comments

@along101
Copy link

along101 commented Jul 12, 2017

Please answer these questions before submitting your issue. Thanks!

Which version of Sharding-Jdbc do you using?

1.4.2

Expected behavior

分页查询,select * from t_order limit 2,2 路由多个分片,只有一个分片有数据,查询4条记录

Actual behavior

应该只返回2条记录

Steps to reproduce the behavior

sharding-jdbc-example-mybatis这个demo工程中,增加测试类
`
package com.dangdang.ddframe.rdb.sharding.example.jdbc;
import com.dangdang.ddframe.rdb.sharding.example.jdbc.entity.Order;
import com.dangdang.ddframe.rdb.sharding.example.jdbc.repository.OrderRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;

public class Test {
public static void main(final String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/mybatisContext.xml");
OrderRepository orderRepository = applicationContext.getBean(OrderRepository.class);
List ls = orderRepository.selectAll();
System.out.println(ls);
}
}

`

修改OrderMapper.xml最后一个查询,增加limit
`

<select id="selectAll" resultMap="baseResultMap">
    SELECT
    <include refid="baseColumnList"/>
    FROM `t_order` limit 2,2
</select>

修改数据库数据:
delete from ds_0.t_order_0;
delete from ds_0.t_order_1;
delete from ds_1.t_order_0;
delete from ds_1.t_order_1;

INSERT INTO ds_0.t_order_0 VALUES (1000, 10, 'INIT');
INSERT INTO ds_0.t_order_0 VALUES (1002, 10, 'INIT');
INSERT INTO ds_0.t_order_0 VALUES (1004, 10, 'INIT');
INSERT INTO ds_0.t_order_0 VALUES (1006, 10, 'INIT');
INSERT INTO ds_0.t_order_0 VALUES (1008, 10, 'INIT');
`

执行打印结果有4条记录:
[order_id: 1000, user_id: 10, status: INIT, order_id: 1002, user_id: 10, status: INIT, order_id: 1004, user_id: 10, status: INIT, order_id: 1006, user_id: 10, status: INIT]

调试代码发现,shardingJDBC在包装结果集时,根据mergeContext判断是不是要聚合,ResultSetFactory.getResultSet方法中
ShardingResultSets shardingResultSets = new ShardingResultSets(resultSets);
传入的resultSets有4个rs,获取到的shardingResultSets 只有一个,因为只有一个rs有数据,
ShardingResultSets.filterResultSets方法会过滤掉没有记录的rs
`

private List filterResultSets(final List resultSets) throws SQLException {
List result = new ArrayList<>(resultSets.size());
for (ResultSet each : resultSets) {
if (each.next()) {
result.add(new WrapperResultSet(each));
}
}
return result;
}
`

这样的话在shardingResultSets.getType()返回的是SINGLE
`

switch (shardingResultSets.getType()) {
case EMPTY:
return buildEmpty(resultSets);
case SINGLE:
return buildSingle(shardingResultSets);
case MULTIPLE:
return buildMultiple(shardingResultSets, mergeContext);
default:
throw new UnsupportedOperationException(shardingResultSets.getType().toString());
}
`
这样就不会去做合并结果集操作了

Please provide the reproduce example codes (such as github link) if possible.

@terrymanu
Copy link
Member

terrymanu commented Jul 12, 2017

这个问题已经在1.5.0.M2修复了
duplicate with #239

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

2 participants