forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_test.go
66 lines (54 loc) · 1.32 KB
/
sql_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gorp_test
import (
"database/sql"
_ "github.com/ziutek/mymysql/godrv"
"testing"
)
func connectDb() *sql.DB {
db, err := sql.Open("mymysql", "gomysql_test/gomysql_test/abc123")
if err != nil {
panic("Error connecting to db: " + err.Error())
}
return db
}
// This fails on my machine with:
//
// panic: Received #1461 error from MySQL server:
// "Can't create more than max_prepared_stmt_count statements
// (current value: 16382)"
//
// Cause: stmt.Exec() is opening a new db connection for each call
// because each connection is still considered in use
//
func _TestPrepareExec(t *testing.T) {
db := connectDb()
defer db.Close()
db.Exec("drop table if exists test")
db.Exec("create table test (id int primary key, str varchar(20))")
query := "insert into test values (?, ?)"
stmt, err := db.Prepare(query)
if err != nil {
panic(err)
}
defer stmt.Close()
for i := 0; i < 20000; i++ {
_, err := stmt.Exec(i, "some str")
if err != nil {
panic(err)
}
}
}
// This works
func _TestQuery(t *testing.T) {
db := connectDb()
defer db.Close()
db.Exec("drop table if exists test")
db.Exec("create table test (id int primary key, str varchar(20))")
query := "insert into test values (?, ?)"
for i := 0; i < 20000; i++ {
_, err := db.Exec(query, i, "some str")
if err != nil {
panic(err)
}
}
}