Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
3479cf4b
Commit
3479cf4b
authored
Jun 18, 2018
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
expose functions to use sessions
parent
2b849086
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
11 deletions
+69
-11
pkg/services/sqlstore/session.go
+10
-10
pkg/services/sqlstore/sqlstore.go
+59
-1
No files found.
pkg/services/sqlstore/session.go
View file @
3479cf4b
...
...
@@ -27,18 +27,18 @@ func startSession(ctx context.Context, engine *xorm.Engine, beginTran bool) (*DB
var
sess
*
DBSession
sess
,
ok
:=
value
.
(
*
DBSession
)
if
!
ok
{
newSess
:=
&
DBSession
{
Session
:
engine
.
NewSession
()}
if
beginTran
{
err
:=
newSess
.
Begin
()
if
err
!=
nil
{
return
nil
,
err
}
}
return
newSess
,
nil
if
ok
{
return
sess
,
nil
}
return
sess
,
nil
newSess
:=
&
DBSession
{
Session
:
engine
.
NewSession
()}
if
beginTran
{
err
:=
newSess
.
Begin
()
if
err
!=
nil
{
return
nil
,
err
}
}
return
newSess
,
nil
}
func
withDbSession
(
ctx
context
.
Context
,
callback
dbTransactionFunc
)
error
{
...
...
pkg/services/sqlstore/sqlstore.go
View file @
3479cf4b
...
...
@@ -26,7 +26,7 @@ import (
_
"github.com/grafana/grafana/pkg/tsdb/mssql"
_
"github.com/lib/pq"
_
"github.com/mattn/go-sqlite3"
sqlite3
"github.com/mattn/go-sqlite3"
)
var
(
...
...
@@ -56,6 +56,64 @@ type SqlStore struct {
skipEnsureAdmin
bool
}
// NewSession returns a new DBSession
func
(
ss
*
SqlStore
)
NewSession
()
*
DBSession
{
return
&
DBSession
{
Session
:
ss
.
engine
.
NewSession
()}
}
// WithDbSession calls the callback with an session attached to the context.
func
(
ss
*
SqlStore
)
WithDbSession
(
ctx
context
.
Context
,
callback
dbTransactionFunc
)
error
{
sess
,
err
:=
startSession
(
ctx
,
ss
.
engine
,
false
)
if
err
!=
nil
{
return
err
}
return
callback
(
sess
)
}
// WithTransactionalDbSession calls the callback with an session within a transaction
func
(
ss
*
SqlStore
)
WithTransactionalDbSession
(
ctx
context
.
Context
,
callback
dbTransactionFunc
)
error
{
return
ss
.
inTransactionWithRetryCtx
(
ctx
,
callback
,
0
)
}
func
(
ss
*
SqlStore
)
inTransactionWithRetryCtx
(
ctx
context
.
Context
,
callback
dbTransactionFunc
,
retry
int
)
error
{
sess
,
err
:=
startSession
(
ctx
,
ss
.
engine
,
true
)
if
err
!=
nil
{
return
err
}
defer
sess
.
Close
()
err
=
callback
(
sess
)
// special handling of database locked errors for sqlite, then we can retry 3 times
if
sqlError
,
ok
:=
err
.
(
sqlite3
.
Error
);
ok
&&
retry
<
5
{
if
sqlError
.
Code
==
sqlite3
.
ErrLocked
{
sess
.
Rollback
()
time
.
Sleep
(
time
.
Millisecond
*
time
.
Duration
(
10
))
sqlog
.
Info
(
"Database table locked, sleeping then retrying"
,
"retry"
,
retry
)
return
ss
.
inTransactionWithRetryCtx
(
ctx
,
callback
,
retry
+
1
)
}
}
if
err
!=
nil
{
sess
.
Rollback
()
return
err
}
else
if
err
=
sess
.
Commit
();
err
!=
nil
{
return
err
}
if
len
(
sess
.
events
)
>
0
{
for
_
,
e
:=
range
sess
.
events
{
if
err
=
bus
.
Publish
(
e
);
err
!=
nil
{
log
.
Error
(
3
,
"Failed to publish event after commit"
,
err
)
}
}
}
return
nil
}
func
(
ss
*
SqlStore
)
Init
()
error
{
ss
.
log
=
log
.
New
(
"sqlstore"
)
ss
.
readConfig
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment