Symbian3/SDK/Source/GUID-442F2E69-87B5-54E7-B62A-70B742432372.dita
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Thu, 21 Jan 2010 18:18:20 +0000
changeset 0 89d6a7a84779
permissions -rw-r--r--
Initial contribution of Documentation_content according to Feature bug 1266 bug 1268 bug 1269 bug 1270 bug 1372 bug 1374 bug 1375 bug 1379 bug 1380 bug 1381 bug 1382 bug 1383 bug 1385

<?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 xml:lang="en" id="GUID-442F2E69-87B5-54E7-B62A-70B742432372"><title>How to commit and revert </title><prolog><metadata><keywords/></metadata></prolog><conbody><p>Transactions are supported by a commit and revert protocol. Changes to a store are not made permanent until they are committed. Until such changes are committed, they can be rolled back (i.e. reverted).</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> <section><title>See also</title> <p><xref href="GUID-C9D8D913-C65F-5A69-A606-30F59BFB38E2.dita">File stores</xref> </p> </section> </conbody></concept>