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 { public: ... void AddItemL(const TItem& anItem); void StoreL() const; void ExternalizeL(RWriteStream& aStream) const; ... private: 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.
... TRAPD(error,doUpdateStoreL(*store)); if (error!=KErrNone) { store->Revert(); ... } ...
LOCAL_C void doUpdateStoreL(CPersistentStore& aStore) { _LIT(KTxtHello,"hello"); _LIT(KtxtWorld," world!"); // get the root stream into memory CItemArray* array=CItemArray::NewLC(aStore,aStore.Root()); // Add some items TItem item; item = KTxtHello; array->AddItemL(item); item = KTxtWorld; array->AddItemL(item); // Re-write the root stream with new data array->StoreL(); // commit all changes aStore.CommitL(); ... }
void CItemArray::AddItemL(const TItem& anItem) { RStoreWriteStream outstream; TStreamId id=outstream.CreateLC(iStore); outstream<<anItem; outstream.CommitL(); CleanupStack::PopAndDestroy(); iArray->AppendL(id); }
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.