Commit 9cdd7cb0 by Daniel Lee

database: expose SetConnMaxLifetime as config setting

For MySQL, setting this to be shorter than the wait_timeout MySQL setting
solves the issue with connection errors after the session has timed out for
the connection to the database via xorm.
parent 5a3ba68a
...@@ -82,6 +82,9 @@ max_idle_conn = 2 ...@@ -82,6 +82,9 @@ max_idle_conn = 2
# Max conn setting default is 0 (mean not set) # Max conn setting default is 0 (mean not set)
max_open_conn = 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. # Set to true to log the sql calls and execution times.
log_queries = log_queries =
......
...@@ -90,6 +90,9 @@ ...@@ -90,6 +90,9 @@
# Max conn setting default is 0 (mean not set) # Max conn setting default is 0 (mean not set)
;max_open_conn = ;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. # Set to true to log the sql calls and execution times.
log_queries = log_queries =
......
...@@ -234,7 +234,12 @@ The maximum number of connections in the idle connection pool. ...@@ -234,7 +234,12 @@ The maximum number of connections in the idle connection pool.
### max_open_conn ### max_open_conn
The maximum number of open connections to the database. 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 ### log_queries
Set to `true` to log the sql calls and execution times. Set to `true` to log the sql calls and execution times.
<hr /> <hr />
......
...@@ -34,6 +34,7 @@ type DatabaseConfig struct { ...@@ -34,6 +34,7 @@ type DatabaseConfig struct {
ServerCertName string ServerCertName string
MaxOpenConn int MaxOpenConn int
MaxIdleConn int MaxIdleConn int
ConnMaxLifetime int
} }
var ( var (
...@@ -158,9 +159,11 @@ func getEngine() (*xorm.Engine, error) { ...@@ -158,9 +159,11 @@ func getEngine() (*xorm.Engine, error) {
engine, err := xorm.NewEngine(DbCfg.Type, cnnstr) engine, err := xorm.NewEngine(DbCfg.Type, cnnstr)
if err != nil { if err != nil {
return nil, err return nil, err
} else { }
engine.SetMaxOpenConns(DbCfg.MaxOpenConn) engine.SetMaxOpenConns(DbCfg.MaxOpenConn)
engine.SetMaxIdleConns(DbCfg.MaxIdleConn) engine.SetMaxIdleConns(DbCfg.MaxIdleConn)
engine.SetConnMaxLifetime(time.Second * time.Duration(DbCfg.ConnMaxLifetime))
debugSql := setting.Cfg.Section("database").Key("log_queries").MustBool(false) debugSql := setting.Cfg.Section("database").Key("log_queries").MustBool(false)
if !debugSql { if !debugSql {
engine.SetLogger(&xorm.DiscardLogger{}) engine.SetLogger(&xorm.DiscardLogger{})
...@@ -169,7 +172,7 @@ func getEngine() (*xorm.Engine, error) { ...@@ -169,7 +172,7 @@ func getEngine() (*xorm.Engine, error) {
engine.ShowSQL(true) engine.ShowSQL(true)
engine.ShowExecTime(true) engine.ShowExecTime(true)
} }
}
return engine, nil return engine, nil
} }
...@@ -203,6 +206,7 @@ func LoadConfig() { ...@@ -203,6 +206,7 @@ func LoadConfig() {
} }
DbCfg.MaxOpenConn = sec.Key("max_open_conn").MustInt(0) DbCfg.MaxOpenConn = sec.Key("max_open_conn").MustInt(0)
DbCfg.MaxIdleConn = sec.Key("max_idle_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" { if DbCfg.Type == "sqlite3" {
UseSQLite3 = true UseSQLite3 = true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment