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 // No traces. |
|
88 return iDb; |
|
89 } |
|
90 |
|
91 // --------------------------------------------------------------------------- |
|
92 // Opens a CommsDat transaction if it is not already open. Reference counter |
|
93 // is inreased by one. |
|
94 // --------------------------------------------------------------------------- |
|
95 // |
|
96 void CCmmTransactionHandler::OpenTransactionLC() |
|
97 { |
|
98 OstTraceFunctionEntry0( CCMTRANSACTIONHANDLER_OPENTRANSACTIONLC_ENTRY ); |
|
99 |
|
100 iRefCount++; |
|
101 CleanupClosePushL( *this ); |
|
102 |
|
103 if ( !iDb.IsInTransaction() ) |
|
104 { |
|
105 TInt err( KErrNone ); |
|
106 TUint32 attempts( KMaxOpenTransAttempts ); |
|
107 |
|
108 do |
|
109 { |
|
110 TRAP( err, iDb.OpenTransactionL() ); |
|
111 if ( err ) |
|
112 { |
|
113 User::After( KRetryAfter ); |
|
114 } |
|
115 } while ( err && attempts-- ); |
|
116 User::LeaveIfError( err ); |
|
117 } |
|
118 |
|
119 OstTraceFunctionExit0( CCMTRANSACTIONHANDLER_OPENTRANSACTIONLC_EXIT ); |
|
120 } |
|
121 |
|
122 // --------------------------------------------------------------------------- |
|
123 // Decreases the reference counter by one. If it reaches zero, commits the open |
|
124 // CommsDat transaction. If an error code is given as argument, the CommsDat |
|
125 // transaction is rolled back instead. |
|
126 // --------------------------------------------------------------------------- |
|
127 // |
|
128 void CCmmTransactionHandler::CommitTransactionL( TInt aError ) |
|
129 { |
|
130 OstTraceFunctionEntry0( CCMTRANSACTIONHANDLER_COMMITTRANSACTIONL_ENTRY ); |
|
131 |
|
132 iRefCount--; |
|
133 CleanupStack::Pop( this ); |
|
134 |
|
135 if ( !iRefCount ) |
|
136 { |
|
137 if ( aError ) |
|
138 { |
|
139 iDb.RollbackTransactionL(); |
|
140 } |
|
141 else |
|
142 { |
|
143 iDb.CommitTransactionL(); |
|
144 } |
|
145 } |
|
146 |
|
147 OstTraceFunctionExit0( CCMTRANSACTIONHANDLER_COMMITTRANSACTIONL_EXIT ); |
|
148 } |
|
149 |
|
150 // --------------------------------------------------------------------------- |
|
151 // Performs RollbackTransactionL(). |
|
152 // Pay attention to CleanupStack if calling this. The transaction handler |
|
153 // needs to be popped from CleanupStack manually. |
|
154 // --------------------------------------------------------------------------- |
|
155 // |
|
156 void CCmmTransactionHandler::Close() |
|
157 { |
|
158 OstTraceFunctionEntry0( CCMTRANSACTIONHANDLER_CLOSE_ENTRY ); |
|
159 |
|
160 if ( iRefCount == 0 ) |
|
161 { |
|
162 // No active transaction, do nothing. |
|
163 OstTraceFunctionExit0( DUP1_CCMTRANSACTIONHANDLER_CLOSE_EXIT ); |
|
164 return; |
|
165 } |
|
166 |
|
167 if ( !iDb.IsInTransaction() ) |
|
168 { |
|
169 // Sometimes CommsDat closes the transaction on its own decision w/o any |
|
170 // notification or reason. E.g. when you try to delete a non-existing |
|
171 // record, it leaves with KErrNotFound, but rolls back the transaction. |
|
172 iRefCount = 0; |
|
173 } |
|
174 else |
|
175 { |
|
176 iRefCount--; |
|
177 |
|
178 if ( !iRefCount ) |
|
179 { |
|
180 if ( iDb.IsInTransaction() ) |
|
181 { |
|
182 TRAP_IGNORE( iDb.RollbackTransactionL() ); |
|
183 } |
|
184 } |
|
185 } |
|
186 |
|
187 OstTraceFunctionExit0( DUP2_CCMTRANSACTIONHANDLER_CLOSE_EXIT ); |
|
188 } |
|
189 |
|
190 // --------------------------------------------------------------------------- |
|
191 // Return the current reference count. Transaction is currently open if the |
|
192 // count above 0. |
|
193 // --------------------------------------------------------------------------- |
|
194 // |
|
195 TUint32 CCmmTransactionHandler::GetReferenceCount() |
|
196 { |
|
197 OstTraceFunctionEntry0( CCMTRANSACTIONHANDLER_GETREFERENCECOUNT_ENTRY ); |
|
198 OstTraceFunctionExit0( CCMTRANSACTIONHANDLER_GETREFERENCECOUNT_EXIT ); |
|
199 return iRefCount; |
|
200 } |
|
201 |
|
202 // End of file |
|