cmmanager/cmmgr/cmmserver/src/cmmtransactionhandler.cpp
changeset 20 9c97ad6591ae
child 40 c5b848e6c7d1
equal deleted inserted replaced
18:fcbbe021d614 20:9c97ad6591ae
       
     1 /*
       
     2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * Common transaction handler of framework and plugins.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "cmmtransactionhandler.h"
       
    21 
       
    22 #include "OstTraceDefinitions.h"
       
    23 #ifdef OST_TRACE_COMPILER_IN_USE
       
    24 #include "cmmtransactionhandlerTraces.h"
       
    25 #endif
       
    26 
       
    27 const TUint32 KMaxOpenTransAttempts = 10;
       
    28 const TUint32 KRetryAfter = 100000;
       
    29 
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // NewL.
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CCmmTransactionHandler* CCmmTransactionHandler::NewL( CommsDat::CMDBSession& aDb )
       
    36     {
       
    37     OstTraceFunctionEntry0( CCMTRANSACTIONHANDLER_NEWL_ENTRY );
       
    38 
       
    39     CCmmTransactionHandler* self = new( ELeave ) CCmmTransactionHandler( aDb );
       
    40     CleanupStack::PushL( self );
       
    41     self->ConstructL();
       
    42     CleanupStack::Pop( self );
       
    43 
       
    44     OstTraceFunctionExit0( CCMTRANSACTIONHANDLER_NEWL_EXIT );
       
    45     return self;
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // Destructor.
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CCmmTransactionHandler::~CCmmTransactionHandler()
       
    53     {
       
    54     OstTraceFunctionEntry0( CCMTRANSACTIONHANDLER_CCMTRANSACTIONHANDLER_ENTRY );
       
    55     delete &iDb;
       
    56     OstTraceFunctionExit0( CCMTRANSACTIONHANDLER_CCMTRANSACTIONHANDLER_EXIT );
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // Constructor.
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 CCmmTransactionHandler::CCmmTransactionHandler( CommsDat::CMDBSession& aDb ) : iDb( aDb )
       
    64     {
       
    65     OstTraceFunctionEntry0( DUP1_CCMTRANSACTIONHANDLER_CCMTRANSACTIONHANDLER_ENTRY );
       
    66     iRefCount = 0;
       
    67     OstTraceFunctionExit0( DUP1_CCMTRANSACTIONHANDLER_CCMTRANSACTIONHANDLER_EXIT );
       
    68     }
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // Second phase constructor.
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 void CCmmTransactionHandler::ConstructL()
       
    75     {
       
    76     OstTraceFunctionEntry0( CCMTRANSACTIONHANDLER_CONSTRUCTL_ENTRY );
       
    77     iDb.SetAttributeMask( CommsDat::ECDHidden | CommsDat::ECDProtectedWrite );
       
    78     OstTraceFunctionExit0( CCMTRANSACTIONHANDLER_CONSTRUCTL_EXIT );
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // Return the CommsDat session.
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 CommsDat::CMDBSession& CCmmTransactionHandler::Session() const
       
    86     {
       
    87     return iDb;
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // Opens a CommsDat transaction if it is not already open. Reference counter
       
    92 // is inreased by one.
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 void CCmmTransactionHandler::OpenTransactionLC()
       
    96     {
       
    97     OstTraceFunctionEntry0( CCMTRANSACTIONHANDLER_OPENTRANSACTIONLC_ENTRY );
       
    98 
       
    99     iRefCount++;
       
   100     CleanupClosePushL( *this );
       
   101 
       
   102     if ( !iDb.IsInTransaction() )
       
   103         {
       
   104         TInt err( KErrNone );
       
   105         TUint32 attempts( KMaxOpenTransAttempts );
       
   106 
       
   107         do
       
   108             {
       
   109             TRAP( err, iDb.OpenTransactionL() );
       
   110             if ( err )
       
   111                 {
       
   112                 User::After( KRetryAfter );
       
   113                 }
       
   114             } while ( err && attempts-- );
       
   115         User::LeaveIfError( err );
       
   116         }
       
   117 
       
   118     OstTraceFunctionExit0( CCMTRANSACTIONHANDLER_OPENTRANSACTIONLC_EXIT );
       
   119     }
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // Decreases the reference counter by one. If it reaches zero, commits the open
       
   123 // CommsDat transaction. If an error code is given as argument, the CommsDat
       
   124 // transaction is rolled back instead.
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 void CCmmTransactionHandler::CommitTransactionL( TInt aError )
       
   128     {
       
   129     OstTraceFunctionEntry0( CCMTRANSACTIONHANDLER_COMMITTRANSACTIONL_ENTRY );
       
   130 
       
   131     iRefCount--;
       
   132     CleanupStack::Pop( this );
       
   133 
       
   134     if ( !iRefCount )
       
   135         {
       
   136         if ( aError )
       
   137             {
       
   138             iDb.RollbackTransactionL();
       
   139             }
       
   140         else
       
   141             {
       
   142             iDb.CommitTransactionL();
       
   143             }
       
   144         }
       
   145 
       
   146     OstTraceFunctionExit0( CCMTRANSACTIONHANDLER_COMMITTRANSACTIONL_EXIT );
       
   147     }
       
   148 
       
   149 // ---------------------------------------------------------------------------
       
   150 // Performs RollbackTransactionL().
       
   151 // Pay attention to CleanupStack if calling this. The transaction handler
       
   152 // needs to be popped from CleanupStack manually.
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 void CCmmTransactionHandler::Close()
       
   156     {
       
   157     OstTraceFunctionEntry0( CCMTRANSACTIONHANDLER_CLOSE_ENTRY );
       
   158 
       
   159     if ( iRefCount == 0 )
       
   160         {
       
   161         // No active transaction, do nothing.
       
   162         OstTraceFunctionExit0( DUP1_CCMTRANSACTIONHANDLER_CLOSE_EXIT );
       
   163         return;
       
   164         }
       
   165 
       
   166     if ( !iDb.IsInTransaction() )
       
   167         {
       
   168         // Sometimes CommsDat closes the transaction on its own decision w/o any
       
   169         // notification or reason. E.g. when you try to delete a non-existing
       
   170         // record, it leaves with KErrNotFound, but rolls back the transaction.
       
   171         iRefCount = 0;
       
   172         }
       
   173     else
       
   174         {
       
   175         iRefCount--;
       
   176 
       
   177         if ( !iRefCount )
       
   178             {
       
   179             if ( iDb.IsInTransaction() )
       
   180                 {
       
   181                 TRAP_IGNORE( iDb.RollbackTransactionL() );
       
   182                 }
       
   183             }
       
   184         }
       
   185 
       
   186     OstTraceFunctionExit0( DUP2_CCMTRANSACTIONHANDLER_CLOSE_EXIT );
       
   187     }
       
   188 
       
   189 // ---------------------------------------------------------------------------
       
   190 // Return the current reference count. Transaction is currently open if the
       
   191 // count above 0.
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 TUint32 CCmmTransactionHandler::GetReferenceCount()
       
   195     {
       
   196     OstTraceFunctionEntry0( CCMTRANSACTIONHANDLER_GETREFERENCECOUNT_ENTRY );
       
   197     OstTraceFunctionExit0( CCMTRANSACTIONHANDLER_GETREFERENCECOUNT_EXIT );
       
   198     return iRefCount;
       
   199     }
       
   200 
       
   201 // End of file