Special

Clearance Sale!

We've been publishing for over five years now and it's time to clear out our inventory of back issues, so we're slashing prices!

RBD Magazines

Check out this amazing clearance sale of all our past issues. Missing some issues? This is a great time to complete your RBD collection. Save up to 40% off the regular price of our printed back issue packages. These prices are only good until the end of the year May 2008 and supplies are limited, so place your order today.

Article Preview


Buy Now

Print:
PDF:

Object-Oriented Thinking

Automatic Transaction Rollback

Leverage REALbasic Memory Management

Issue: 5.2 (January/February 2007)
Author: Charles Yeomans
Article Description: No description available.
Article Length (in bytes): 5,391
Starting Page Number: 37
RBD Number: 5214
Resource File(s): None
Related Web Link(s):

http://www.declareSub.com/

Known Limitations: None

Excerpt of article text...

Database transactions require a lot of error-checking code. However, we can take advantage of memory management in REALbasic to simplify error-checking and make it more robust.

Here is a typical example block of database transaction code.

Sub PerformExampleTransaction(db as Database)

db.SQLExecute "INSERT INTO table1 ('foo')

if db.Error then

db.Rollback

return

end if

db.SQLExecute "INSERT INTO table2 ('foo')

if db.Error then

db.Rollback

return

end if

//etc.

db.Commit

exception oops

db.Rollback

//re-raise exception or not, depending on general error-handling strategy

End Sub

The problem here is that you have to check for an error after each database operation, and roll back the changes if an error occurred. You also have to anticipate the possibility of an exception and roll back changes if one was raised. For the example code above, this is simple. But note that most of the code above is devoted to error-handling. For a complicated transaction, this will add up. More code means more opportunities for bugs. Worse yet, most of the error-handling code is repeated. What happens when we decide to change how we handle errors?

...End of Excerpt. Please purchase the magazine to read the full article.

Article copyrighted by REALbasic Developer magazine. All rights reserved.


 


|

 


Weblog Commenting and Trackback by HaloScan.com