LoginSignup
10
12

More than 5 years have passed since last update.

Spring Bootでmysqlのコネクションが切断された時に自動的に張りなおす方法

Posted at

概要

Spring Bootで、jpaを使ってmysqlに接続しているときに、10分(600秒)でコネクションが切断されてしまって、DBの接続エラーが発生する事態があったので対応方法をメモ。

application.yml

すみません、例がmariadbです。

spring:
  datasource:
    url: jdbc:mariadb://xxx.xxx.xxx.xxx:3306/testdb
    username: testuser
    password: testpassword
    driverClassName: org.mariadb.jdbc.Driver
    testOnBorrow: true
    validationQuery: SELECT 1

validationQueryだけでいけるかと思ったけど、testOnBorrowの設定が必要でした。

参考

http://yyhayashi303.hatenablog.com/entry/20120911/1347322683
http://stackoverflow.com/questions/22684807/spring-boot-jpa-configuring-auto-reconnect

なぞ

mysqlのコネクションが10分(600秒)で切断されてしまう事象は謎。
8時間(28800秒)で設定されてそうなんだが。

mysql> show variables like '%timeout%';
+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 5        |
| deadlock_timeout_long       | 50000000 |
| deadlock_timeout_short      | 10000    |
| delayed_insert_timeout      | 300      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 50       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         | 28800    |
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| slave_net_timeout           | 3600     |
| thread_pool_idle_timeout    | 60       |
| wait_timeout                | 28800    |
+-----------------------------+----------+
14 rows in set (0.37 sec)
10
12
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
10
12