Starting and Finishing Transactions: Tutorial

This topic shows you how to start, finish and roll back a transaction with the Comms Database.

Context

A transaction is a sequence of read and write operations. In a transaction all operations must be successful to make sure of the integrity of the data.

A tool or application must mark the start of a transaction. The tool or application must also mark the end of a transaction.

A transaction ends normally or the transaction is rolled back. A transaction is rolled back when one of the read or write operations fails. Other events can cause the tool or application to abandon the transaction. Other events depend on the purpose of the tool or application.

Create a session with the Comms Database before you start a transaction.

There are many ways to implement transactions. The procedure below is one method. The method shows the principle of transactions. The method you use depends on the organisation of your code.

Prerequisites

Before you start, you must understand:

  • the general concept of the Comms Database

  • the specific concept of Comms Database Transactions

  • how to write and build application code to run on Symbian platform

Steps

  1. Make sure that you have created a session before you start your first transaction.

  2. Start the transaction before you start the operations that access the database. Commit the transaction if all operations are successful. Rollback the transaction if any operation fails.

    The code uses the functions: [[[ERROR: [NOKX000E] Unable to find definition for key reference 'CommsDat']]]CMDBSession::OpenTransactionL(), [[[ERROR: [NOKX000E] Unable to find definition for key reference 'CommsDat']]]CMDBSession::CommitTransactionL() and [[[ERROR: [NOKX000E] Unable to find definition for key reference 'CommsDat']]]CMDBSession::RollbackTransactionL().
    ...
    
    // This code fragment assumes that a session with the Comms Database has been created.
    // iDb is a pointer to a CMDBSession object
    
    TInt rc;
    
    TRAP(rc,PerformSequenceOfOperationsL());
    if (!rc)
        {
        // Sequence of read and write operations has failed.
        iDb->RollbackTransactionL();
        ...
        }
    ...
    ...
    void PerformSequenceOfOperationsL()
        {
        // Start a transaction
        iDb->OpenTransactionL();
        ...
        // Sequence of read and write operations
        ...
        // Commit the changes to the Comms Database. This assumes that
        // all operations have completed successfully, and no other event has
        // caused the sequence of operations to be abandoned.
        iDb->CommitTransactionL()
        ...
        }
    ...

Related concepts