-
Notifications
You must be signed in to change notification settings - Fork 11
/
sql_connection.go
executable file
·121 lines (104 loc) · 2.62 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
package dsc
import (
"database/sql"
"fmt"
"time"
)
type sqlConnection struct {
*AbstractConnection
db *sql.DB
tx *sql.Tx
init bool
}
func (c *sqlConnection) CloseNow() error {
db, err := asSQLDb(c.db)
if err != nil {
return err
}
return db.Close()
}
func (c *sqlConnection) Begin() error {
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.tx == nil {
return fmt.Errorf("No active transaction")
}
err := c.tx.Commit()
c.tx = nil
return err
}
func (c *sqlConnection) Rollback() error {
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()
db, err := sql.Open(config.DriverName, config.Descriptor)
if err != nil {
return nil, fmt.Errorf("failed to open connection to %v on %v due to %v", config.DriverName, config.Descriptor, err)
}
var sqlConnection = &sqlConnection{db: db}
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
}
//TODO add to control this with config parameters
//set to min to not have lingered connection
db.SetConnMaxLifetime(1 * time.Second)
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
}