Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

调用wuid生成id后,会自动关闭数据库连接,导致后续数据库操作无法继续执行 #7

Closed
qingmumao opened this issue May 12, 2019 · 1 comment

Comments

@qingmumao
Copy link

调用wuid生成id后,会自动关闭数据库连接,导致后续数据库操作无法继续执行
package tools
import (

"database/sql"
"github.com/astaxie/beego/orm"
"github.com/edwingeng/wuid/mysql"
_"github.com/go-sql-driver/mysql"

)

var newDB = func() (*sql.DB,bool,error){
db,_ := orm.GetDB()
return db,true,nil
}

var g = wuid.NewWUID("default", nil)

func NewId() (uint64){
g.LoadH28FromMysql(newDB, "wuid")
return g.Next()
}

调用代码:

func AddUser(u *User) (int64, error) {
// db,err := orm.GetDB()
//fmt.Println(err)
o := orm.NewOrm()
u.Id = 74837477878
for i:=0;i<100;i++ {
fmt.Println(tools.NewId())
}

fmt.Println(u)
return o.Insert(u)

}

插入操作无法继续执行,
[ORM]2019/05/12 22:09:56 -[Queries/default] - [FAIL / db.Exec / 0.0ms] - [INSERT INTO sys_user (id, username, password, phone, email, name, avatar, salt, status, is_staff, last_login_time, login_error_times, remark, delete_flag, create_by, create_time, update_by, update_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)] - 74837477878, qingmumao,
xxxxxxxx, 13332978703, huiii@qq.com, , , , `0`, `0`, `0`, `0`, , 0, 0, 0, 0, 0 - sql: database is closed

@edwingeng
Copy link
Owner

var newDB = func() (*sql.DB,bool,error){
db,_ := orm.GetDB()
return db,true,nil

这样就可以了:return db, false, nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants