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

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

4 REPLIES 4
Reply
Message 1 of 5
tanerpro
2307 Views, 4 Replies

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

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,

 

4 REPLIES 4
Message 2 of 5
Gdiael
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

 

 

Message 3 of 5
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 5
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 5
tanerpro
in reply to: _gile

Thank you very much you nade my mind clear now.

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