diff --git a/conf/defaults.ini b/conf/defaults.ini
index 4a2240f1924..557c5e49ee1 100644
--- a/conf/defaults.ini
+++ b/conf/defaults.ini
@@ -82,6 +82,9 @@ max_idle_conn = 2
# Max conn setting default is 0 (mean not set)
max_open_conn =
+# Connection Max Lifetime default is 14400 (means 14400 seconds or 4 hours)
+conn_max_lifetime = 14400
+
# Set to true to log the sql calls and execution times.
log_queries =
diff --git a/conf/sample.ini b/conf/sample.ini
index 3e45ac44d61..fa30c301014 100644
--- a/conf/sample.ini
+++ b/conf/sample.ini
@@ -90,6 +90,9 @@
# Max conn setting default is 0 (mean not set)
;max_open_conn =
+# Connection Max Lifetime default is 14400 (means 14400 seconds or 4 hours)
+;conn_max_lifetime = 14400
+
# Set to true to log the sql calls and execution times.
log_queries =
diff --git a/docs/sources/installation/configuration.md b/docs/sources/installation/configuration.md
index 66072a98f84..6169280b798 100644
--- a/docs/sources/installation/configuration.md
+++ b/docs/sources/installation/configuration.md
@@ -234,7 +234,12 @@ The maximum number of connections in the idle connection pool.
### max_open_conn
The maximum number of open connections to the database.
+### conn_max_lifetime
+
+Sets the maximum amount of time a connection may be reused. The default is 14400 (which means 14400 seconds or 4 hours). For MySQL, this setting should be shorter than the [`wait_timeout`](https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_wait_timeout) variable.
+
### log_queries
+
Set to `true` to log the sql calls and execution times.
diff --git a/pkg/services/sqlstore/sqlstore.go b/pkg/services/sqlstore/sqlstore.go
index 493243f5185..ae1e3fc482a 100644
--- a/pkg/services/sqlstore/sqlstore.go
+++ b/pkg/services/sqlstore/sqlstore.go
@@ -34,6 +34,7 @@ type DatabaseConfig struct {
ServerCertName string
MaxOpenConn int
MaxIdleConn int
+ ConnMaxLifetime int
}
var (
@@ -158,18 +159,20 @@ func getEngine() (*xorm.Engine, error) {
engine, err := xorm.NewEngine(DbCfg.Type, cnnstr)
if err != nil {
return nil, err
- } else {
- engine.SetMaxOpenConns(DbCfg.MaxOpenConn)
- engine.SetMaxIdleConns(DbCfg.MaxIdleConn)
- debugSql := setting.Cfg.Section("database").Key("log_queries").MustBool(false)
- if !debugSql {
- engine.SetLogger(&xorm.DiscardLogger{})
- } else {
- engine.SetLogger(NewXormLogger(log.LvlInfo, log.New("sqlstore.xorm")))
- engine.ShowSQL(true)
- engine.ShowExecTime(true)
- }
}
+
+ engine.SetMaxOpenConns(DbCfg.MaxOpenConn)
+ engine.SetMaxIdleConns(DbCfg.MaxIdleConn)
+ engine.SetConnMaxLifetime(time.Second * time.Duration(DbCfg.ConnMaxLifetime))
+ debugSql := setting.Cfg.Section("database").Key("log_queries").MustBool(false)
+ if !debugSql {
+ engine.SetLogger(&xorm.DiscardLogger{})
+ } else {
+ engine.SetLogger(NewXormLogger(log.LvlInfo, log.New("sqlstore.xorm")))
+ engine.ShowSQL(true)
+ engine.ShowExecTime(true)
+ }
+
return engine, nil
}
@@ -203,6 +206,7 @@ func LoadConfig() {
}
DbCfg.MaxOpenConn = sec.Key("max_open_conn").MustInt(0)
DbCfg.MaxIdleConn = sec.Key("max_idle_conn").MustInt(0)
+ DbCfg.ConnMaxLifetime = sec.Key("conn_max_lifetime").MustInt(14400)
if DbCfg.Type == "sqlite3" {
UseSQLite3 = true