使用 Go 和使用 database/sql 的数据库驱动程序实现,我似乎在事务中遇到的行为似乎是每次事务后都需要关闭连接。如果我不这样做,数据库会用完连接,并且我会收到以下错误:
“开始事务失败。错误 = 错误 1040:连接太多”。这发生在 101 次提交之后。
我尝试使用来自 github 的两个不同的驱动程序 - lib/pq 和 go-sql-driver/mysql,结果相同。
这种行为对我来说似乎很奇怪。这是可以预料的,还是我可能做错了什么?
根据要求,代码如下:
package main
import (
"database/sql"
"fmt"
"time"
"os"
"bufio"
"strconv"
_ "github.com/lib/pq"
//// _ "github.com/go-sql-driver/mysql"
//// _ "github.com/arnehormann/mysql"
)
const C_CONN_RDBMS = "postgres"
const C_CONN_STR = "user=admin dbname=testdb password=admin sslmode=disable"
////const C_CONN_RDBMS = "mysql"
////const C_CONN_STR = "test:test@/testdb?charset=utf8"
var pogDbConn *sql.DB // Db connection
func main() {
fmt.Println("\ntestdb1 - small test on "+C_CONN_RDBMS+" driver")
println()
var iIters int = fGetIterations()
println()
var tCloseConn bool = fGetCloseConn()
tmeStart := time.Now()
fDbTestInserts(iIters, tCloseConn) // run test Insert
fmt.Printf("Elapsed Time to process = %s\n", time.Since(tmeStart))
if pogDbConn != nil {
pogDbConn.Close()
}
}
func fDbTestInserts(iIters int, tCloseConn bool) {
var iCommitted int = 0
var oOsError error
var poDbTxn *sql.Tx
println("Running test inserts .........")
defer func() {fRollback(poDbTxn, iCommitted)} ()
for iPos := 1; iPos <= iIters; iPos += 1 {
if pogDbConn == nil { // must open db
pogDbConn, oOsError = sql.Open(C_CONN_RDBMS, C_CONN_STR)
if oOsError != nil {
fmt.Printf("Failed to open Db Connection. Error = %s\n")
return
}
}
poDbTxn, oOsError := pogDbConn.Begin()
if oOsError != nil {
fmt.Printf("Begin Transaction failed. Error = %s\n", oOsError)
return
}
var sSql string = "INSERT INTO test01 " +
"(sName, dBalance)" +
" VALUES ('Bart Simpson', 999.99)"
_, oOsError = poDbTxn.Exec(sSql)
if oOsError != nil {
fmt.Printf("INSERT for Table failed. Error = %s\n", oOsError)
return
}
_, oOsError = poDbTxn.Exec("COMMIT")
if oOsError != nil {
fmt.Printf("COMMIT for Insert failed. Error = %s\n", oOsError)
return
}
poDbTxn = nil
iCommitted += 1
if iPos%100 == 0 {
fmt.Printf("Iteration = %d, Inserted = %d \n", iPos, iCommitted)
}
if tCloseConn {
pogDbConn.Close()
pogDbConn = nil
}
}
fmt.Printf("Inserts completed - committed = %d\n", iCommitted)
}
func fRollback(poDbTxn *sql.Tx, iCommitted int) {
println("In fDbRollbackTran\n")
fmt.Printf("Committed trans = %d\n", iCommitted)
if poDbTxn == nil {
println("No Rollback required")
} else {
if pogDbConn == nil {
print ("Unable to Rollback - no connection")
} else {
println("Attempting Rollback")
var oOsError error = poDbTxn.Rollback()
if oOsError != nil {
fmt.Printf("Rollback of Transaction failed. Error = %s\n", oOsError)
} else {
println("Rollback Succeeded")
}
}
}
}
func fGetIterations() int {
oBufReader := bufio.NewReader(os.Stdin)
for {
print("Number of Inserts to process : (1 to 10,000) or 'end' : ")
vLine, _, _ := oBufReader.ReadLine()
var sInput string = string(vLine)
if sInput == "end" || sInput == "END" {
os.Exit(1)
}
iTot, oError := strconv.Atoi(sInput)
if oError != nil {
println("Invalid number")
} else if iTot < 1 || iTot > 10000 {
println ("Number must be from 1 to 10,000")
} else {
return iTot
}
}
}
func fGetCloseConn() bool {
oBufReader := bufio.NewReader(os.Stdin)
for {
print("Close Connection every transaction? (y/n/end) : ")
vLine, _, _ := oBufReader.ReadLine()
sInput := string(vLine)
if sInput == "y" || sInput == "n" {
return (sInput == "y")
}
if sInput == "end" || sInput == "END" {
os.Exit(1)
}
}
}