Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对类似rollback这样的功能怎么通过框架实现呢? #59

Open
mengmengpengpeng opened this issue Oct 26, 2020 · 1 comment
Open

Comments

@mengmengpengpeng
Copy link

No description provided.

@zhuxiujia
Copy link
Owner

example/ 文件夹里面 有例子。 文档网站里面也有文档。
2种办法,
第一种是声明式事务,和java spring的类似,有传播行为

type TestService struct {
	exampleActivityMapper *ExampleActivityMapper //服务包含一个mapper操作数据库,类似java spring mvc
	//类似拷贝spring MVC的声明式事务注解
	//rollback:回滚操作为error类型(你也可以自定义实现了builtin.error接口的自定义struct,框架会把自定义的error类型转换为string,检查是否包含,是则回滚
	//tx:"" 开启事务,`tx:"PROPAGATION_REQUIRED,error"` 指定传播行为为REQUIRED(默认REQUIRED))
	UpdateName   func(id string, name string) error   `tx:"" rollback:"error"`
	UpdateRemark func(id string, remark string) error `tx:"" rollback:"error"`
}

第二种本地事务,直接使用session.begin , close 等等方法

//本地事务使用例子
func Test_local_Transation(t *testing.T) {
	if MysqlUri == "" || MysqlUri == "*" {
		fmt.Println("no database url define in Example_config.go , you must set the mysql link!")
		return
	}
	//使用事务
	var session, err = exampleActivityMapper.SessionSupport.NewSession()
	if err != nil {
		t.Fatal(err)
	}
	session.Begin(nil) //开启事务
	var activityBean = Activity{
		Id:   "170",
		Name: "rs168-8",
	}
	var updateNum, e = exampleActivityMapper.UpdateById(&session, activityBean) //sessionId 有值则使用已经创建的session,否则新建一个session
	fmt.Println("updateNum=", updateNum)
	if e != nil {
		panic(e)
	}
	session.Commit() //提交事务
	session.Close()  //关闭事务
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants