update
parent
4c6d505685
commit
aa0fb37da2
|
@ -53,11 +53,11 @@ func Instance(key ...string) *gorm.DB {
|
|||
return instance
|
||||
}
|
||||
|
||||
func NewDefault(config *Config) (*gorm.DB, error) {
|
||||
return New(DefaultKey, config)
|
||||
func InitDefault(config *Config) error {
|
||||
return Init(DefaultKey, config)
|
||||
}
|
||||
|
||||
func New(key string, config *Config) (*gorm.DB, error) {
|
||||
func Init(key string, config *Config) error {
|
||||
var (
|
||||
maxLifeTime, _ = time.ParseDuration(DefaultConfig.MaxLifeTime)
|
||||
maxIdleTime, _ = time.ParseDuration(DefaultConfig.MaxIdleTime)
|
||||
|
@ -75,7 +75,7 @@ func New(key string, config *Config) (*gorm.DB, error) {
|
|||
if config.MaxLifeTime != "" {
|
||||
t, err := time.ParseDuration(config.MaxLifeTime)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse MaxLifeTime err: %s\n", err)
|
||||
return fmt.Errorf("parse MaxLifeTime err: %s\n", err)
|
||||
|
||||
}
|
||||
maxLifeTime = t
|
||||
|
@ -84,7 +84,7 @@ func New(key string, config *Config) (*gorm.DB, error) {
|
|||
if config.MaxIdleTime != "" {
|
||||
t, err := time.ParseDuration(config.MaxIdleTime)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse MaxIdleTime err: %s\n", err)
|
||||
return fmt.Errorf("parse MaxIdleTime err: %s\n", err)
|
||||
}
|
||||
maxIdleTime = t
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ func New(key string, config *Config) (*gorm.DB, error) {
|
|||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("connect mysql err: %s", err)
|
||||
return fmt.Errorf("connect mysql err: %s", err)
|
||||
}
|
||||
sqlDb, _ := db.DB()
|
||||
|
||||
|
@ -113,7 +113,7 @@ func New(key string, config *Config) (*gorm.DB, error) {
|
|||
sqlDb.SetConnMaxIdleTime(maxIdleTime)
|
||||
|
||||
instanceMap[key] = db
|
||||
return db, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func DefaultGormLogger(level gormLogger.LogLevel) gormLogger.Interface {
|
||||
|
|
|
@ -6,8 +6,8 @@ import (
|
|||
)
|
||||
|
||||
func TestMysql(t *testing.T) {
|
||||
myDB, err := NewDefault(&Config{
|
||||
Dsn: "root:root@tcp(127.0.0.1:3306)/site?loc=Local&charset=utf8mb4&writeTimeout=3s&readTimeout=3s&timeout=2s&parseTime=true",
|
||||
err := InitDefault(&Config{
|
||||
Dsn: "root:Tqa129126@tcp(119.29.187.200:3306)/site?loc=Local&charset=utf8mb4&writeTimeout=3s&readTimeout=3s&timeout=2s&parseTime=true",
|
||||
MaxOpenConn: 16,
|
||||
MaxIdleConn: 4,
|
||||
MaxIdleTime: "5m",
|
||||
|
@ -20,10 +20,10 @@ func TestMysql(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
defer myDB.Close()
|
||||
defer CloseAll()
|
||||
|
||||
var res = make(map[string]interface{})
|
||||
err = myDB.db.Table("image").Limit(1).Take(&res).Error
|
||||
err = Instance().Table("image").Limit(1).Take(&res).Error
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue