-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-pg-instance-tx-impl.go
80 lines (63 loc) · 1.24 KB
/
db-pg-instance-tx-impl.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
76
77
78
79
80
/*
* Copyright (c) 2024.
* Created by Andy Pangaribuan <https://github.com/apangaribuan>.
*
* This product is protected by copyright and distributed under
* licenses restricting copying, distribution and decompilation.
* All Rights Reserved.
*/
package db
import (
"database/sql"
"log"
"math/rand"
"time"
)
func (slf *pgInstanceTx) Commit() (err error) {
if slf == nil {
return nil
}
if slf.isCommit || slf.isRollback || slf.tx == nil {
return nil
}
err = slf.tx.Commit()
slf.isCommit = err == nil
return
}
func (slf *pgInstanceTx) Rollback() {
const (
min int64 = 100
max int64 = 300
maxTry = 3
)
var err error
if slf == nil {
return
}
if slf.isCommit || slf.isRollback || slf.tx == nil {
if slf.errCommit != nil && slf.isCommit {
log.Printf("db.tx.rollback: isCommit = %v\n", slf.isCommit)
}
if slf.isRollback {
log.Printf("db.tx.rollback: isRollback = %v\n", slf.isRollback)
}
return
}
iteration := -1
for {
iteration++
err = slf.tx.Rollback()
if err == nil {
break
}
if err == sql.ErrTxDone {
err = nil
break
}
if iteration >= maxTry {
break
}
time.Sleep(time.Microsecond * time.Duration(rand.Int63n(max-min)+min))
}
slf.isRollback = err == nil
}