-
Notifications
You must be signed in to change notification settings - Fork 11
/
sql_connection.go
executable file
·155 lines (138 loc) · 3.52 KB
/
sql_connection.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package dsc
import (
"database/sql"
"fmt"
"time"
)
const (
connMaxLifetimeMsKey = "connMaxLifetimeMs"
defaultConnMaxLifetimeMs = 1000
maxIdleConnsKey = "maxIdleConns"
)
type sqlConnection struct {
canHandleTransaction bool
*AbstractConnection
db *sql.DB
tx *sql.Tx
init bool
}
func (c *sqlConnection) CloseNow() error {
db, err := asSQLDb(c.db)
if err != nil {
return err
}
db.SetConnMaxLifetime(1000 * time.Millisecond)
return db.Close()
}
func (c *sqlConnection) Begin() error {
if !c.canHandleTransaction {
return nil
}
db, err := asSQLDb(c.db)
if err != nil {
return err
}
tx, err := db.Begin()
if err != nil {
return err
}
c.tx = tx
return nil
}
func (c *sqlConnection) Unwrap(target interface{}) interface{} {
if target == sqlDbPointer {
return c.db
} else if target == sqlTxtPointer {
return c.tx
}
panic(fmt.Sprintf("unsupported target type %v", target))
}
func (c *sqlConnection) Commit() error {
if !c.canHandleTransaction {
return nil
}
if c.tx == nil {
return fmt.Errorf("no active transaction")
}
err := c.tx.Commit()
c.tx = nil
return err
}
func (c *sqlConnection) Rollback() error {
if !c.canHandleTransaction {
return nil
}
if c.tx == nil {
return fmt.Errorf("no active transaction")
}
err := c.tx.Rollback()
c.tx = nil
return err
}
type sqlConnectionProvider struct {
*AbstractConnectionProvider
}
func (c *sqlConnectionProvider) NewConnection() (Connection, error) {
config := c.ConnectionProvider.Config()
dsn, err := config.DsnDescriptor()
if err != nil {
return nil, err
}
db, err := sql.Open(config.DriverName, dsn)
if err != nil {
return nil, fmt.Errorf("failed to open connection to %v on %v due to %v", config.DriverName, config.Descriptor, err)
}
if len(config.InitSQL) > 0 {
for _, SQL := range config.InitSQL {
if _, err = db.Exec(SQL); err != nil {
return nil, fmt.Errorf("failed to execute init SQL %v on %v due to %v", SQL, config.Descriptor, err)
}
}
}
dialect := GetDatastoreDialect(config.DriverName)
var sqlConnection = &sqlConnection{db: db, canHandleTransaction: dialect.CanHandleTransaction()}
var connection Connection = sqlConnection
var super = NewAbstractConnection(config, c.ConnectionProvider.ConnectionPool(), connection)
sqlConnection.AbstractConnection = super
return connection, nil
}
func (c *sqlConnectionProvider) Get() (Connection, error) {
result, err := c.AbstractConnectionProvider.Get()
if err != nil {
return nil, err
}
db, err := asSQLDb(result.Unwrap(sqlDbPointer))
if err != nil {
return nil, err
}
if result.LastUsed() != nil && (time.Now().Sub(*result.LastUsed()) > 60*time.Second) {
err = db.Ping()
}
if err == nil {
return result, nil
}
if c.config.Has(connMaxLifetimeMsKey) {
connMaxLifetime := c.config.GetDuration(connMaxLifetimeMsKey, time.Millisecond, defaultConnMaxLifetimeMs)
if connMaxLifetime != 0 {
db.SetConnMaxLifetime(connMaxLifetime)
}
}
if c.config.Has(maxIdleConnsKey) {
db.SetMaxIdleConns(c.config.GetInt(maxIdleConnsKey, 1))
}
result, err = c.NewConnection()
if err != nil {
return nil, err
}
return result, nil
}
func newSQLConnectionProvider(config *Config) ConnectionProvider {
if config.MaxPoolSize == 0 {
config.MaxPoolSize = 1
}
sqlConnectionProvider := &sqlConnectionProvider{}
var connectionProvider ConnectionProvider = sqlConnectionProvider
super := NewAbstractConnectionProvider(config, make(chan Connection, config.MaxPoolSize), connectionProvider)
sqlConnectionProvider.AbstractConnectionProvider = super
return connectionProvider
}