You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
503 B

4 years ago
4 years ago
  1. package config
  2. import (
  3. "fmt"
  4. _ "github.com/go-sql-driver/mysql"
  5. "github.com/jmoiron/sqlx"
  6. "sync"
  7. )
  8. var dbx *sqlx.DB
  9. var dbLock sync.Mutex
  10. func DBX() *sqlx.DB {
  11. dbLock.Lock()
  12. defer dbLock.Unlock()
  13. if dbx == nil {
  14. dbx = sqlx.MustConnect("mysql", fmt.Sprintf(
  15. "%s:%s@(%s:%d)/%s?parseTime=true",
  16. MySqlUsername(),
  17. MySQlPassword(),
  18. MySqlHost(),
  19. MySqlPort(),
  20. MySqlSchema(),
  21. ))
  22. dbx.SetMaxIdleConns(20)
  23. dbx.SetMaxOpenConns(40)
  24. dbx.SetConnMaxLifetime(0)
  25. }
  26. return dbx
  27. }