英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论