Skip to content

Commit 1028f52

Browse files
committed
Implement token promotion to session when it is necessary
- promotion from transaction to session - session tokens expiry - no session token when it is not needed
1 parent 41264cf commit 1028f52

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created: 2008.11.07
66

77
using System;
8+
using System.Collections.Generic;
89
using System.Transactions;
910
using Xtensive.Orm.Configuration;
1011
using Xtensive.Orm.Internals;
@@ -18,7 +19,8 @@ public partial class Session
1819
{
1920
private const string SavepointNameFormat = "s{0}";
2021

21-
private readonly StateLifetimeToken sessionLifetimeToken = new StateLifetimeToken();
22+
private readonly StateLifetimeToken sessionLifetimeToken;
23+
private readonly List<StateLifetimeToken> promotedLifetimeTokens;
2224
private int nextSavepoint;
2325

2426
/// <summary>
@@ -338,5 +340,14 @@ internal StateLifetimeToken GetLifetimeToken()
338340
return sessionLifetimeToken;
339341
throw new InvalidOperationException(Strings.ExActiveTransactionIsRequiredForThisOperationUseSessionOpenTransactionToOpenIt);
340342
}
343+
344+
internal bool TryPromoteTokens(IEnumerable<StateLifetimeToken> tokens)
345+
{
346+
if (promotedLifetimeTokens is null) {
347+
return false;
348+
}
349+
promotedLifetimeTokens.AddRange(tokens);
350+
return true;
351+
}
341352
}
342353
}

Orm/Xtensive.Orm/Orm/Session.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,10 @@ internal Session(Domain domain, StorageNode selectedStorageNode, SessionConfigur
589589
remapper = new KeyRemapper(this);
590590

591591
disableAutoSaveChanges = !configuration.Supports(SessionOptions.AutoSaveChanges);
592+
if (configuration.Supports(SessionOptions.NonTransactionalReads)) {
593+
promotedLifetimeTokens = new List<StateLifetimeToken>();
594+
sessionLifetimeToken = new StateLifetimeToken();
595+
}
592596

593597
// Perform activation
594598
if (activate) {
@@ -613,6 +617,13 @@ public void Dispose()
613617
OrmLog.Debug(Strings.LogSessionXDisposing, this);
614618
}
615619

620+
sessionLifetimeToken?.Expire();
621+
if (promotedLifetimeTokens?.Count > 0) {
622+
foreach (var token in promotedLifetimeTokens) {
623+
token.Expire();
624+
}
625+
}
626+
616627
SystemEvents.NotifyDisposing();
617628
Events.NotifyDisposing();
618629

Orm/Xtensive.Orm/Orm/Transaction.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using System;
88
using System.Collections.Generic;
9+
using System.Linq;
910
using System.Transactions;
1011
using JetBrains.Annotations;
1112
using Xtensive.Collections;
@@ -188,10 +189,10 @@ internal void Commit()
188189
Rollback();
189190
throw;
190191
}
191-
if (Outer!=null)
192+
if (Outer != null)
192193
PromoteLifetimeTokens();
193194
else if (Session.Configuration.Supports(SessionOptions.NonTransactionalReads))
194-
ClearLifetimeTokens();
195+
PromoteLifetimeTokensToSession();
195196
else
196197
ExpireLifetimeTokens();
197198
State = TransactionState.Committed;
@@ -244,6 +245,15 @@ private void PromoteLifetimeTokens()
244245
ClearLifetimeTokens();
245246
}
246247

248+
private void PromoteLifetimeTokensToSession()
249+
{
250+
if (Outer != null
251+
|| !Session.TryPromoteTokens(EnumerableUtils.One(LifetimeToken).Union(lifetimeTokens))) {
252+
throw new InvalidOperationException("Promoting to session is not supported.");
253+
}
254+
ClearLifetimeTokens();
255+
}
256+
247257
private void ClearLifetimeTokens()
248258
{
249259
lifetimeTokens.Clear();

0 commit comments

Comments
 (0)