Transaction Processing

To improve your application's performance and to ensure that database activities can be recovered in case an unexpected hardware or software error occurs, consider grouping sets of database activities into a transaction. A transaction is a set of operations that are performed together as as a single unit. If you use an automatic teller machine (ATM), you are already familiar with transaction processing. When you go to the bank to get cash, your account must be debited. In other words, the cash withdrawal must be deducted from your savings or checking account. A transaction is a two-sided operation. If anything goes wrong during the transaction, the entire transaction is cancelled. If both operations succeed, that is, you get the cash and the bank debits your account, the transaction's work is saved (or committed).

Database transactions often involve modifications and additions of one or more records in a single table or in several tables. When a transaction has to be undone or cancelled, the transaction is rolled back. Often, when you perform batch updates to database tables and an error occurs, updates to all tables must be cancelled or the database could be left in an inconsistent state.

Transactions are extremely important for maintaining data integrity and consistency. If you don't use transactions for operations that should be performed together, the database could be left in an inconsistent state, resulting not only in loss of important information but also in a number of other headaches.

In ADO, the Connection object offers three methods (BeginTrans, CommitTrans, and RollbackTrans) for managing transaction processing. You should use these methods to save or cancel a series of changes made to the data as a single unit.

■ BeginTrans — Begins a new transaction.

CommitTrans — Saves any changes and ends the current transaction. RollbackTrans — Cancels any changes made during the current transaction and ends the transaction.

Please note that in ADO a transaction is limited to one database because the Connection object can only point to one database.

0 0

Post a comment