.NET

.NET

Reply
Distinguished Contributor
141 Posts
2 Kudos
Registered: ‎08-23-2011
Post 1 of 3
Accepted Solution

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

146 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*
753 Posts
54 Kudos
Registered: ‎02-13-2008
Post 2 of 3

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
141 Posts
2 Kudos
Registered: ‎08-23-2011
Post 3 of 3

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 interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!