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

[功能改进]: 是否可以在运行时支持修改查询超时时间 #6623

Open
3 tasks done
ms-maosheng opened this issue Dec 5, 2024 · 1 comment
Open
3 tasks done

Comments

@ms-maosheng
Copy link

确认

  • 我的版本是最新版本, 我的版本号与 version 相同, 并且项目里无依赖冲突
  • 我已经在 issue 中搜索过, 确认问题没有被提出过
  • 我已经修改标题, 将标题中的 描述 替换为遇到的问题

功能改进

mybatis-plus提供了default-statement-timeout: 60参数用于配置sql查询超时时间,实际开发中有些sql需要超出这个时间,但mybatis-plus没有单独设置的功能
原生mybatis在mapper.xml可以设置timeout,mapper接口可以设置@options,但是mybatis-plus通过代理生成的查询方法没有设置超时时间的功能
mybatis-plus在注入MappedStatement阶段timeout参数为null,这就意味着代理生成的查询方法不能自定义查询超时时间

MappedStatement被final修饰,本身无法修改,而通过MappedStatement提供的Build类重新生成再赋值内存地址变更又存在极大的安全隐患

参考资料

这是mybatis的代码,在配置的基础上优先获取MappedStatement超时时间
protected void setStatementTimeout(Statement stmt, Integer transactionTimeout) throws SQLException {
Integer queryTimeout = null;
if (mappedStatement.getTimeout() != null) {
queryTimeout = mappedStatement.getTimeout();
} else if (configuration.getDefaultStatementTimeout() != null) {
queryTimeout = configuration.getDefaultStatementTimeout();
}
if (queryTimeout != null) {
stmt.setQueryTimeout(queryTimeout);
}
StatementUtil.applyTransactionTimeout(stmt, queryTimeout, transactionTimeout);
}

这是mybatis-plus的代码,生成 MappedStatement timeout参数为null
protected MappedStatement addMappedStatement(Class mapperClass, String id, SqlSource sqlSource, SqlCommandType sqlCommandType, Class parameterType,
String resultMap, Class<?> resultType, KeyGenerator keyGenerator,
String keyProperty, String keyColumn) {
String statementName = mapperClass.getName() + DOT + id;
if (hasMappedStatement(statementName)) {
logger.warn(LEFT_SQ_BRACKET + statementName + "] Has been loaded by XML or SqlProvider or Mybatis's Annotation, so ignoring this injection for [" + getClass() + RIGHT_SQ_BRACKET);
return null;
}
/* 缓存逻辑处理 */
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
return builderAssistant.addMappedStatement(id, sqlSource, StatementType.PREPARED, sqlCommandType,
null, null, null, parameterType, resultMap, resultType,
null, !isSelect, isSelect, false, keyGenerator, keyProperty, keyColumn,
configuration.getDatabaseId(), languageDriver, null);
}

@Ai-010
Copy link

Ai-010 commented Dec 7, 2024

不是应该更多的去优化sql的效率,很少见去设置 timeout 和 fetchSize ; 不过 看源码 你是可以设置 事务时间

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

2 participants