Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hujiebin
hilo-common
Commits
8d8ceb62
Commit
8d8ceb62
authored
Feb 24, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create service.go
parent
cdb952ca
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
0 deletions
+58
-0
service.go
domain/service.go
+58
-0
No files found.
domain/service.go
0 → 100644
View file @
8d8ceb62
package
domain
import
(
"git.hilo.cn/hilo-common/mycontext"
"git.hilo.cn/hilo-common/resource/mysql"
"git.hilo.cn/hilo-common/resource/redisCli"
"runtime/debug"
)
type
Service
struct
{
*
CtxAndDb
}
func
(
service
*
Service
)
getMyContext
()
*
mycontext
.
MyContext
{
return
service
.
MyContext
}
/**
* 创建服务
* @param
* @return
**/
func
CreateService
(
myContext
*
mycontext
.
MyContext
)
*
Service
{
if
myContext
==
nil
{
return
&
Service
{
CtxAndDb
:
&
CtxAndDb
{
Db
:
mysql
.
Db
,
MyContext
:
mycontext
.
CreateMyContext
(
nil
),
Redis
:
redisCli
.
GetRedis
(),
}}
}
else
{
return
&
Service
{
CtxAndDb
:
&
CtxAndDb
{
Db
:
mysql
.
Db
,
MyContext
:
myContext
,
Redis
:
redisCli
.
GetRedis
(),
}}
}
}
//事务钩子回调,遇到错误,异常则回调,写service都需要钩子回调
func
(
service
*
Service
)
Transactional
(
callback
func
()
error
)
error
{
//异常回调
defer
func
()
{
if
err
:=
recover
();
err
!=
nil
{
service
.
Log
.
Errorf
(
"doTransactional SYSTEM ACTION PANIC: %v, stack: %v"
,
err
,
string
(
debug
.
Stack
()))
service
.
Db
.
Rollback
()
//为了防止给controller层造成数据错误,继续抛恐慌
panic
(
err
)
}
}()
service
.
CtxAndDb
.
Db
=
mysql
.
Db
.
Begin
()
err
:=
callback
()
if
err
!=
nil
{
service
.
Db
.
Rollback
()
return
err
}
//提交
return
service
.
Db
.
Commit
()
.
Error
}
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