【golang】gorm的一些特殊的配置

在观光一个go项目github.com/gotify/server的时候,它的两段代码引起了我的注意:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// 文件: database/database.go

    if dialect == "sqlite3" {
        // We use the database connection inside the handlers from the http
        // framework, therefore concurrent access occurs. Sqlite cannot handle
        // concurrent writes, so we limit sqlite to one connection.
        // see https://github.com/mattn/go-sqlite3/issues/274
        db.DB().SetMaxOpenConns(1)
    }

    if dialect == "mysql" {
        // Mysql has a setting called wait_timeout, which defines the duration
        // after which a connection may not be used anymore.
        // The default for this setting on mariadb is 10 minutes.
        // See https://github.com/docker-library/mariadb/issues/113
        db.DB().SetConnMaxLifetime(9 * time.Minute)
    }

使用sqlite的时候,连接数只能设置为1.

使用mysql的时候,连接最大时间为9分钟.

记录一下

Licensed under CC BY-NC-SA 4.0