How to Commit and Revert

Changes to a store are not made permanent until they are committed and until such changes are committed, they can be rolled back (i.e. reverted)

Transactions are supported by a commit and revert protocol.

In the following code fragment, store is a pointer to an opened permanent file store. The function doUpdateStoreL() adds a number of streams to this store and, if successful, establishes a new commit point.

If a failure occurs or the call to CommitL() fails, then doUpdateStoreL() leaves; the leave is trapped and any changes made to the store are rolled back, so that the store is in the same state as it was before the changes were started. The store, therefore, remains in a consistent state in the event of failure.

CItemArray is a class that encapsulates an array of TItem objects; the relevant part of the definition is:

typedef TBuf<100> TItem;
class CItemArray : public CBase
    void AddItemL(const TItem& anItem);
    void StoreL() const;
       void ExternalizeL(RWriteStream& aStream) const;
    CStreamStore& iStore;
    CArrayFixFlat<TStreamId>* iArray;

The example also assumes that the store is one that allows streams to be modified and replaced; for example, a permanent file store.

if (error!=KErrNone)
LOCAL_C void doUpdateStoreL(CPersistentStore& aStore)
    _LIT(KtxtWorld," world!");
              // get the root stream into memory
    CItemArray* array=CItemArray::NewLC(aStore,aStore.Root());
              // Add some items
    TItem item;
    item = KTxtHello;
    item = KTxtWorld;
             // Re-write the root stream with new data
            // commit all changes
void CItemArray::AddItemL(const TItem& anItem)
    RStoreWriteStream outstream;
    TStreamId id=outstream.CreateLC(iStore);
Related concepts
File Stores