限制 pq 连接 SetMaxOpenConns

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

Limiting pq connections SetMaxOpenConns

问题

我正在使用go语言中的pq驱动(http://github.com/lib/pq)来向PostgreSQL数据库写入数据,但是当同时发生大量事务时,驱动程序会发生panic,并显示以下错误信息:

pq: sorry, too many clients already

为了防止这种行为,我想使用SetMaxOpenConns(如http://golang.org/pkg/database/sql/中所述),但是编译器报错:

db.SetMaxOpenConns undefined (type *sql.DB has no field or method SetMaxOpenConns)

我以为pq中的函数也会在sql中可用,但显然它们不可用。

我的代码如下:

package main

import (
    "database/sql"
    _ "github.com/lib/pq"
)

func Main() {
    var db, _ = sql.Open("postgres", "user=user dbname=db")
    db.SetMaxOpenConns(10)
}

是否有其他方法来限制打开的连接数?

英文:

I am using the pq driver (http://github.com/lib/pq) in go for writing into a postgres database, but when a lot of transactions happen at once, the driver panics and does the following:

pq: sorry, too many clients already

To prevent this behavior I wanted to use SetMaxOpenConns (as documented in http://golang.org/pkg/database/sql/), but the compiler says:

db.SetMaxOpenConns undefined (type *sql.DB has no field or method SetMaxOpenConns)

I thought the functions from sql would also be available in pq, but apparently they aren't.

My code:

package main

import (
	"database/sql"
	_ "github.com/lib/pq"
)

func Main() {
	var db, _ = sql.Open("postgres", "user=user dbname=db")
	db.SetMaxOpenConns(10)
}

Is there any other possibility to limit the amount of open connections?

答案1

得分: 3

正如James指出的那样,我引用了一个只在Go 1.2上可用的函数,而我仍然安装的是1.1版本。

简而言之,解决方案是:升级到Go 1.2

英文:

As James pointed out, I referred to a function that is only available on Go 1.2, while I had still installed 1.1.

In short the Solution is: Update to Go 1.2

huangapple
  • 本文由 发表于 2013年12月4日 22:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/20377674.html
匿名

发表评论

匿名网友

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

确定