datastore.RunInTransaction

167 views
Skip to first unread message

Jeff

unread,
Dec 26, 2020, 10:54:36 PM12/26/20
to golang-nuts
I was curious how datastore.RunInTransaction was implemented in comparison to the Transaction method set so I took quick look at the source code.  However, I hope I am simply misunderstanding the source code for datastore.RunInTransaction at:


I "think" I understand what it is supposed to do.  In short, if the transaction fails it should retry a number of times.  But, when looking at the code, it appears to return after the first error and only succeed if it runs successfully through the for loop the number of times.  Thus, increasing the attempts would actually decrease the chances of successfully committing the transaction as it would need to be successful for each attempt.

Hopefully, my interpretation is wrong and someone can explain what is actually going on.

Thanks,
Jeff

peterGo

unread,
Dec 27, 2020, 3:04:32 AM12/27/20
to golang-nuts
Jeff,

Retry if err == ErrConcurrentTransaction.

Peter

Jeff

unread,
Dec 27, 2020, 12:12:17 PM12/27/20
to golang-nuts
Thanks.  I see now that if err == nil in the following code snippet, then the for loop will end.  Not sure why I find the effect of this err check confusing, but I do.  Probably its dual-use is the culprit.  Return on happy path (i.e., err == nil) and return on most errors.

if cmt, err := tx.Commit(); err != ErrConcurrentTransaction {
      return cmt, err
}
Reply all
Reply to author
Forward
0 new messages