Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

如果mysql的配置是autocommit,对于innodb 引擎的表,是否还会支持事务? #1121

Closed
qianxiansheng90 opened this issue Oct 19, 2018 · 2 comments
Labels

Comments

@qianxiansheng90
Copy link

环境:
mysql是5.7
mysql配置是autocommit=1
表结构是:
Create Table: CREATE TABLE t (
id int(11) DEFAULT NULL,
name varchar(255) DEFAULT NULL,
other text,
other2 text
) ENGINE=InnoDB DEFAULT CHARSET=utf8

代码:
type t struct {
ID int
Name string
Other interface{} xorm:"JSON"
Other2 interface{} xorm:"JSON"
}

session:=eng1.NewSession()
err=session.Begin()
if err != nil{
	fmt.Println(err)
	return
}
defer session.Close()

ss:=make(map[string]string)
ss["1"]="1"
ss["2"]="4"
t1.Other,_=json.Marshal(ss)
var affectRows int64
affectRows,err=eng1.Cols("other").Update(&t{Other:t1.Other},&t{ID:1})
if err != nil{
	session.Rollback()
	fmt.Printf("update 1 err %s",err.Error())
	return
}
fmt.Println(affectRows)
err=session.Commit()
fmt.Println(err)

我在update 之后和 session.Commit() 之前设置了断点,当程序执行到断点的时候,到数据库上查询,发现数据已经更改完成;
对于mysql配置为autocommit,这种情况下是不支持事务吧。
在eng.NewSession() 的时候,是否应该mysql session级别 的autocommit 关闭?

@lunny
Copy link
Member

lunny commented Oct 19, 2018

--- affectRows,err=eng1.Cols("other").Update(&t{Other:t1.Other},&t{ID:1})
+++ affectRows,err=session.Cols("other").Update(&t{Other:t1.Other},&t{ID:1})

@lunny lunny added the question label Oct 19, 2018
@qianxiansheng90
Copy link
Author

好吧,我的锅
thanks

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

No branches or pull requests

3 participants