|
1 /* |
|
2 * Copyright (c) 2006 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: Common transaction handler of framework and plugins. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <e32std.h> |
|
19 #include <e32base.h> |
|
20 |
|
21 #include "cmlogger.h" |
|
22 #include "cmmanagerimpl.h" |
|
23 #include "cmtransactionhandler.h" |
|
24 |
|
25 using namespace CommsDat; |
|
26 |
|
27 const TUint32 KMaxOpenTransAttempts = 10; |
|
28 const TUint32 KRetryAfter = 100000; |
|
29 |
|
30 // --------------------------------------------------------------------------- |
|
31 // CCmTransactionHandler::NewL |
|
32 // --------------------------------------------------------------------------- |
|
33 // |
|
34 CCmTransactionHandler* CCmTransactionHandler::NewL( CMDBSession& aDb ) |
|
35 { |
|
36 CCmTransactionHandler* self = new (ELeave) CCmTransactionHandler( aDb ); |
|
37 CleanupStack::PushL( self ); |
|
38 self->ConstructL(); |
|
39 CleanupStack::Pop( self ); // self |
|
40 return self; |
|
41 } |
|
42 |
|
43 // --------------------------------------------------------------------------- |
|
44 // CCmTransactionHandler::CCmTransactionHandler |
|
45 // --------------------------------------------------------------------------- |
|
46 // |
|
47 CCmTransactionHandler::CCmTransactionHandler( CMDBSession& aDb ) |
|
48 : iDb( aDb ) |
|
49 { |
|
50 CLOG_CREATE; |
|
51 } |
|
52 |
|
53 // --------------------------------------------------------------------------- |
|
54 // CCmTransactionHandler::ConstructL |
|
55 // --------------------------------------------------------------------------- |
|
56 // |
|
57 void CCmTransactionHandler::ConstructL() |
|
58 { |
|
59 TRAPD( err, CCmManagerImpl::HasCapabilityL( ECapabilityNetworkControl ) ); |
|
60 |
|
61 if( !err ) |
|
62 { |
|
63 iProtectionFlag = ECDProtectedWrite; |
|
64 } |
|
65 else |
|
66 { |
|
67 iProtectionFlag = 0; |
|
68 } |
|
69 } |
|
70 |
|
71 // --------------------------------------------------------------------------- |
|
72 // CCmTransactionHandler::~CCmTransactionHandler |
|
73 // --------------------------------------------------------------------------- |
|
74 // |
|
75 CCmTransactionHandler::~CCmTransactionHandler() |
|
76 { |
|
77 CLOG_CLOSE; |
|
78 } |
|
79 |
|
80 // --------------------------------------------------------------------------- |
|
81 // CCmTransactionHandler::OpenTransactionLC |
|
82 // --------------------------------------------------------------------------- |
|
83 // |
|
84 void CCmTransactionHandler::OpenTransactionLC( TBool aSetAttribs ) |
|
85 { |
|
86 LOGGER_ENTERFN( "CCmTransactionHandler::OpenTransactionL" ); |
|
87 |
|
88 ++iRefCount; |
|
89 CLOG_WRITE_1( "Refs: [%d]", iRefCount ); |
|
90 |
|
91 // To call ::Close() on leave |
|
92 CleanupClosePushL( *this ); |
|
93 |
|
94 // We don't have parent destination -> Session is opened only once |
|
95 if( !iDb.IsInTransaction() ) |
|
96 { |
|
97 TInt err( KErrNone ); |
|
98 TUint32 attempts( KMaxOpenTransAttempts ); |
|
99 |
|
100 do |
|
101 { |
|
102 CLOG_WRITE( "Opening" ); |
|
103 TRAP( err, iDb.OpenTransactionL() ); |
|
104 CLOG_WRITE_1( "Error: [%d]", err ); |
|
105 |
|
106 if( err ) |
|
107 { |
|
108 User::After( KRetryAfter ); |
|
109 } |
|
110 }while( err && attempts-- ); |
|
111 |
|
112 User::LeaveIfError( err ); |
|
113 |
|
114 if( aSetAttribs ) |
|
115 { |
|
116 iDb.SetAttributeMask( ECDHidden | iProtectionFlag ); |
|
117 } |
|
118 } |
|
119 } |
|
120 |
|
121 // --------------------------------------------------------------------------- |
|
122 // CCmTransactionHandler::CommitTransactionL |
|
123 // --------------------------------------------------------------------------- |
|
124 // |
|
125 void CCmTransactionHandler::CommitTransactionL( TInt aError ) |
|
126 { |
|
127 LOGGER_ENTERFN( "CCmTransactionHandler::CommitTransactionL" ); |
|
128 |
|
129 --iRefCount; |
|
130 CLOG_WRITE_1( "ref: [%d]", iRefCount ); |
|
131 CLOG_WRITE_1( "Error: [%d]", aError ); |
|
132 |
|
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 iDb.ClearAttributeMask( ECDHidden | ECDProtectedWrite ); |
|
147 } |
|
148 } |
|
149 |
|
150 // --------------------------------------------------------------------------- |
|
151 // CCmTransactionHandler::Close |
|
152 // --------------------------------------------------------------------------- |
|
153 // |
|
154 void CCmTransactionHandler::Close() |
|
155 { |
|
156 LOGGER_ENTERFN( "CCmTransactionHandler::Close" ); |
|
157 |
|
158 if( 0 == iRefCount ) |
|
159 { |
|
160 CLOG_WRITE( "No active transaction. Do nothing" ); |
|
161 return; |
|
162 } |
|
163 |
|
164 if( !iDb.IsInTransaction() ) |
|
165 // Sometimes CommsDat closes the transaction |
|
166 // on its own decision w/o any notification or reaseon. |
|
167 // e.g. when you try to delete a non-existing record. |
|
168 // It leaves with KErrNotFound, but rolls back the transaction. |
|
169 { |
|
170 CLOG_WRITE( "CommsDat already rolled back transaction. :(((" ); |
|
171 iRefCount = 0; |
|
172 } |
|
173 else |
|
174 { |
|
175 --iRefCount; |
|
176 CLOG_WRITE_1( "ref: [%d]", iRefCount ); |
|
177 |
|
178 if( !iRefCount ) |
|
179 { |
|
180 iDb.ClearAttributeMask( ECDHidden | ECDProtectedWrite ); |
|
181 |
|
182 if( iDb.IsInTransaction() ) |
|
183 { |
|
184 TRAP_IGNORE( iDb.RollbackTransactionL() ); |
|
185 } |
|
186 } |
|
187 } |
|
188 } |