StatementUtil.java

  1. /*
  2.  *    Copyright 2009-2024 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.executor.statement;

  17. import java.sql.SQLException;
  18. import java.sql.Statement;

  19. /**
  20.  * Utility for {@link java.sql.Statement}.
  21.  *
  22.  * @since 3.4.0
  23.  *
  24.  * @author Kazuki Shimizu
  25.  */
  26. public class StatementUtil {

  27.   private StatementUtil() {
  28.     // NOP
  29.   }

  30.   /**
  31.    * Apply a transaction timeout.
  32.    * <p>
  33.    * Update a query timeout to apply a transaction timeout.
  34.    *
  35.    * @param statement
  36.    *          a target statement
  37.    * @param queryTimeout
  38.    *          a query timeout
  39.    * @param transactionTimeout
  40.    *          a transaction timeout
  41.    *
  42.    * @throws SQLException
  43.    *           if a database access error occurs, this method is called on a closed <code>Statement</code>
  44.    */
  45.   public static void applyTransactionTimeout(Statement statement, Integer queryTimeout, Integer transactionTimeout)
  46.       throws SQLException {
  47.     if (transactionTimeout == null) {
  48.       return;
  49.     }
  50.     if (queryTimeout == null || queryTimeout == 0 || transactionTimeout < queryTimeout) {
  51.       statement.setQueryTimeout(transactionTimeout);
  52.     }
  53.   }

  54. }