.NET

Reply
Distinguished Contributor
JanetDavidson
Posts: 139
Registered: ‎08-23-2011
Message 1 of 3 (143 Views)
Accepted Solution

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

143 Views, 2 Replies
07-10-2012 10:08 PM

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

 

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.

 

 

*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 2 of 3 (121 Views)

Re: Which one of these transactions is right. I am confused when we need to comm

07-11-2012 09:05 AM in reply to: JanetDavidson

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
Distinguished Contributor
JanetDavidson
Posts: 139
Registered: ‎08-23-2011
Message 3 of 3 (97 Views)

Re: Which one of these transactions is right. I am confused when we need to comm

07-12-2012 03:18 PM in reply to: chiefbraincloud

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

Cheers.

Janet.

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!