forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
69 lines (61 loc) · 1.4 KB
/
db.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
// This plugin provides a database transaction to the application.
// A transaction is begun before each request and committed on success.
// If a panic occurred during the request, the transaction is rolled back.
// (The application may also roll the transaction back itself.)
package db
import (
"database/sql"
"github.com/robfig/revel"
)
var (
Db *sql.DB
Driver string
Spec string
)
type DbPlugin struct {
revel.EmptyPlugin
}
func (p DbPlugin) OnAppStart() {
// Read configuration.
var found bool
if Driver, found = revel.Config.String("db.driver"); !found {
revel.ERROR.Fatal("No db.driver found.")
}
if Spec, found = revel.Config.String("db.spec"); !found {
revel.ERROR.Fatal("No db.spec found.")
}
// Open a connection.
var err error
Db, err = sql.Open(Driver, Spec)
if err != nil {
revel.ERROR.Fatal(err)
}
}
// Begin a transaction.
func (p DbPlugin) BeforeRequest(c *revel.Controller) {
txn, err := Db.Begin()
if err != nil {
panic(err)
}
c.Txn = txn
}
// Commit the active transaction.
func (p DbPlugin) AfterRequest(c *revel.Controller) {
if err := c.Txn.Commit(); err != nil {
if err != sql.ErrTxDone {
panic(err)
}
}
c.Txn = nil
}
// Rollback the active transaction, if any.
func (p DbPlugin) OnException(c *revel.Controller, err interface{}) {
if c.Txn == nil {
return
}
if err := c.Txn.Rollback(); err != nil {
if err != sql.ErrTxDone {
panic(err)
}
}
}