Skip to content

Commit 24b3b5d

Browse files
committed
Events which can cause data loss are executed in context of disabled change registration
1 parent d853247 commit 24b3b5d

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

Orm/Xtensive.Orm/Orm/Session.Persist.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,14 @@ private async ValueTask Persist(PersistReason reason, bool isAsync, Cancellation
135135
var ts = await InnerOpenTransaction(
136136
TransactionOpenMode.Default, IsolationLevel.Unspecified, false, isAsync, token);
137137

138-
IDisposable changesGuard = null;
139138
try {
140139
IsPersisting = true;
141140
persistingIsFailed = false;
142141
SystemEvents.NotifyPersisting();
143142
Events.NotifyPersisting();
144143

145-
changesGuard = PreventRegistryChanges();
146144
using (OpenSystemLogicOnlyRegion()) {
147-
DemandTransaction();
145+
_ = DemandTransaction();
148146
if (IsDebugEventLoggingEnabled) {
149147
OrmLog.Debug(nameof(Strings.LogSessionXPersistingReasonY), this, reason);
150148
}
@@ -176,16 +174,11 @@ private async ValueTask Persist(PersistReason reason, bool isAsync, Cancellation
176174
}
177175
catch (Exception) {
178176
persistingIsFailed = true;
179-
changesGuard.Dispose();
180-
changesGuard = null;
181-
182177
RollbackChangesOfEntitySets();
183178
RestoreEntityChangesAfterPersistFailed();
184179
throw;
185180
}
186181
finally {
187-
changesGuard.DisposeSafely();
188-
changesGuard = null;
189182
if (persistIsSuccessful || !Configuration.Supports(SessionOptions.NonTransactionalEntityStates)) {
190183
DropDifferenceBackup();
191184
foreach (var item in itemsToPersist.GetItems(PersistenceState.New)) {
@@ -217,13 +210,13 @@ private async ValueTask Persist(PersistReason reason, bool isAsync, Cancellation
217210
}
218211
}
219212
}
220-
221-
SystemEvents.NotifyPersisted();
222-
Events.NotifyPersisted();
213+
using (PreventRegistryChanges()) {
214+
SystemEvents.NotifyPersisted();
215+
Events.NotifyPersisted();
216+
}
223217
}
224218
finally {
225219
IsPersisting = false;
226-
changesGuard.DisposeSafely();
227220
if (isAsync) {
228221
await ts.DisposeAsync().ConfigureAwait(false);
229222
}

Orm/Xtensive.Orm/Orm/Session.Transactions.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,10 @@ internal async ValueTask CommitTransaction(Transaction transaction, bool isAsync
259259

260260
ValidationContext.Validate(ValidationReason.Commit);
261261

262-
SystemEvents.NotifyTransactionCommitting(transaction);
263-
Events.NotifyTransactionCommitting(transaction);
262+
using (PreventRegistryChanges()) {
263+
SystemEvents.NotifyTransactionCommitting(transaction);
264+
Events.NotifyTransactionCommitting(transaction);
265+
}
264266

265267
Handler.CompletingTransaction(transaction);
266268
if (transaction.IsNested) {
@@ -366,24 +368,28 @@ internal void CompleteTransaction(Transaction transaction)
366368
Transaction = transaction.Outer;
367369

368370
switch (transaction.State) {
369-
case TransactionState.Committed:
370-
if (IsDebugEventLoggingEnabled) {
371-
OrmLog.Debug(nameof(Strings.LogSessionXCommittedTransaction), this);
372-
}
371+
case TransactionState.Committed:
372+
if (IsDebugEventLoggingEnabled) {
373+
OrmLog.Debug(nameof(Strings.LogSessionXCommittedTransaction), this);
374+
}
373375

374-
SystemEvents.NotifyTransactionCommitted(transaction);
375-
Events.NotifyTransactionCommitted(transaction);
376-
break;
377-
case TransactionState.RolledBack:
378-
if (IsDebugEventLoggingEnabled) {
379-
OrmLog.Debug(nameof(Strings.LogSessionXRolledBackTransaction), this);
380-
}
376+
using (PreventRegistryChanges()) {
377+
SystemEvents.NotifyTransactionCommitted(transaction);
378+
Events.NotifyTransactionCommitted(transaction);
379+
}
380+
break;
381+
case TransactionState.RolledBack:
382+
if (IsDebugEventLoggingEnabled) {
383+
OrmLog.Debug(nameof(Strings.LogSessionXRolledBackTransaction), this);
384+
}
381385

382-
SystemEvents.NotifyTransactionRollbacked(transaction);
383-
Events.NotifyTransactionRollbacked(transaction);
384-
break;
385-
default:
386-
throw new ArgumentOutOfRangeException("transaction.State");
386+
using (PreventRegistryChanges()) {
387+
SystemEvents.NotifyTransactionRollbacked(transaction);
388+
Events.NotifyTransactionRollbacked(transaction);
389+
}
390+
break;
391+
default:
392+
throw new ArgumentOutOfRangeException("transaction.State");
387393
}
388394
}
389395

0 commit comments

Comments
 (0)