<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Do we have to start transaction and dispose it in every subroutine? in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/do-we-have-to-start-transaction-and-dispose-it-in-every/m-p/5077666#M43777</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Keep in mind a transaction works like an undo grouping. So, you don't need to start a transaction in every Sub (nested transactions) except if you need the ability to roll back the changes done in in this sub.&lt;/P&gt;
&lt;P&gt;You can start a transaction (if needed) in your main Sub (i.e. a CommandMethod attributed method) and pass it as argument (or get it from another object passed as argument) to the methods the main Sub calls.&lt;/P&gt;
&lt;P&gt;And always dispose a transaction (Using statement is the safer way).&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;But never start a transaction (nor make Document, Database or Editor variables instanciation) from a Module. I don't know much about VB but it seems to me a Module is equivalent to a C# public static class (shared in VB) wich means the module members have a global application scope.&lt;/P&gt;
&lt;P&gt;If you instantiate some variables in a module, with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dim doc As Document = Application.DocumentManager.MdiActiveDocument&lt;/P&gt;
&lt;P&gt;Dim db As Database = doc.Database&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the doc variable is instantiated with the document which was active when the application loaded (the same for db) and these two global variables will always be available when you switch to another document but still bound to the first document ans its database.&lt;/P&gt;</description>
    <pubDate>Thu, 05 Jun 2014 19:23:42 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2014-06-05T19:23:42Z</dc:date>
    <item>
      <title>Do we have to start transaction and dispose it in every subroutine?</title>
      <link>https://forums.autodesk.com/t5/net-forum/do-we-have-to-start-transaction-and-dispose-it-in-every/m-p/5067544#M43775</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;Why I am asking this is; I was declaring module base Public variables like Acad, doc, mspace and assiging them once in a sub and using through whole program. But what I see in .net example codes is always start transaction and dispose. Lets say I want to get polyline vertices from&amp;nbsp; a sub or something else. Which is better to start transaction and dispose evertime the sub is called. Or like I said at the beginnig to assign transaction or document or database to public variables and use them throughout the program.&lt;/P&gt;&lt;P&gt;What precautions sholud we take to prevent crash? Somtimes when transaction is started and an error occurs ,autocad crashes. I am not so familiar with .net.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Jun 2014 21:22:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/do-we-have-to-start-transaction-and-dispose-it-in-every/m-p/5067544#M43775</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-06-01T21:22:11Z</dc:date>
    </item>
    <item>
      <title>Re: Do we have to start transaction and dispose it in every subroutine?</title>
      <link>https://forums.autodesk.com/t5/net-forum/do-we-have-to-start-transaction-and-dispose-it-in-every/m-p/5077388#M43776</link>
      <description>&lt;P&gt;the best way to handle transactions is this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Using trans As Transaction = db.TransactionManager.StartTransaction 'db is the Database object of current locked document
                Try
                    'do your stuff, you need a transaction to deal with database resident objects, access they properties, add new objects and so on.&lt;BR /&gt;                    'if you need some of this properties out of this block, you can get it from the object and assign to a variable of out side,&lt;BR /&gt;                    'but is better to keep the object here. Accessing database residents objects out of a transaction may cause a crash.
                    trans.Commit() 'commit it every time that this is possible, this is less expensive to the program.
                Catch ex As System.Exception
                    'make something when the program finds an error
                    trans.Abort() 'abort it if something goes wrong, but if you just have acessed objects properties and havent change anything is best to commit
                End Try
            End Using 'with "Using" you dont need do dispose the transaction&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2014 17:37:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/do-we-have-to-start-transaction-and-dispose-it-in-every/m-p/5077388#M43776</guid>
      <dc:creator>Gdiael</dc:creator>
      <dc:date>2014-06-05T17:37:44Z</dc:date>
    </item>
    <item>
      <title>Re: Do we have to start transaction and dispose it in every subroutine?</title>
      <link>https://forums.autodesk.com/t5/net-forum/do-we-have-to-start-transaction-and-dispose-it-in-every/m-p/5077666#M43777</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Keep in mind a transaction works like an undo grouping. So, you don't need to start a transaction in every Sub (nested transactions) except if you need the ability to roll back the changes done in in this sub.&lt;/P&gt;
&lt;P&gt;You can start a transaction (if needed) in your main Sub (i.e. a CommandMethod attributed method) and pass it as argument (or get it from another object passed as argument) to the methods the main Sub calls.&lt;/P&gt;
&lt;P&gt;And always dispose a transaction (Using statement is the safer way).&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;But never start a transaction (nor make Document, Database or Editor variables instanciation) from a Module. I don't know much about VB but it seems to me a Module is equivalent to a C# public static class (shared in VB) wich means the module members have a global application scope.&lt;/P&gt;
&lt;P&gt;If you instantiate some variables in a module, with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dim doc As Document = Application.DocumentManager.MdiActiveDocument&lt;/P&gt;
&lt;P&gt;Dim db As Database = doc.Database&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the doc variable is instantiated with the document which was active when the application loaded (the same for db) and these two global variables will always be available when you switch to another document but still bound to the first document ans its database.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2014 19:23:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/do-we-have-to-start-transaction-and-dispose-it-in-every/m-p/5077666#M43777</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2014-06-05T19:23:42Z</dc:date>
    </item>
    <item>
      <title>Re: Do we have to start transaction and dispose it in every subroutine?</title>
      <link>https://forums.autodesk.com/t5/net-forum/do-we-have-to-start-transaction-and-dispose-it-in-every/m-p/5077902#M43779</link>
      <description>&lt;P&gt;Gdiael a écrit&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;the best way to handle transactions is this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Using trans As Transaction = db.TransactionManager.StartTransaction 'db is the Database object of current locked document
                Try
                    'do your stuff, you need a transaction to deal with database resident objects, access they properties, add new objects and so on.&lt;BR /&gt;                    'if you need some of this properties out of this block, you can get it from the object and assign to a variable of out side,&lt;BR /&gt;                    'but is better to keep the object here. Accessing database residents objects out of a transaction may cause a crash.
                    trans.Commit() 'commit it every time that this is possible, this is less expensive to the program.
                Catch ex As System.Exception
                    'make something when the program finds an error
                    trans.Abort() 'abort it if something goes wrong, but if you just have acessed objects properties and havent change anything is best to commit
                End Try
            End Using 'with "Using" you dont need do dispose the transaction&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;You do not need to Abort the transaction in the Catch. If a transaction isn't committed, it is automatically aborted (rolled back) this is why it's better to always commit it (when needed).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Using tr As Transaction =  db.TransactionManager.StartTransaction()
    ' do your stuff here, using Try / Catch if needed
    tr.Commit()
End Using&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;is equivalent to:&lt;/P&gt;
&lt;PRE&gt;Dim tr As Transaction = db.TransactionManager.StartTransaction()
Try 
    ' do your stuff here, using nested Try / Catch if needed&lt;BR /&gt;    tr.Commit()&lt;BR /&gt;Catch&lt;BR /&gt;    tr.Abort()
Finally 
    tr.Dispose()
End Try&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Jun 2014 20:26:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/do-we-have-to-start-transaction-and-dispose-it-in-every/m-p/5077902#M43779</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2014-06-05T20:26:02Z</dc:date>
    </item>
    <item>
      <title>Re: Do we have to start transaction and dispose it in every subroutine?</title>
      <link>https://forums.autodesk.com/t5/net-forum/do-we-have-to-start-transaction-and-dispose-it-in-every/m-p/5079384#M43780</link>
      <description>Thank you very much you nade my mind clear now.</description>
      <pubDate>Fri, 06 Jun 2014 13:21:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/do-we-have-to-start-transaction-and-dispose-it-in-every/m-p/5079384#M43780</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-06-06T13:21:20Z</dc:date>
    </item>
  </channel>
</rss>

