Go/Golang sql.DB reuse in functions

huangapple go评论65阅读模式
英文:

Go/Golang sql.DB reuse in functions

问题

sql.Open()返回类型为*sql.DB的变量。

我有一个函数调用了其他10个函数,这些函数都需要进行数据库调用。

在以下两种方式中,哪种更正确/更高效:

  • 将*sql.DB指针发送给每个函数,或者
  • 在每个函数中创建一个新的*sql.DB对象

意思是:

func DoLotsOfThings() {
    db, _ := sql.Open()
    defer db.Close()
    DoTask1(db)
    DoTask2(db)
}

或者

func DoLotsOfThings() {
    DoTask1()
    DoTask2()
}

func DoTask1() {
    db, _ := sql.Open()
    defer db.Close()
}

func DoTask1() {
    db, _ := sql.Open()
    defer db.Close()
}

我之所以问这个问题是因为我目前正在将指针发送给每个函数,但我的驱动程序似乎出现了问题。我正在使用http://code.google.com/p/odbc,这使我相信每个函数都应该有自己的驱动程序,并且我可以依赖于驱动程序的内部机制。

编辑

关于驱动程序出现问题,只有在高流量环境下才会发生。而且只有在大约十分钟左右的时间后才会发生。这使我相信可能存在某种内存泄漏导致驱动程序停止工作。然而,我对每个*sql.DB实例都使用了defer db.Close(),所以我不知道还能做什么来解决这个问题。

andybalholm说连接池是由内部处理的,这似乎是准确的,因为它只在我尝试执行某些操作时才会出错,而不是在我调用sql.Open()时出错。

如果我让我的Go应用程序保持运行状态,它将无法执行任何类型的SQL查询,但如果我尝试单独运行其他连接到MSSQL并运行查询的Go测试,它是可以工作的。

英文:

sql.Open() returns a variable of type *sql.DB

I have a function that calls 10 other functions that all need to make database calls

Is it more correct/efficient to:

  • Send the *sql.DB pointer to every function, or
  • Create a new *sql.DB object in each function

Meaning

func DoLotsOfThings() {
    db, _ := sql.Open()
    defer db.Close()
    DoTask1(db)
    DoTask2(db)
}

or

func DoLotsOfThings() {
    DoTask1()
    DoTask2()
}

func DoTask1() {
    db, _ := sql.Open()
    defer db.Close()
}

func DoTask1() {
    db, _ := sql.Open()
    defer db.Close()
}

The reason why I'm asking is because I am currently sending the pointer to each function and my driver seems to break. I'm using http://code.google.com/p/odbc , which leads me to believe each function should have its own, and that I can rely on the driver's internals.

EDIT

RE driver breakage, it only happens under high traffic environments. And it only happens after say, ten minutes or so of time. Which leads me to believe that there is some sort of memory leak that makes using the driver stop working. However I defer db.Close() for every instance of *sql.DB, so I don't know what else I can do to resolve this issue.

andybalholm says the connection pooling is handled internally, which seems to be accurate, because it only breaks after I attempt to execute something, not when I invoke sql.Open()

If I leave my Go app running, it will not be able to execute any sort of SQL queries, but if I attempt to run other Go tests separately connecting to MSSQL and running queries, it works.

答案1

得分: 20

在你的代码中全局声明一个 var db *sql.DB,然后在整个代码中重复使用它。这是一个简化的示例:

var db *sql.DB

func DoLotsOfThings() {
    DoTask1(db)
    DoTask2(db)
}

func main() {
  db, _ = sql.Open() // 或者你使用的其他方法
  defer db.Close()
  DoLotsOfThings()
}

在全局声明 *sql.DB 还有一些额外的好处,比如 SetMaxIdleConns(调整连接池大小)或者在整个应用程序中 预编译 SQL 语句。

英文:

Declare a var db *sql.DB globally, and then reuse it across your code. Here is an example (simplified):

var db *sql.DB

func DoLotsOfThings() {
    DoTask1(db)
    DoTask2(db)
}

func main() {
  db, _ = sql.Open() # or whatever you use
  defer db.Close()
  DoLotsOfThings()
}

Declaring *sql.DB globally also have some additional benefits such as SetMaxIdleConns (regulating connection pool size) or preparing SQL statements across your application.

答案2

得分: 9

你不需要在各个地方都打开数据库连接。数据库/sql包在内部进行连接池管理,根据需要打开和关闭连接,同时提供了一个可以并发使用的单个连接的假象。

也许你需要在其他地方寻找驱动程序出错的原因。提供一些更多的细节将有助于其他人弄清楚发生了什么。

英文:

You shouldn't need to open database connections all over the place. The database/sql package does connection pooling internally, opening and closing connections as needed, while providing the illusion of a single connection that can be used concurrently.

Probably you need to look elsewhere for the cause of your driver breakage. Some more details about that would make it easier for people to figure out what is going on.

huangapple
  • 本文由 发表于 2013年11月14日 20:41:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/19977940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定