Symbian3/SDK/Source/GUID-442F2E69-87B5-54E7-B62A-70B742432372.dita
author Dominic Pinkman <dominic.pinkman@nokia.com>
Tue, 20 Jul 2010 12:00:49 +0100
changeset 13 48780e181b38
parent 7 51a74ef9ed63
permissions -rw-r--r--
Week 28 contribution of SDK documentation content. See release notes for details. Fixes bugs Bug 1897 and Bug 1522.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-442F2E69-87B5-54E7-B62A-70B742432372" xml:lang="en"><title>How
to Commit and Revert </title><shortdesc>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)</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>Transactions are supported by a commit and revert protocol.</p>
<p>In the following code fragment, <codeph>store</codeph> is a pointer to
an opened permanent file store. The function <codeph>doUpdateStoreL()</codeph> adds
a number of streams to this store and, if successful, establishes a new commit
point.</p>
<p>If a failure occurs or the call to <codeph>CommitL()</codeph> fails, then <codeph>doUpdateStoreL()</codeph> 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.</p>
<p><codeph>CItemArray</codeph> is a class that encapsulates an array of <codeph>TItem</codeph> objects;
the relevant part of the definition is:</p>
<codeblock id="GUID-0B5B04A7-AB4A-55CA-BCF0-9E19623F1554" xml:space="preserve">typedef TBuf&lt;100&gt; TItem;</codeblock>
<codeblock id="GUID-52A3FAC2-4E23-5574-A21C-DE087ADDB911" xml:space="preserve">class CItemArray : public CBase
    {
public:
    ...
    void AddItemL(const TItem&amp; anItem);
    void StoreL() const;
       void ExternalizeL(RWriteStream&amp; aStream) const;
    ...
private:
    CStreamStore&amp; iStore;
       ...
    CArrayFixFlat&lt;TStreamId&gt;* iArray;
    }</codeblock>
<p>The example also assumes that the store is one that allows streams to be
modified and replaced; for example, a permanent file store.</p>
<codeblock id="GUID-21AFD117-3D52-55BE-BEB7-430CF08D0429" xml:space="preserve">...
TRAPD(error,doUpdateStoreL(*store));
if (error!=KErrNone)
    {
    store-&gt;Revert();
    ...
    }
...</codeblock>
<codeblock id="GUID-EB07D2AA-ECAA-5014-84F0-85767A60E366" xml:space="preserve">LOCAL_C void doUpdateStoreL(CPersistentStore&amp; 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-&gt;AddItemL(item);
    item = KTxtWorld;
    array-&gt;AddItemL(item);
             // Re-write the root stream with new data
    array-&gt;StoreL();
            // commit all changes
    aStore.CommitL();
    ...
    }</codeblock>
<codeblock id="GUID-3E2184DA-7C74-58F0-925E-6AE1317732C9" xml:space="preserve">void CItemArray::AddItemL(const TItem&amp; anItem)
    {
    RStoreWriteStream outstream;
    TStreamId id=outstream.CreateLC(iStore);
    outstream&lt;&lt;anItem;
    outstream.CommitL();
    CleanupStack::PopAndDestroy();
    iArray-&gt;AppendL(id);
    }
</codeblock>
</conbody><related-links>
<link href="GUID-C9D8D913-C65F-5A69-A606-30F59BFB38E2.dita"><linktext>File Stores</linktext>
</link>
</related-links></concept>