初始化项目
This commit is contained in:
146
trunk/goutil/mysqlUtil/convert.go
Normal file
146
trunk/goutil/mysqlUtil/convert.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package mysqlUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 转换数据库连接字符串(从C#->Go)
|
||||
// connString:C#的数据库连接字符串
|
||||
// 返回值:
|
||||
// Go的数据库连接字符串
|
||||
func ConvertConnectionStringFromCSharpToGo(connString string) string {
|
||||
goConnString := "{userid}:{password}@tcp({datasource}:{port})/{database}?charset={charset}&parseTime=true&loc=Local&timeout={timeout}s||MaxOpenConns={MaxOpenConns}||MaxIdleConns={MaxIdleConns}"
|
||||
|
||||
// 将字符串按;进行切割
|
||||
connStringList := strings.Split(connString, ";")
|
||||
var datasource string // DataSource=10.162.2.205;
|
||||
var port string // port=3306;
|
||||
var userid string // UserId=admin;
|
||||
var password string // Password=MOQIkaka$#@!1234;
|
||||
var database string // Database=s201_dzz_log;
|
||||
var charset string // charset=utf8;
|
||||
var pooling string // pooling=true;
|
||||
var minimumpoolsize string // MinimumPoolSize=20; or min pool size=20;
|
||||
var maximumpoolsize string // maximumpoolsize=200; or max pool size=200;
|
||||
var commandtimeout string // command timeout=60;
|
||||
|
||||
// 遍历处理
|
||||
for _, item := range connStringList {
|
||||
// 将字符串按=进行切割
|
||||
subItemList := strings.Split(item, "=")
|
||||
|
||||
// 对每一项进行判断
|
||||
if len(subItemList) != 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 先转换为小写字母
|
||||
switch strings.ToLower(subItemList[0]) {
|
||||
case "datasource":
|
||||
datasource = subItemList[1]
|
||||
case "port":
|
||||
port = subItemList[1]
|
||||
case "userid":
|
||||
userid = subItemList[1]
|
||||
case "password":
|
||||
password = subItemList[1]
|
||||
case "database":
|
||||
database = subItemList[1]
|
||||
case "charset":
|
||||
charset = subItemList[1]
|
||||
case "pooling":
|
||||
pooling = subItemList[1]
|
||||
case "minimumpoolsize", "min pool size":
|
||||
minimumpoolsize = subItemList[1]
|
||||
case "maximumpoolsize", "max pool size":
|
||||
maximumpoolsize = subItemList[1]
|
||||
case "command timeout":
|
||||
commandtimeout = subItemList[1]
|
||||
}
|
||||
}
|
||||
|
||||
// 替换占位符
|
||||
goConnString = strings.Replace(goConnString, "{userid}", userid, 1)
|
||||
goConnString = strings.Replace(goConnString, "{password}", password, 1)
|
||||
goConnString = strings.Replace(goConnString, "{datasource}", datasource, 1)
|
||||
goConnString = strings.Replace(goConnString, "{port}", port, 1)
|
||||
goConnString = strings.Replace(goConnString, "{database}", database, 1)
|
||||
goConnString = strings.Replace(goConnString, "{charset}", charset, 1)
|
||||
goConnString = strings.Replace(goConnString, "{timeout}", commandtimeout, 1)
|
||||
if pooling == "true" {
|
||||
goConnString = strings.Replace(goConnString, "{MaxOpenConns}", maximumpoolsize, 1)
|
||||
goConnString = strings.Replace(goConnString, "{MaxIdleConns}", minimumpoolsize, 1)
|
||||
} else {
|
||||
goConnString = strings.Replace(goConnString, "{MaxOpenConns}", "0", 1)
|
||||
goConnString = strings.Replace(goConnString, "{MaxIdleConns}", "0", 1)
|
||||
}
|
||||
|
||||
return goConnString
|
||||
}
|
||||
|
||||
// 解析连接字符串(obsolete,建议使用NewDBConfig2)
|
||||
// connString:数据库连接字符串
|
||||
// 返回值
|
||||
// 有效的连接字符串
|
||||
// 最大开启连接数量
|
||||
// 最大空闲连接数量
|
||||
// 错误对象
|
||||
func ParseConnectionString(connString string) (conn string, maxOpenConns int, maxIdleConns int, err error) {
|
||||
connSlice := strings.Split(connString, "||")
|
||||
length := len(connSlice)
|
||||
if length != 1 && length != 3 {
|
||||
err = fmt.Errorf("connString:%s格式不正确,length:%d", connString, length)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取连接字符串
|
||||
conn = connSlice[0]
|
||||
if conn == "" {
|
||||
err = fmt.Errorf("connString:%s格式不正确,length:%d", connString, length)
|
||||
return
|
||||
}
|
||||
|
||||
// 如果只配置了连接字符串,则MaxOpenConns、MaxIdleConns取默认值
|
||||
if length == 1 {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取连接池相关
|
||||
maxOpenConns_string := strings.Replace(connSlice[1], "MaxOpenConns=", "", 1)
|
||||
maxOpenConns, err = strconv.Atoi(maxOpenConns_string)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("MaxOpenConns必须为int型,连接字符串为:%s", connString)
|
||||
return
|
||||
}
|
||||
|
||||
maxIdleConns_string := strings.Replace(connSlice[2], "MaxIdleConns=", "", 1)
|
||||
maxIdleConns, err = strconv.Atoi(maxIdleConns_string)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("MaxIdleConns必须为int型,连接字符串为:%s", connString)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 连接字符串是否为C#格式
|
||||
func IsCSharpStyle(connString string) bool {
|
||||
lowerString := strings.ToLower(connString)
|
||||
if strings.Contains(lowerString, "datasource") && strings.Contains(lowerString, "port") {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 连接字符串是否为Go格式
|
||||
func IsGoStyle(connString string) bool {
|
||||
lowerString := strings.ToLower(connString)
|
||||
if strings.Contains(lowerString, "@tcp") {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
45
trunk/goutil/mysqlUtil/convert_test.go
Normal file
45
trunk/goutil/mysqlUtil/convert_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package mysqlUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConvertConnectionStringFromCSharpToGo(t *testing.T) {
|
||||
csharp := "DataSource=10.66.195.134;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=s9501_sd_log;Allow Zero Datetime=true;charset=utf8;pooling=false;command timeout=60;AllowUserVariables=True;"
|
||||
expected := "admin:MOQIkaka$#@!1234@tcp(10.66.195.134:3306)/s9501_sd_log?charset=utf8&parseTime=true&loc=Local&timeout=60s||MaxOpenConns=0||MaxIdleConns=0"
|
||||
|
||||
if goConn := ConvertConnectionStringFromCSharpToGo(csharp); goConn != expected {
|
||||
t.Errorf("Expected %s, but got %s", expected, goConn)
|
||||
}
|
||||
|
||||
// csharp = "DataSource=10.162.2.205;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=s201_dzz_log;Allow Zero Datetime=true;charset=utf8;pooling=false;min pool size=20;max pool size=200;command timeout=60;AllowUserVariables=True;"
|
||||
// expected = "admin:MOQIkaka$#@!1234@tcp(10.162.2.205:3306)/s201_dzz_log?charset=utf8&parseTime=true&loc=Local&timeout=60s||MaxOpenConns=0||MaxIdleConns=0"
|
||||
|
||||
// if goConn := ConvertConnectionStringFromCSharpToGo(csharp); goConn != expected {
|
||||
// t.Errorf("Expected %s, but got %s", expected, goConn)
|
||||
// }
|
||||
}
|
||||
|
||||
func TestIsCSharpStyle(t *testing.T) {
|
||||
connString := "DataSource=10.66.195.134;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=s9501_sd_log;Allow Zero Datetime=true;charset=utf8;pooling=false;command timeout=60;AllowUserVariables=True;"
|
||||
if !IsCSharpStyle(connString) {
|
||||
t.Errorf("it's should be C# style, but now not")
|
||||
}
|
||||
|
||||
connString = "admin:MOQIkaka$#@!1234@tcp(10.66.195.134:3306)/s9501_sd_log?charset=utf8&parseTime=true&loc=Local&timeout=60s||MaxOpenConns=0||MaxIdleConns=0"
|
||||
if IsCSharpStyle(connString) {
|
||||
t.Errorf("it's should not be C# style, but now it is")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsGoStyle(t *testing.T) {
|
||||
connString := "admin:MOQIkaka$#@!1234@tcp(10.66.195.134:3306)/s9501_sd_log?charset=utf8&parseTime=true&loc=Local&timeout=60s||MaxOpenConns=0||MaxIdleConns=0"
|
||||
if !IsGoStyle(connString) {
|
||||
t.Errorf("it's should be Go style, but now not")
|
||||
}
|
||||
|
||||
connString = "DataSource=10.66.195.134;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=s9501_sd_log;Allow Zero Datetime=true;charset=utf8;pooling=false;command timeout=60;AllowUserVariables=True;"
|
||||
if IsGoStyle(connString) {
|
||||
t.Errorf("it's should not be Go style, but now it is")
|
||||
}
|
||||
}
|
||||
85
trunk/goutil/mysqlUtil/dbConfig.go
Normal file
85
trunk/goutil/mysqlUtil/dbConfig.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package mysqlUtil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 数据库配置对象
|
||||
type DBConfig struct {
|
||||
// 连接字符串
|
||||
ConnectionString string
|
||||
|
||||
// 最大开启连接数量
|
||||
MaxOpenConns int
|
||||
|
||||
// 最大空闲连接数量
|
||||
MaxIdleConns int
|
||||
}
|
||||
|
||||
func (this *DBConfig) String() string {
|
||||
bytes, _ := json.Marshal(this)
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
// 创建数据库配置对象
|
||||
// connectionString:连接字符串
|
||||
// maxOpenConns:最大开启连接数
|
||||
// maxIdleConns:最大空闲连接数
|
||||
// 返回值:
|
||||
// 数据库配置对象
|
||||
func NewDBConfig(connectionString string, maxOpenConns, maxIdleConns int) *DBConfig {
|
||||
return &DBConfig{
|
||||
ConnectionString: connectionString,
|
||||
MaxOpenConns: maxOpenConns,
|
||||
MaxIdleConns: maxIdleConns,
|
||||
}
|
||||
}
|
||||
|
||||
// 创建数据库配置对象
|
||||
// dbConfigStr:数据库配置字符串,格式:root:moqikaka3306@tcp(10.1.0.10:3306)/gameserver_data?charset=utf8&parseTime=true&loc=Local&timeout=60s||MaxOpenConns=10||MaxIdleConns=5
|
||||
// 返回值:
|
||||
// 数据库配置对象
|
||||
// 错误对象
|
||||
func NewDBConfig2(dbConfigStr string) (*DBConfig, error) {
|
||||
connectionString := ""
|
||||
maxOpenConns := 0
|
||||
maxIdleConns := 0
|
||||
|
||||
// 按照||来进行分割
|
||||
connSlice := strings.Split(dbConfigStr, "||")
|
||||
length := len(connSlice)
|
||||
if length != 1 && length != 3 {
|
||||
return nil, fmt.Errorf("dbConfigStr:%s格式不正确,length:%d", dbConfigStr, length)
|
||||
}
|
||||
|
||||
// 获取连接字符串
|
||||
connectionString = connSlice[0]
|
||||
if connectionString == "" {
|
||||
return nil, fmt.Errorf("dbConfigStr:%s格式不正确,length:%d", dbConfigStr, length)
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
// 如果配置了MaxOpenConns、MaxIdleConns,则进行解析
|
||||
if length == 3 {
|
||||
// 获取连接池相关
|
||||
maxOpenConns_string := strings.Replace(connSlice[1], "MaxOpenConns=", "", 1)
|
||||
maxOpenConns, err = strconv.Atoi(maxOpenConns_string)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("MaxOpenConns必须为int型,连接字符串为:%s", connectionString)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
maxIdleConns_string := strings.Replace(connSlice[2], "MaxIdleConns=", "", 1)
|
||||
maxIdleConns, err = strconv.Atoi(maxIdleConns_string)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("MaxIdleConns必须为int型,连接字符串为:%s", connectionString)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return NewDBConfig(connectionString, maxOpenConns, maxIdleConns), nil
|
||||
}
|
||||
54
trunk/goutil/mysqlUtil/dbConfig_test.go
Normal file
54
trunk/goutil/mysqlUtil/dbConfig_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package mysqlUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewDBConfig(t *testing.T) {
|
||||
connectionString := "root:moqikaka3306@tcp(10.1.0.10:3306)/gameserver_data?charset=utf8&parseTime=true&loc=Local&timeout=60s"
|
||||
maxOpenConns := 10
|
||||
maxIdleConns := 5
|
||||
|
||||
dbConfigObj := NewDBConfig(connectionString, maxOpenConns, maxIdleConns)
|
||||
if connectionString != dbConfigObj.ConnectionString {
|
||||
t.Errorf("Expected %s, but now %s", connectionString, dbConfigObj.ConnectionString)
|
||||
}
|
||||
|
||||
if maxOpenConns != dbConfigObj.MaxOpenConns {
|
||||
t.Errorf("Expected %d, but now %d", maxOpenConns, dbConfigObj.MaxOpenConns)
|
||||
}
|
||||
|
||||
if maxIdleConns != dbConfigObj.MaxIdleConns {
|
||||
t.Errorf("Expected %d, but now %d", maxIdleConns, dbConfigObj.MaxIdleConns)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDBConfig2(t *testing.T) {
|
||||
dbConfigStr := "root:moqikaka3306@tcp(10.1.0.10:3306)/gameserver_data?charset=utf8&parseTime=true&loc=Local&timeout=60s||MaxOpenConns=10||MaxIdleConns2=5"
|
||||
|
||||
if _, err := NewDBConfig2(dbConfigStr); err == nil {
|
||||
t.Errorf("there should be err, but now not.")
|
||||
}
|
||||
|
||||
dbConfigStr = "root:moqikaka3306@tcp(10.1.0.10:3306)/gameserver_data?charset=utf8&parseTime=true&loc=Local&timeout=60s||MaxOpenConns=10||MaxIdleConns=5"
|
||||
dbConfigObj, err := NewDBConfig2(dbConfigStr)
|
||||
if err != nil {
|
||||
t.Errorf("there should be no err, but now has.")
|
||||
}
|
||||
|
||||
connectionString := "root:moqikaka3306@tcp(10.1.0.10:3306)/gameserver_data?charset=utf8&parseTime=true&loc=Local&timeout=60s"
|
||||
maxOpenConns := 10
|
||||
maxIdleConns := 5
|
||||
|
||||
if connectionString != dbConfigObj.ConnectionString {
|
||||
t.Errorf("Expected %s, but now %s", connectionString, dbConfigObj.ConnectionString)
|
||||
}
|
||||
|
||||
if maxOpenConns != dbConfigObj.MaxOpenConns {
|
||||
t.Errorf("Expected %d, but now %d", maxOpenConns, dbConfigObj.MaxOpenConns)
|
||||
}
|
||||
|
||||
if maxIdleConns != dbConfigObj.MaxIdleConns {
|
||||
t.Errorf("Expected %d, but now %d", maxIdleConns, dbConfigObj.MaxIdleConns)
|
||||
}
|
||||
}
|
||||
137
trunk/goutil/mysqlUtil/dbConn.go
Normal file
137
trunk/goutil/mysqlUtil/dbConn.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package mysqlUtil
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"goutil/logUtil"
|
||||
)
|
||||
|
||||
// 打开数据库连接
|
||||
// connectionString:数据库连接字符串,格式:root:moqikaka3306@tcp(10.1.0.10:3306)/gameserver_data?charset=utf8&parseTime=true&loc=Local&timeout=60s||MaxOpenConns=10||MaxIdleConns=5
|
||||
// 返回值:
|
||||
// 数据库对象
|
||||
// 错误对象
|
||||
func OpenMysqlConnection(connectionString string) (dbObj *sql.DB, err error) {
|
||||
dbConfigObj, err1 := NewDBConfig2(connectionString)
|
||||
if err1 != nil {
|
||||
err = err1
|
||||
return
|
||||
}
|
||||
|
||||
dbObj, err = OpenMysqlConnection3(dbConfigObj)
|
||||
return
|
||||
}
|
||||
|
||||
// 打开数据库连接
|
||||
// connectionString:数据库连接字符串
|
||||
// maxOpenConns:最大打开的连接数
|
||||
// maxIdleConns:最大处于闲置状态的连接数
|
||||
// 返回值:
|
||||
// 数据库对象
|
||||
// 错误对象
|
||||
func OpenMysqlConnection2(connectionString string, maxOpenConns, maxIdleConns int) (dbObj *sql.DB, err error) {
|
||||
dbConfigObj := NewDBConfig(connectionString, maxOpenConns, maxIdleConns)
|
||||
dbObj, err = OpenMysqlConnection3(dbConfigObj)
|
||||
return
|
||||
}
|
||||
|
||||
// 建立Mysql数据库连接
|
||||
// dbConfigObj:数据库配置对象
|
||||
// 返回值:
|
||||
// 数据库对象
|
||||
// 错误对象
|
||||
func OpenMysqlConnection3(dbConfigObj *DBConfig) (dbObj *sql.DB, err error) {
|
||||
// 建立数据库连接
|
||||
logUtil.DebugLog("开始连接Mysql数据库")
|
||||
dbObj, err = sql.Open("mysql", dbConfigObj.ConnectionString)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("打开游戏数据库失败,连接字符串为:%s", dbConfigObj.ConnectionString)
|
||||
return
|
||||
}
|
||||
logUtil.DebugLog("连接Mysql数据库成功")
|
||||
|
||||
if dbConfigObj.MaxOpenConns > 0 && dbConfigObj.MaxIdleConns > 0 {
|
||||
dbObj.SetMaxOpenConns(dbConfigObj.MaxOpenConns)
|
||||
dbObj.SetMaxIdleConns(dbConfigObj.MaxIdleConns)
|
||||
}
|
||||
|
||||
if err = dbObj.Ping(); err != nil {
|
||||
err = fmt.Errorf("Ping数据库失败,连接字符串为:%s,错误信息为:%s", dbConfigObj.ConnectionString, err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 测试数据库连接
|
||||
// dbObj:数据库连对象
|
||||
// 返回值:
|
||||
// 错误对象
|
||||
func TestConnection(dbObj *sql.DB) error {
|
||||
command := "SHOW DATABASES;"
|
||||
rows, err := dbObj.Query(command)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
// db:数据库对象
|
||||
// 返回值:
|
||||
// 事务对象
|
||||
// 错误对象
|
||||
func BeginTransaction(dbObj *sql.DB) (*sql.Tx, error) {
|
||||
tx, err := dbObj.Begin()
|
||||
if err != nil {
|
||||
logUtil.Log(fmt.Sprintf("开启事务失败,错误信息:%s", err), logUtil.Error, true)
|
||||
}
|
||||
|
||||
return tx, err
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
// tx:事务对象
|
||||
// 返回值:
|
||||
// 错误对象
|
||||
func CommitTransaction(tx *sql.Tx) error {
|
||||
err := tx.Commit()
|
||||
if err != nil {
|
||||
logUtil.Log(fmt.Sprintf("提交事务失败,错误信息:%s", err), logUtil.Error, true)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// 记录Prepare错误
|
||||
// command:执行的SQL语句
|
||||
// err:错误对象
|
||||
func WritePrepareError(command string, err error) {
|
||||
logUtil.Log(fmt.Sprintf("Prepare失败,错误信息:%s,command:%s", err, command), logUtil.Error, true)
|
||||
}
|
||||
|
||||
// 记录Query错误
|
||||
// command:执行的SQL语句
|
||||
// err:错误对象
|
||||
func WriteQueryError(command string, err error) {
|
||||
logUtil.Log(fmt.Sprintf("Query失败,错误信息:%s,command:%s", err, command), logUtil.Error, true)
|
||||
}
|
||||
|
||||
// 记录Exec错误
|
||||
// command:执行的SQL语句
|
||||
// err:错误对象
|
||||
func WriteExecError(command string, err error) {
|
||||
logUtil.Log(fmt.Sprintf("Exec失败,错误信息:%s,command:%s", err, command), logUtil.Error, true)
|
||||
}
|
||||
|
||||
// 记录Scan错误
|
||||
// command:执行的SQL语句
|
||||
// err:错误对象
|
||||
func WriteScanError(command string, err error) {
|
||||
logUtil.Log(fmt.Sprintf("Scan失败,错误信息:%s,command:%s", err, command), logUtil.Error, true)
|
||||
}
|
||||
56
trunk/goutil/mysqlUtil/dbConn_test.go
Normal file
56
trunk/goutil/mysqlUtil/dbConn_test.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package mysqlUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestOpenMysqlConnection(t *testing.T) {
|
||||
connectionString := "root:moqikaka3306@tcp(10.1.0.10:3306)/gameserver_data?charset=utf8&parseTime=true&loc=Local&timeout=60s||MaxOpenConns=10||MaxIdleConns=5"
|
||||
|
||||
if _, err := OpenMysqlConnection(connectionString); err != nil {
|
||||
t.Errorf("there should be no err, but now has:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenMysqlConnection2(t *testing.T) {
|
||||
connectionString := "root:moqikaka3306@tcp(10.1.0.10:3306)/gameserver_data?charset=utf8&parseTime=true&loc=Local&timeout=60s"
|
||||
maxOpenConns := 10
|
||||
maxIdleConns := 5
|
||||
|
||||
if _, err := OpenMysqlConnection2(connectionString, maxOpenConns, maxIdleConns); err != nil {
|
||||
t.Errorf("there should be no err, but now has:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenMysqlConnection3(t *testing.T) {
|
||||
dbConfigObj := NewDBConfig("root:moqikaka3306@tcp(10.1.0.10:3306)/sdkcenter?charset=utf8&parseTime=true&loc=Local&timeout=10s", 5, 2)
|
||||
if _, err := OpenMysqlConnection3(dbConfigObj); err != nil {
|
||||
t.Errorf("there should be no err, but now has:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTestConnection(t *testing.T) {
|
||||
dbConfigObj := NewDBConfig("root:moqikaka3306@tcp(10.1.0.10:3306)/sdkcenter?charset=utf8&parseTime=true&loc=Local&timeout=10s", 5, 2)
|
||||
dbObj, err := OpenMysqlConnection3(dbConfigObj)
|
||||
if err != nil {
|
||||
t.Errorf("there should be no err, but now has:%s", err)
|
||||
}
|
||||
|
||||
succeedCount := 0
|
||||
expectedCount := 5
|
||||
for i := 0; i < expectedCount; i++ {
|
||||
if err := TestConnection(dbObj); err != nil {
|
||||
fmt.Printf("%s:%s\n", time.Now(), err)
|
||||
} else {
|
||||
succeedCount += 1
|
||||
fmt.Printf("%s:%s\n", time.Now(), "ok")
|
||||
}
|
||||
time.Sleep(time.Second * 3)
|
||||
}
|
||||
|
||||
if succeedCount != expectedCount {
|
||||
t.Errorf("ExecptedCount:%d, but got %d", expectedCount, succeedCount)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user