.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
JanetDavidson
497 Views, 2 Replies

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

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

 

2 REPLIES 2
Message 2 of 3

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
Message 3 of 3

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

Cheers.

Janet.

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost