SQL/数据库事务在使用MS SQL时无法正确回滚。

huangapple go评论85阅读模式
英文:

sql/db transaction not rolling back properly with ms sql

问题

所以我不能过于具体,但我认为我可以告诉你的内容足以解决这个问题。首先,我正在使用gorp来设置和获取事务。我正在使用github.com/denisenkom/go-mssqldb驱动程序。

然后,我执行一系列操作,如果其中任何一个失败,我会执行rollback操作,如果全部成功,我会执行commit操作。问题是,它只会回滚失败的语句,而不会回滚其他操作。我是否错误地认为这不是它应该工作的方式?

以下是一些伪代码,以便更好地理解我的问题:

trans,err := dbmap.Begin()
//假设在继续之前已经接收并检查了所有错误
id := trans.Exec("insert thing") //成功,回滚后仍然存在
thing := trans.Select("Select thing") //成功
trans.Exec("update other_thing with thing") //成功,回滚后仍然存在
newthing := trans.Exec("insert new_thing with thing") //失败,回滚
if err != nil{
   trans.Rollback() //没有错误
   return
}
trans.Commit()

我是否错误地认为应该从dbmap.Begin()开始回滚所有内容?这是驱动程序实现中的一个错误吗?非常感谢任何帮助。

更新

测试了https://play.golang.org/p/0L3Vgk8C_F,它可以正常工作,所以我猜这可能与gorp有关。我正在使用v1分支,因为这很快将投入生产,所以稳定性很重要。我会仔细检查它,但看起来它只是轻微地包装了一下。

英文:

So I can't get too specific, but I think what I can tell you will be enough to figure this out. First I'm using gorp to set things up and get the transaction. I am using the github.com/denisenkom/go-mssqldb driver.

Then I run through a series of operations and if of them fails I rollback and if all have success I commit. The problem is that it's only rolling back the statement that failed and not the rest of the operations. Am I wrong that this is not how that is suppose to work?

Here is some rough psudocode to give you a better idea of what I'm talking about:

trans,err := dbmap.Begin()
//assume all errors are received and checked before continuing
id := trans.Exec("insert thing") //successful, persists after rollback 
thing := trans.Select("Select thing") //successful
trans.Exec("update other_thing with thing") //successful, persists after rollback
newthing := trans.Exec("insert new_thing with thing") //fails, rollsback
if err != nil{
   trans.Rollback() //No errors
   return
}
trans.Commit()

Am I wrong that that should rollback everything since dbmap.Begin()? Is this a bug in the driver implementation? Any and all help is GREATLY welcome. Thanks!

Update

Tested https://play.golang.org/p/0L3Vgk8C_F and it worked so I'm guessing that means it something to do with gorp. I'm using the v1 branch since this will be production soon and so stability is key. I'll be picking through it, but it looks like it's just wrapping it lightly.

答案1

得分: 2

请检查您是否启用了自动提交功能。许多命令行工具、用户界面甚至驱动程序默认启用了该功能。

英文:

Check you don't have autocomit enabled. Many command line tools, uis and even drivers enable it by default

答案2

得分: 0

根据我阅读的一些内容(链接:https://technet.microsoft.com/en-us/library/ms187878(v=sql.105).aspx),默认情况下,autocommit似乎是开启的,这让我得出结论,如果go-mssqldb的作者没有为此功能实现开关,那么它应该是开启的。

你可以自己切换它,只需搜索"SET IMPLICIT_TRANSACTIONS"。

英文:

According to this( I read just a few lines) https://technet.microsoft.com/en-us/library/ms187878(v=sql.105).aspx

autocommit seems to be ON by defauly, that leads me to conclusion that if the author of go-mssqldb didn't implement on/off for this feature its ON.

You can toggle it yourself just search for SET IMPLICIT_TRANSACTIONS

huangapple
  • 本文由 发表于 2015年9月2日 11:18:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/32344117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定