mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-23 14:53:10 -06:00
Reuse existing database connections
This commit is contained in:
parent
eb25ba72ef
commit
12ce66dee2
|
|
@ -96,10 +96,16 @@ func trackGoID(query string) {
|
|||
logrus.Warnf("unsafe goid: SQL executed not on an ExclusiveWriter: %s", q)
|
||||
}
|
||||
|
||||
var dbConns = make(map[string]*sql.DB)
|
||||
|
||||
// Open opens a database specified by its database driver name and a driver-specific data source name,
|
||||
// usually consisting of at least a database name and connection information. Includes tracing driver
|
||||
// if DENDRITE_TRACE_SQL=1
|
||||
func Open(dbProperties *config.DatabaseOptions) (*sql.DB, error) {
|
||||
if conn, ok := dbConns[string(dbProperties.ConnectionString)]; ok {
|
||||
logrus.Debug("Reusing existing database connection")
|
||||
return conn, nil
|
||||
}
|
||||
var err error
|
||||
var driverName, dsn string
|
||||
switch {
|
||||
|
|
@ -134,6 +140,7 @@ func Open(dbProperties *config.DatabaseOptions) (*sql.DB, error) {
|
|||
db.SetMaxIdleConns(dbProperties.MaxIdleConns())
|
||||
db.SetConnMaxLifetime(dbProperties.ConnMaxLifetime())
|
||||
}
|
||||
dbConns[string(dbProperties.ConnectionString)] = db
|
||||
return db, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue