.NET

Reply
Valued Contributor
tanerpro
Posts: 83
Registered: ‎10-07-2005
Message 1 of 5 (266 Views)

Do we have to start transaction and dispose it in every subroutine?

266 Views, 4 Replies
06-01-2014 02:22 PM

Hello,

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  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.

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.

Thanks,

 

Windows 7 (x64)
AutoCAD 2012 (x64)
Active Contributor
Gdiael
Posts: 26
Registered: ‎06-09-2012
Message 2 of 5 (196 Views)

Re: Do we have to start transaction and dispose it in every subroutine?

06-05-2014 10:37 AM in reply to: tanerpro

the best way to handle transactions is this

 

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.
'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,
'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

 

 

*Expert Elite*
_gile
Posts: 2,094
Registered: ‎04-29-2006
Message 3 of 5 (187 Views)

Re: Do we have to start transaction and dispose it in every subroutine?

06-05-2014 12:18 PM in reply to: Gdiael

Hi,

 

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.

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.

And always dispose a transaction (Using statement is the safer way).


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.

If you instantiate some variables in a module, with:

 

Dim doc As Document = Application.DocumentManager.MdiActiveDocument

Dim db As Database = doc.Database

...

 

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.

Gilles Chanteau
*Expert Elite*
_gile
Posts: 2,094
Registered: ‎04-29-2006
Message 4 of 5 (172 Views)

Re: Do we have to start transaction and dispose it in every subroutine?

06-05-2014 01:26 PM in reply to: Gdiael

Gdiael a écrit :

the best way to handle transactions is this

 

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.
'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,
'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

 

 


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).

 

Using tr As Transaction =  db.TransactionManager.StartTransaction()
    ' do your stuff here, using Try / Catch if needed
    tr.Commit()
End Using

 is equivalent to:

Dim tr As Transaction = db.TransactionManager.StartTransaction()
Try 
    ' do your stuff here, using nested Try / Catch if needed
tr.Commit()
Catch
tr.Abort() Finally tr.Dispose() End Try
Gilles Chanteau
Valued Contributor
tanerpro
Posts: 83
Registered: ‎10-07-2005
Message 5 of 5 (141 Views)

Re: Do we have to start transaction and dispose it in every subroutine?

06-06-2014 06:21 AM in reply to: _gile
Thank you very much you nade my mind clear now.
Windows 7 (x64)
AutoCAD 2012 (x64)
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.