Adapter and transaction: explicit commit and implicit rollback

Posts   
 
    
Findev
User
Posts: 107
Joined: 08-Dec-2014
# Posted on: 23-Mar-2020 00:37:44   

Hi,

https://www.llblgen.com/Documentation/5.5/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/Adapter/gencode_transactions_adapter.htm

first example has a using block with a nested try-catch block. Is it safe to skip the try-catch and just call the .Commit() as the last statement in the using block? Quickly glanced into adapter's Dispose, it seems it calls .Rollback() if there's an active transaction.

Thank you!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Mar-2020 06:30:04   

Findev wrote:

Is it safe to skip the try-catch and just call the .Commit() as the last statement in the using block?

That's correct. The try-catch it's recommended anyway to manage possible errors (concurrency, timeouts, DB validation errors, etc). However a using block is recommended at least.

Findev wrote:

Quickly glanced into adapter's Dispose, it seems it calls .Rollback() if there's an active transaction.

Correct. This is also mentioned in the documentation:

First a DataAccessAdapter object is created and a transaction is explicitly started. As soon as you start the transaction, a database connection is open and usable. It's best practice to embed the usage of a transaction in a try/catch/finally statement as it is done in the example above, or a using block. This ensures that if something fails during the usage of the transaction, everything is rolled back or, at the end, everything is committed correctly. A** DataAccessAdapter instance will rollback an open transaction when it's disposed**.

David Elizondo | LLBLGen Support Team
Findev
User
Posts: 107
Joined: 08-Dec-2014
# Posted on: 23-Mar-2020 12:19:22   

I'd suppose catching in the caller will do the trick too, in the end, it depends where the exception handling is planned to be done.

Thank you! simple_smile