-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
75 lines (68 loc) · 1.39 KB
/
main.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
67
68
69
70
71
72
73
74
75
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"strings"
"time"
)
func MustExec(s *sql.DB, q string) (result sql.Result) {
// FIXME:
for _, stmt := range strings.Split(q, ";") {
stmt = strings.TrimSpace(stmt)
if stmt == "" {
continue
}
res, err := s.Exec(stmt)
if err != nil {
panic(err)
}
result = res
}
return result
}
func table1(s *sql.DB) {
MustExec(s, `
drop table if exists t1;
create table t1 (i int, j int, k int, unique index unq_j (j));
insert into t1 values (1, 1, 1), (2, 2, 2);
`)
}
func table2(s *sql.DB) {
MustExec(s, `
drop table if exists t1;
create table t1 (i int key, j int, k int, unique index unq_j (j));
insert into t1 values (1, 1, 1), (2, 2, 2);
`)
}
func main() {
DSN := "root:@tcp(172.16.4.82:30488)/test"
// DSN := "root:@tcp(127.0.0.1:3306)/test"
s1, err := sql.Open("mysql", DSN)
if err != nil {
panic("connect failed")
}
defer s1.Close()
s2, err := sql.Open("mysql", DSN)
if err != nil {
panic("connect failed")
}
defer s2.Close()
table1(s1)
//table2(s1)
MustExec(s1, `
begin ;
insert into t1 values (3, 3, 3);
`)
start := time.Now().Unix()
fmt.Printf("start s2: %d\n", start)
go MustExec(s2, `
set innodb_lock_wait_timeout=5;
begin ;
update t1 set k = 33 where j = 3;
`)
time.Sleep(1)
fmt.Printf("wait time: %d\n", time.Now().Unix()-start)
MustExec(s1, "commit;")
MustExec(s2, "commit;")
}