mybatis-plus全局控制
- mybatis-plus:
- configuration:
- # 单个SQL执行超时时间(含insert,delete,select),一般应用不宜过长,单位秒
- default-statement-timeout: 30
复制代码
- ### Cause: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
- ; Statement cancelled due to timeout or client request; nested exception is com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
- at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:76)
- at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
- at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
- at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:91)
- at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
- at com.sun.proxy.$Proxy133.selectList(Unknown Source)
- at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224)
复制代码 mybatis单个SQL控制
- <insert
- id="insert"
- parameterType="com.test.abc.Person"
- ...
- timeout="30">
- ...
- </insert>
复制代码
- @Mapper
- public interface DemoMapper extends BaseMapper<Demo> {
- @Select("SELECT * FROM DEMO")
- @Options(timeout = 30) // 设置超时时间为 30 秒
- List<Demo> select();
- }
复制代码
spring事务超时控制
- 整个事务的超时时间,单元是秒。
- 它的原理大抵是事务启动时,事务上下文会计算一个到期时间deadLine(当前时间+超时时间),当mybatis -> prepareStatement时,会调用 SpringManagedTransaction 的getTimeOut,该方法会计算事务剩余时间timeToLiveInSeconds(deadLine-System.currentTimeMillis()),如果timeToLiveInSeconds小于0则报错TransactionTimedOutException,大于0根据情况设置为statement 的queryTimeOut (参照下文的源码分析)
情况1:事务剩余时间 timeToLiveInSeconds小于 statement 本身的 queryTimeOut,则 statement 的 setQueryTimeout 设置为 timeToLiveInSeconds
情况2: 事务剩余时间 timeToLiveInSeconds大于 statement 本身的 queryTimeOut,则 statement 的 setQueryTimeout 设置为 queryTimeOut
- @Transactional(timeout = 15)
- public int test(){
- demoMapper.select();
- demoMapper.insert(...);
-
- // 其他数据库操作
- }
复制代码 根据它的原理及测试,大概会出现以下两种非常。
- 事务超时非常: TransactionTimedOutException
- org.springframework.transaction.TransactionTimedOutException: Transaction timed out: deadline was Thu Dec 14 16:06:47 CST 2023
- at org.springframework.transaction.support.ResourceHolderSupport.checkTransactionTimeout(ResourceHolderSupport.java:155)
- at org.springframework.transaction.support.ResourceHolderSupport.getTimeToLiveInMillis(ResourceHolderSupport.java:144)
- at org.springframework.transaction.support.ResourceHolderSupport.getTimeToLiveInSeconds(ResourceHolderSupport.java:128)
- at org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout(SpringManagedTransaction.java:125)
- at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:87)
- at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
复制代码
- statement实行超时非常: MySQLTimeoutException
- com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
- at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:113)
- at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:955)
- at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:372)
- at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)
- at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java)
- at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
- at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.base/java.lang.reflect.Method.invoke(Method.java:566)
- at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59)
- at com.sun.proxy.$Proxy197.execute(Unknown Source)
- at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64)
复制代码 JDBC也可以设置socket超时时间
- 设置socketTimeout 单元毫秒,一般不推荐配这个
- jdbc:mysql://localhost:3066/testdb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
- &socketTimeout=60000
复制代码 总结
mybatis 实行SQL超时,底层也是用了 Jdbc 的 Statment 的 setQueryTimeout
mybatis设置超时源码(spring5.2.15+mybatis3.5.9)
BaseStatementHandler.java
- public abstract class BaseStatementHandler implements StatementHandler {
- 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);
- }
复制代码 StatementUtil.java
- public class StatementUtil {
- private StatementUtil() {
- // NOP
- }
- /**
- * Apply a transaction timeout.
- * <p>
- * Update a query timeout to apply a transaction timeout.
- * </p>
- * @param statement a target statement
- * @param queryTimeout a query timeout
- * @param transactionTimeout a transaction timeout
- * @throws SQLException if a database access error occurs, this method is called on a closed <code>Statement</code>
- */
- public static void applyTransactionTimeout(Statement statement, Integer queryTimeout, Integer transactionTimeout) throws SQLException {
- if (transactionTimeout == null) {
- return;
- }
- // 事务剩余时间小于 statement自己的queryTimeout,就用事务的剩余时间,所以在事务里面statement的超时时间是动态的。
- if (queryTimeout == null || queryTimeout == 0 || transactionTimeout < queryTimeout) {
- statement.setQueryTimeout(transactionTimeout);
- }
- }
- }
复制代码 jdbc 测试代码
- try (Connection connection = dataSource.getConnection()) {
- // 创建 Statement 对象
- Statement statement = connection.createStatement();
- // 设置查询超时时间为 10 秒
- int timeoutInSeconds = 10;
- statement.setQueryTimeout(timeoutInSeconds);
- // 执行查询
- String sqlQuery = "SELECT sleep(12)";
- ResultSet resultSet = statement.executeQuery(sqlQuery);
- // 处理结果集
- while (resultSet.next()) {
- // 处理每一行数据
- }
- resultSet.close();
- statement.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |