Creating a Simple Transaction

Use the BeginTrans method to specify the beginning of a transaction. Use the CommitTran method to save the changes. BeginTrans and CommitTrans are used in pairs. The data-modifying instructions you place between these keywords are stored in memory until Visual Basic encounters the CommitTrans statement. After reaching CommitTrans, Access writes the changes that have occurred since the BeginTrans statement to the disk; therefore, any changes you've done in the tables become permanent.

If an error is generated during the transaction process, the RollbackTrans statement placed further down in your procedure will undo all changes made

Creating and Manipulating Databases with ADO

since the BeginTrans statement. The rollback ensures that the data is returned to the state it was in before you started the transaction.

Using transaction processing helps improve database performance since the operations carried out during a transaction are run in memory. If the transaction succeeds, the results are written to the disk in a single operation. If any operation included in a transaction fails, the transaction is simply aborted and no changes are written to the database. If you don't use transactions, the results of each operation must be written to the disk separately — a process that consumes more database resources.

The procedure in Hands-On 16-6 assumes that you want to enter an order for a new customer. Because this customer does not exist in the database, you will use a transaction to ensure that the new order is entered only after the customer record has been created in the Customers table.

0 0

Post a comment