bugfix - #1491 tx will rollback unexpected when session isAutoClose is true #1734

Closed
Avtion wants to merge 1 commits from master into master
First-time contributor

question

when use engine.Context(context.background()) to get a sesion to start a transaction, the session will rollback unexpected.In my opinion when we begin a new transaction with isAutoClose is true, the session will auto rollback our transaction.

this is my unit test

type User struct {
	Id   int `xorm:"pk autoincr"`
	Name string
}

func TestTx(t *testing.T) {
	s := engine.Context(context.Background())

	if err := s.Begin(); err != nil {
		log.Fatal(err)
	}

	if affected, err := s.InsertOne(&User{Name: "test"}); err != nil || affected == 0 {
		_ = s.Rollback()
		t.Error(err)
		return
	}

	if has, err := s.Get(&User{Name: "test"}); err != nil || !has {
		_ = s.Rollback()
		t.Error(err)
		return
	}

	if err := s.Commit(); err != nil {
		t.Fatal(err)
	}
}

this is the result

[xorm] [info]  2020/07/08 16:09:23.116689 [SQL] BEGIN TRANSACTION [] - 1.0045ms
[xorm] [info]  2020/07/08 16:09:23.118688 [SQL] INSERT INTO `user` (`name`) VALUES (?) [test] - 1.9993ms
[xorm] [info]  2020/07/08 16:09:23.132809 [SQL] ROLLBACK [] - 14.1207ms
[xorm] [info]  2020/07/08 16:09:23.132809 [SQL] ROLL BACK [] - 14.1207ms
[xorm] [info]  2020/07/08 16:09:23.134810 [SQL] SELECT `id`, `name` FROM `user` WHERE `name`=? LIMIT 1 [test] - 2.0009ms

fix

when i add session.isAutoClose = false in fun Begin(), it work.

[xorm] [info]  2020/07/08 16:10:58.127243 [SQL] BEGIN TRANSACTION [] - 999.7µs
[xorm] [info]  2020/07/08 16:10:58.129244 [SQL] INSERT INTO `user` (`name`) VALUES (?) [test] - 2.0012ms
[xorm] [info]  2020/07/08 16:10:58.131244 [SQL] SELECT `id`, `name` FROM `user` WHERE `name`=? LIMIT 1 [test] - 1.9996ms
[xorm] [info]  2020/07/08 16:10:58.134242 [SQL] COMMIT [] - 2.9986ms
# question when use `engine.Context(context.background())` to get a sesion to start a transaction, the session will rollback unexpected.In my opinion when we begin a new transaction with `isAutoClose` is true, the session will auto rollback our transaction. this is my unit test ``` type User struct { Id int `xorm:"pk autoincr"` Name string } func TestTx(t *testing.T) { s := engine.Context(context.Background()) if err := s.Begin(); err != nil { log.Fatal(err) } if affected, err := s.InsertOne(&User{Name: "test"}); err != nil || affected == 0 { _ = s.Rollback() t.Error(err) return } if has, err := s.Get(&User{Name: "test"}); err != nil || !has { _ = s.Rollback() t.Error(err) return } if err := s.Commit(); err != nil { t.Fatal(err) } } ``` this is the result ``` [xorm] [info] 2020/07/08 16:09:23.116689 [SQL] BEGIN TRANSACTION [] - 1.0045ms [xorm] [info] 2020/07/08 16:09:23.118688 [SQL] INSERT INTO `user` (`name`) VALUES (?) [test] - 1.9993ms [xorm] [info] 2020/07/08 16:09:23.132809 [SQL] ROLLBACK [] - 14.1207ms [xorm] [info] 2020/07/08 16:09:23.132809 [SQL] ROLL BACK [] - 14.1207ms [xorm] [info] 2020/07/08 16:09:23.134810 [SQL] SELECT `id`, `name` FROM `user` WHERE `name`=? LIMIT 1 [test] - 2.0009ms ``` # fix when i add `session.isAutoClose = false` in fun `Begin()`, it work. ``` [xorm] [info] 2020/07/08 16:10:58.127243 [SQL] BEGIN TRANSACTION [] - 999.7µs [xorm] [info] 2020/07/08 16:10:58.129244 [SQL] INSERT INTO `user` (`name`) VALUES (?) [test] - 2.0012ms [xorm] [info] 2020/07/08 16:10:58.131244 [SQL] SELECT `id`, `name` FROM `user` WHERE `name`=? LIMIT 1 [test] - 1.9996ms [xorm] [info] 2020/07/08 16:10:58.134242 [SQL] COMMIT [] - 2.9986ms ```
Avtion added 1 commit 2020-07-08 08:12:04 +00:00
bugfix - #1491
Some checks failed
continuous-integration/drone/pr Build is failing
f3c53f44b8
Avtion closed this pull request 2020-07-08 08:39:35 +00:00
Some checks failed
continuous-integration/drone/pr Build is failing

Pull request closed

Sign in to join this conversation.
No description provided.