Why is it better to wrap transactions inside a using block?

Why is it better to wrap transactions inside a using block?

adam.krug
Advocate Advocate
424 Views
1 Reply
Message 1 of 2

Why is it better to wrap transactions inside a using block?

adam.krug
Advocate
Advocate

I read here and there on the net that it is always better to set up a transaction like below:

 

 

 

using (Transaction  tx = new Transaction(doc, name))
{
 tx.Start();
 some stuff here
 tx.Commit();
}

The alternative is like:

 

 

Transaction tx = new Transaction(doc, name);
tx.Start();
some stuff here
tx.Commit();

What is the reason the first is better than the latter? Or you have a different opinion? Please share.

 

0 Likes
Accepted solutions (1)
425 Views
1 Reply
Reply (1)
Message 2 of 2

cwaluga
Advocate
Advocate
Accepted solution

The Transaction class implements IDisposable, so it is just a matter of controlling when unmanaged resources are released.