LoginSignup
0
0

MyBatis+Spring Bootでクエリ毎のタイムアウト時間を指定する

Last updated at Posted at 2023-06-10

通常はコネクションプールの設定でクエリタイムアウトを指定すると思いますが、特定のクエリだけタイムアウト時間を長くしたい!!みたいなケースが稀にあります。本投稿ではMyBatisを使っている場合の指定方法を紹介します。

バージョン

  • MyBatis 3.5.13
  • MyBatis Spring Boot 3.0.2
  • Spring Boot 3.1.0

XMLで指定する

SQLをXMLファイルに記載する場合は、 SQLタグのtimeout属性にタイムアウト時間を指定することができます。

<select id="select" resultType="int" timeout="30">
  SELECT 1
</select>

アノテーションで指定する

SQLをアノテーションに記載する場合は、 @Optionstimeout属性にタイムアウト時間を指定することができます。

@Options(timeout = 30)
@Select("SELECT 1")
int select();

プロパティで指定する

SQLをXMLファイルに記載する場合は、 SQLタグのtimeout属性に変数名を指定してプロパティから取得した値を埋め込むこともできます。

<select id="select" resultType="int" timeout="${timeout.select}">
  SELECT 1
</select>
application.yml
mybatis:
  configuration:
    variables:
      timeout:
        select: 30 # ★★★

mybatis.configuration.variables配下に変数(例:timeout.select)を指定する。

MyBatis 3.5.13時点ではアノテーションでSQLを記載する場合は、プロパティから取得した値にすることはできません。

プロパティにデフォルト値を指定する

プロパティ値を埋め込む際に、デフォルト値を指定することもできます。デフォルト値を指定する場合は、MyBatis本体の設定を変更する必要があります。

<select id="select" resultType="int" timeout="${timeout.select:30}">
  SELECT 1
</select>
application.yml
mybatis:
  configuration:
    variables:
      org:
        apache:
          ibatis:
            parsing:
              PropertyParser:
                enable-default-value: true # ★★★

mybatis.configuration.variables配下のorg.apache.ibatis.parsing.PropertyParser.enable-default-valuetrueにする。

参考ページ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0