Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql/db transaction not rolling back properly with ms sql

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.

like image 952
Matthew Clark Avatar asked Sep 02 '15 03:09

Matthew Clark


1 Answers

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

like image 126
Sedy Vlk Avatar answered Sep 24 '22 03:09

Sedy Vlk