1.使用query、queryRows返回的rows,应该使用rows.next(),扫描完每一行或者调用rows.close(),否则该链接永远不会被释放。
The connection used by the Query remains active until either all rows are exhausted by the iteration via Next, or rows.Close() is called, at which point it is released. For more information, see the section on the connection pool.
2.使用事务时不能使用下面这种方式,因为sqlx维护了一个连接池,开启事务、执行语句、提交回滚不一定在同一个连接上。
// this will not work if connection pool > 1
db.MustExec("BEGIN;")
db.MustExec(...)
db.MustExec("COMMIT;")
应该使用下面的方式
tx := db.MustBegin()
tx.MustExec(...)
err = tx.Commit()
一定要提交或者回滚,不然连接不会释放。
3.不能使用占位符修改sql语句的表结构
// doesn't work
db.Query("SELECT * FROM ?", "mytable")
// also doesn't work
db.Query("SELECT ?, ? FROM people", "name", "location")
814

被折叠的 条评论
为什么被折叠?



