add default configuration for MaxOpenConnCount for package gdb

This commit is contained in:
John
2020-11-10 21:19:15 +08:00
parent f8d57096a8
commit c273ce576b
5 changed files with 29 additions and 27 deletions

View File

@ -233,13 +233,14 @@ type (
)
const (
gINSERT_OPTION_DEFAULT = 0
gINSERT_OPTION_REPLACE = 1
gINSERT_OPTION_SAVE = 2
gINSERT_OPTION_IGNORE = 3
gDEFAULT_BATCH_NUM = 10 // Per count for batch insert/replace/save
gDEFAULT_CONN_MAX_IDLE_COUNT = 10 // Max idle connection count in pool.
gDEFAULT_CONN_MAX_LIFE_TIME = 30 // Max life time for per connection in pool in seconds.
insertOptionDefault = 0
insertOptionReplace = 1
insertOptionSave = 2
insertOptionIgnore = 3
defaultBatchNumber = 10 // Per count for batch insert/replace/save.
defaultMaxIdleConnCount = 10 // Max idle connection count in pool.
defaultMaxOpenConnCount = 100 // Max open connection count in pool.
defaultMaxConnLifeTime = 30 // Max life time for per connection in pool in seconds.
)
var (
@ -310,8 +311,9 @@ func New(group ...string) (db DB, err error) {
logger: glog.New(),
prefix: node.Prefix,
config: node,
maxIdleConnCount: gDEFAULT_CONN_MAX_IDLE_COUNT,
maxConnLifetime: gDEFAULT_CONN_MAX_LIFE_TIME, // Default max connection life time if user does not configure.
maxIdleConnCount: defaultMaxIdleConnCount,
maxOpenConnCount: defaultMaxOpenConnCount,
maxConnLifetime: defaultMaxConnLifeTime, // Default max connection life time if user does not configure.
}
if v, ok := driverMap[node.Type]; ok {
c.DB, err = v.New(c, node)

View File

@ -452,7 +452,7 @@ func (c *Core) DoInsert(link Link, table string, data interface{}, option int, b
values = append(values, "?")
params = append(params, v)
}
if option == gINSERT_OPTION_SAVE {
if option == insertOptionSave {
for k, _ := range dataMap {
// If it's SAVE operation,
// do not automatically update the creating time.
@ -599,7 +599,7 @@ func (c *Core) DoBatchInsert(link Link, table string, list interface{}, option i
operation = GetInsertOperationByOption(option)
updateStr = ""
)
if option == gINSERT_OPTION_SAVE {
if option == insertOptionSave {
for _, k := range keys {
// If it's SAVE operation,
// do not automatically update the creating time.
@ -617,7 +617,7 @@ func (c *Core) DoBatchInsert(link Link, table string, list interface{}, option i
}
updateStr = fmt.Sprintf("ON DUPLICATE KEY UPDATE %s", updateStr)
}
batchNum := gDEFAULT_BATCH_NUM
batchNum := defaultBatchNumber
if len(batch) > 0 && batch[0] > 0 {
batchNum = batch[0]
}

View File

@ -248,7 +248,7 @@ func (d *DriverOracle) DoInsert(link Link, table string, data interface{}, optio
indexs := make([]string, 0)
indexMap := make(map[string]string)
indexExists := false
if option != gINSERT_OPTION_DEFAULT {
if option != insertOptionDefault {
index, err := d.getTableUniqueIndex(table)
if err != nil {
return nil, err
@ -276,7 +276,7 @@ func (d *DriverOracle) DoInsert(link Link, table string, data interface{}, optio
k = strings.ToUpper(k)
// 操作类型为REPLACE/SAVE时且存在唯一索引才使用merge否则使用insert
if (option == gINSERT_OPTION_REPLACE || option == gINSERT_OPTION_SAVE) && indexExists {
if (option == insertOptionReplace || option == insertOptionSave) && indexExists {
fields = append(fields, tableAlias1+"."+charL+k+charR)
values = append(values, tableAlias2+"."+charL+k+charR)
params = append(params, v)
@ -302,18 +302,18 @@ func (d *DriverOracle) DoInsert(link Link, table string, data interface{}, optio
}
}
if indexExists && option != gINSERT_OPTION_DEFAULT {
if indexExists && option != insertOptionDefault {
switch option {
case gINSERT_OPTION_REPLACE:
case insertOptionReplace:
fallthrough
case gINSERT_OPTION_SAVE:
case insertOptionSave:
tmp := fmt.Sprintf(
"MERGE INTO %s %s USING(SELECT %s FROM DUAL) %s ON(%s) WHEN MATCHED THEN UPDATE SET %s WHEN NOT MATCHED THEN INSERT (%s) VALUES(%s)",
table, tableAlias1, strings.Join(subSqlStr, ","), tableAlias2,
strings.Join(onStr, "AND"), strings.Join(updateStr, ","), strings.Join(fields, ","), strings.Join(values, ","),
)
return d.DB.DoExec(link, tmp, params...)
case gINSERT_OPTION_IGNORE:
case insertOptionIgnore:
return d.DB.DoExec(link,
fmt.Sprintf(
"INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX(%s(%s)) */ INTO %s(%s) VALUES(%s)",
@ -391,7 +391,7 @@ func (d *DriverOracle) DoBatchInsert(link Link, table string, list interface{},
valueHolderStr := strings.Join(holders, ",")
// 当操作类型非insert时调用单笔的insert功能
if option != gINSERT_OPTION_DEFAULT {
if option != insertOptionDefault {
for _, v := range listMap {
r, err := d.DB.DoInsert(link, table, v, option, 1)
if err != nil {
@ -409,7 +409,7 @@ func (d *DriverOracle) DoBatchInsert(link Link, table string, list interface{},
}
// 构造批量写入数据格式(注意map的遍历是无序的)
batchNum := gDEFAULT_BATCH_NUM
batchNum := defaultBatchNumber
if len(batch) > 0 {
batchNum = batch[0]
}

View File

@ -88,9 +88,9 @@ func ListItemValuesUnique(list interface{}, key string, subKey ...interface{}) [
func GetInsertOperationByOption(option int) string {
var operator string
switch option {
case gINSERT_OPTION_REPLACE:
case insertOptionReplace:
operator = "REPLACE"
case gINSERT_OPTION_IGNORE:
case insertOptionIgnore:
operator = "INSERT IGNORE"
default:
operator = "INSERT"

View File

@ -101,7 +101,7 @@ func (m *Model) Insert(data ...interface{}) (result sql.Result, err error) {
if len(data) > 0 {
return m.Data(data...).Insert()
}
return m.doInsertWithOption(gINSERT_OPTION_DEFAULT, data...)
return m.doInsertWithOption(insertOptionDefault, data...)
}
// InsertIgnore does "INSERT IGNORE INTO ..." statement for the model.
@ -111,7 +111,7 @@ func (m *Model) InsertIgnore(data ...interface{}) (result sql.Result, err error)
if len(data) > 0 {
return m.Data(data...).Insert()
}
return m.doInsertWithOption(gINSERT_OPTION_IGNORE, data...)
return m.doInsertWithOption(insertOptionIgnore, data...)
}
// Replace does "REPLACE INTO ..." statement for the model.
@ -121,7 +121,7 @@ func (m *Model) Replace(data ...interface{}) (result sql.Result, err error) {
if len(data) > 0 {
return m.Data(data...).Replace()
}
return m.doInsertWithOption(gINSERT_OPTION_REPLACE, data...)
return m.doInsertWithOption(insertOptionReplace, data...)
}
// Save does "INSERT INTO ... ON DUPLICATE KEY UPDATE..." statement for the model.
@ -134,7 +134,7 @@ func (m *Model) Save(data ...interface{}) (result sql.Result, err error) {
if len(data) > 0 {
return m.Data(data...).Save()
}
return m.doInsertWithOption(gINSERT_OPTION_SAVE, data...)
return m.doInsertWithOption(insertOptionSave, data...)
}
// doInsertWithOption inserts data with option parameter.
@ -155,7 +155,7 @@ func (m *Model) doInsertWithOption(option int, data ...interface{}) (result sql.
)
// Batch operation.
if list, ok := m.data.(List); ok {
batch := gDEFAULT_BATCH_NUM
batch := defaultBatchNumber
if m.batch > 0 {
batch = m.batch
}