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

5.0.1版本PageHelper导致ShardingJdbc强制分页失效 #384

Closed
andersonme opened this issue May 17, 2019 · 5 comments
Closed

5.0.1版本PageHelper导致ShardingJdbc强制分页失效 #384

andersonme opened this issue May 17, 2019 · 5 comments

Comments

@andersonme
Copy link

配置:
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("autoRuntimeDialect", "true");
pageInterceptor.setProperties(properties);
List mybatisInterceptors = new ArrayList<>();
mybatisInterceptors.add(pageInterceptor);

代码调用:
HintManager hintManager = HintManager.getInstance();
hintManager.addDatabaseShardingValue("project", "id", 2);
hintManager.addTableShardingValue("project", "id", 2);
PageHelper.startPage(0, 2, false);
List projectServices = projectMapper.queryAllProjects();

执行结果异常:报表”product1.product_0001"表不存在

跟踪代码显示ShardingJdbc调用时HintManager中设置的强制分片无效,跟踪PageHelper的代码,发现PageAutoDialect中会在初始dialect时调用getUrl这个方法:
private String getUrl(DataSource dataSource) {
Connection conn = null;
try {
conn = dataSource.getConnection();
return conn.getMetaData().getURL();
} catch (SQLException e) {
throw new PageException(e);
} finally {
if (conn != null) {
try {
if (closeConn) {
conn.close();
}
} catch (SQLException e) {
//ignore
}
}
}
}
这个方法在获取连接,拿到元数据后关闭,问题就在于如果是ShardingJdbc数据源,拿到的连接是ShardingConnection,ShardingJDBC在close连接时,会清除当前线程里的TheadLocal变量,这就导致当前线程设置的强制分片设置丢失了。

能否在getUrl时用新开线程(如Callable)的方式实现,不要对当前线程造成影响?

@abel533
Copy link
Collaborator

abel533 commented May 24, 2019

可以配置是否关闭 conn

@andersonme
Copy link
Author

谢谢回答,我想是可以通过 properties.put("closeConn","false") 来防止关闭连接,但是会不会造成连接不能释放的问题呢?

@lengmianshi
Copy link

我想通过
HintManager.getInstance().setMasterRouteOnly()
来强制查主库,但pagehelper的getUrl方法会关闭连接,导致分页查询走的还是读库,怎么破?

@lengmianshi
Copy link

谢谢回答,我想是可以通过 properties.put("closeConn","false") 来防止关闭连接,但是会不会造成连接不能释放的问题呢?

解决了吗?

@abel533
Copy link
Collaborator

abel533 commented Oct 6, 2021

新版本还可以自定义获取dialect实现

@abel533 abel533 closed this as completed Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants