Which one of these transactions is right. I am confused when we need to commit?

Which one of these transactions is right. I am confused when we need to commit?

JanetDavidson
Advocate Advocate
841 Views
2 Replies
Message 1 of 3

Which one of these transactions is right. I am confused when we need to commit?

JanetDavidson
Advocate
Advocate

Hi  gentlemen, see 2 functions below please. Which one is right ? Shall I commit Transaction or Not? I don't get any error at all in both cases but like to know what is right.

 

 

 

Function  Test() as Boolean

  Using tr As Transaction = Mydwg.TransactionManager.StartTransaction
     If BlahBlah = BlahBlahBlah Then 
            Return True
     End If
  tr.Commit()
  End Using
  Return False

End Function


Function  Test1() as Boolean

  Using tr As Transaction = Mydwg.TransactionManager.StartTransaction
     If BlahBlah = BlahBlahBlah Then 
         tr.commit   
         Return True
     End If
  tr.Commit()
  End Using
  Return False

End Function

 

0 Likes
Accepted solutions (1)
842 Views
2 Replies
Replies (2)
Message 2 of 3

chiefbraincloud
Collaborator
Collaborator
Accepted solution

Hopefully, there is something else going on inside your If block, or you don't need the transaction at all.  That said, in function Test, if BlahBlah = BlahBlahBlah then the transaction will not be committed, because the function returns before hitting the commit.  The only way you could not notice this in your real code is if the transaction is read only (or if you really don't need the transaction at all), otherwise you would see that whatever changes your code made were reverted.  Read-Only transactions should always be committed, because if you do not call commit, then when the transaction is disposed (at your End Using) tr.Abort is automatically called, and tr.Abort carries more overhead than tr.commit.

 

 

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 3 of 3

JanetDavidson
Advocate
Advocate

Thank you very much for explanation , I go with Test1 function then.

Cheers.

Janet.

 

 

0 Likes