This topic shows you how to start, finish and roll back a transaction with the Comms Database.
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.
Before you start, you must understand:
Make sure that you have created a session before you start your first transaction.
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: CMDBSession::OpenTransactionL(), CMDBSession::CommitTransactionL() and CMDBSession::RollbackTransactionL().Example:
... // 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(); ... } ...
Example:
... 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() ... } ...