alone use close
This commit is contained in:
parent
ef323fd70d
commit
1f52bc4ce9
@ -3,7 +3,7 @@ package common
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -81,10 +81,15 @@ func GetDsn(conf map[string]string) string {
|
|||||||
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", conf["user"], conf["password"], conf["host"], conf["port"], conf["database"])
|
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", conf["user"], conf["password"], conf["host"], conf["port"], conf["database"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReplaceDsnPassword(dsn string) string {
|
||||||
|
reg := regexp.MustCompile(`:(.*)@`)
|
||||||
|
return reg.ReplaceAllString(dsn, "****")
|
||||||
|
}
|
||||||
|
|
||||||
func DbGet(conf map[string]string) *MyDb {
|
func DbGet(conf map[string]string) *MyDb {
|
||||||
dsn := GetDsn(conf)
|
dsn := GetDsn(conf)
|
||||||
if dbs[dsn] == nil {
|
if dbs[dsn] == nil {
|
||||||
logrus.Printf("connecting %s", strings.Replace(dsn, conf["password"], "****", 1))
|
logrus.Printf("connecting %s", ReplaceDsnPassword(dsn))
|
||||||
db1, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
db1, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||||
SkipDefaultTransaction: true,
|
SkipDefaultTransaction: true,
|
||||||
})
|
})
|
||||||
@ -95,14 +100,25 @@ func DbGet(conf map[string]string) *MyDb {
|
|||||||
return dbs[dsn]
|
return dbs[dsn]
|
||||||
}
|
}
|
||||||
|
|
||||||
func DbAlone(conf map[string]string) (*MyDb, *sql.DB) {
|
type MyConn struct {
|
||||||
logrus.Printf("opening alone mysql: %s", GetDsn(conf))
|
Conn *sql.DB
|
||||||
mdb, err := sql.Open("mysql", GetDsn(conf))
|
Dsn string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *MyConn) Close() {
|
||||||
|
logrus.Printf("closing alone mysql: %s", ReplaceDsnPassword(conn.Dsn))
|
||||||
|
conn.Conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func DbAlone(conf map[string]string) (*MyDb, *MyConn) {
|
||||||
|
dsn := GetDsn(conf)
|
||||||
|
logrus.Printf("opening alone mysql: %s", ReplaceDsnPassword(dsn))
|
||||||
|
mdb, err := sql.Open("mysql", dsn)
|
||||||
PanicIfError(err)
|
PanicIfError(err)
|
||||||
gormDB, err := gorm.Open(mysql.New(mysql.Config{
|
gormDB, err := gorm.Open(mysql.New(mysql.Config{
|
||||||
Conn: mdb,
|
Conn: mdb,
|
||||||
}), &gorm.Config{})
|
}), &gorm.Config{})
|
||||||
PanicIfError(err)
|
PanicIfError(err)
|
||||||
gormDB.Use(&tracePlugin{})
|
gormDB.Use(&tracePlugin{})
|
||||||
return &MyDb{DB: gormDB}, mdb
|
return &MyDb{DB: gormDB}, &MyConn{Conn: mdb, Dsn: dsn}
|
||||||
}
|
}
|
||||||
|
|||||||
10
xa.go
10
xa.go
@ -6,7 +6,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -41,7 +40,6 @@ func XaClientNew(server string, mysqlConf map[string]string, app *gin.Engine, ca
|
|||||||
common.MustUnmarshal(b, &req)
|
common.MustUnmarshal(b, &req)
|
||||||
tx, my := common.DbAlone(xa.Conf)
|
tx, my := common.DbAlone(xa.Conf)
|
||||||
defer func() {
|
defer func() {
|
||||||
logrus.Printf("closing conn %v", xa.Conf)
|
|
||||||
my.Close()
|
my.Close()
|
||||||
}()
|
}()
|
||||||
if req.Action == "commit" {
|
if req.Action == "commit" {
|
||||||
@ -59,13 +57,7 @@ func (xa *XaClient) XaLocalTransaction(gid string, transFunc XaLocalFunc) (rerr
|
|||||||
defer common.Panic2Error(&rerr)
|
defer common.Panic2Error(&rerr)
|
||||||
branch := common.GenGid()
|
branch := common.GenGid()
|
||||||
tx, my := common.DbAlone(xa.Conf)
|
tx, my := common.DbAlone(xa.Conf)
|
||||||
defer func() {
|
defer func() { my.Close() }()
|
||||||
logrus.Printf("closing conn %v", xa.Conf)
|
|
||||||
my.Close()
|
|
||||||
}()
|
|
||||||
// tx1 := db.Session(&gorm.Session{SkipDefaultTransaction: true})
|
|
||||||
// common.PanicIfError(tx1.Error)
|
|
||||||
// tx := common.MyDb{DB: tx1}
|
|
||||||
tx.Must().Exec(fmt.Sprintf("XA start '%s'", branch))
|
tx.Must().Exec(fmt.Sprintf("XA start '%s'", branch))
|
||||||
err := transFunc(tx)
|
err := transFunc(tx)
|
||||||
common.PanicIfError(err)
|
common.PanicIfError(err)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user