Skip to content

Commit 41342c3

Browse files
committed
Replaced TransactionCompletion delegates with interface implementations
1 parent f0644cf commit 41342c3

18 files changed

+393
-106
lines changed

src/AsyncGenerator.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@
9595
- conversion: Ignore
9696
name: Exists
9797
containingTypeName: AbstractCollectionPersister
98+
- conversion: ToAsync
99+
rule: TransactionCompletion
98100
- conversion: Ignore
99101
name: QuoteTableAndColumns
100102
containingTypeName: SchemaMetadataUpdater
@@ -135,6 +137,7 @@
135137
requiresCancellationToken:
136138
- rule: EventListener
137139
- rule: ICache
140+
- rule: TransactionCompletion
138141
scanMethodBody: true
139142
scanForMissingAsyncMembers:
140143
- all: true
@@ -259,6 +262,14 @@ methodRules:
259262
- containingType: NHibernate.Cache.ICache
260263
name: Unlock
261264
name: ICache
265+
- filters:
266+
- containingType: NHibernate.Action.IAfterTransactionCompletionProcess
267+
name: Execute
268+
- containingType: NHibernate.Action.IBeforeTransactionCompletionProcess
269+
name: Execute
270+
- containingType: NHibernate.Action.EntityAction
271+
name: BeforeTransactionCompletionProcessImpl
272+
name: TransactionCompletion
262273
- filters:
263274
- containingNamespace: NHibernate
264275
- containingType: NHibernate.Tool.hbm2ddl.SchemaUpdate
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace NHibernate.Action
6+
{
7+
public partial class AfterTransactionCompletionProcess : IAfterTransactionCompletionProcess
8+
{
9+
private readonly Action<bool> _syncAction;
10+
private readonly Func<bool, CancellationToken, Task> _asyncAction;
11+
12+
public AfterTransactionCompletionProcess(Action<bool> syncAction, Func<bool, CancellationToken, Task> asyncAction)
13+
{
14+
15+
_syncAction = syncAction;
16+
_asyncAction = asyncAction;
17+
}
18+
19+
public AfterTransactionCompletionProcess(Action<bool> syncAction)
20+
{
21+
_syncAction = syncAction;
22+
_asyncAction = (success, cancellationToken) =>
23+
{
24+
if (cancellationToken.IsCancellationRequested)
25+
{
26+
return Task.FromCanceled<object>(cancellationToken);
27+
}
28+
try
29+
{
30+
_syncAction(success);
31+
return Task.CompletedTask;
32+
}
33+
catch (Exception ex)
34+
{
35+
return Task.FromException<object>(ex);
36+
}
37+
};
38+
}
39+
40+
public void Execute(bool success)
41+
{
42+
_syncAction(success);
43+
}
44+
45+
public Task ExecuteAsync(bool success, CancellationToken cancellationToken)
46+
{
47+
return _asyncAction(success, cancellationToken);
48+
}
49+
}
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace NHibernate.Action
6+
{
7+
public class BeforeTransactionCompletionProcess : IBeforeTransactionCompletionProcess
8+
{
9+
private readonly System.Action _syncAction;
10+
private readonly Func<CancellationToken, Task> _asyncAction;
11+
12+
public BeforeTransactionCompletionProcess(System.Action syncAction, Func<CancellationToken, Task> asyncAction)
13+
{
14+
_syncAction = syncAction;
15+
_asyncAction = asyncAction;
16+
}
17+
18+
public BeforeTransactionCompletionProcess(System.Action syncAction)
19+
{
20+
_syncAction = syncAction;
21+
_asyncAction = (cancellationToken) =>
22+
{
23+
if (cancellationToken.IsCancellationRequested)
24+
{
25+
return Task.FromCanceled<object>(cancellationToken);
26+
}
27+
try
28+
{
29+
_syncAction();
30+
return Task.CompletedTask;
31+
}
32+
catch (Exception ex)
33+
{
34+
return Task.FromException<object>(ex);
35+
}
36+
};
37+
}
38+
39+
public void Execute()
40+
{
41+
_syncAction();
42+
}
43+
44+
public Task ExecuteAsync(CancellationToken cancellationToken)
45+
{
46+
return _asyncAction(cancellationToken);
47+
}
48+
}
49+
}

src/NHibernate/Action/BulkOperationCleanupAction.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,30 @@ public void Execute()
111111
// nothing to do
112112
}
113113

114-
public BeforeTransactionCompletionProcessDelegate BeforeTransactionCompletionProcess
114+
public IBeforeTransactionCompletionProcess BeforeTransactionCompletionProcess
115115
{
116-
get
117-
{
116+
get
117+
{
118118
return null;
119119
}
120120
}
121121

122-
public AfterTransactionCompletionProcessDelegate AfterTransactionCompletionProcess
122+
public IAfterTransactionCompletionProcess AfterTransactionCompletionProcess
123123
{
124124
get
125125
{
126-
return new AfterTransactionCompletionProcessDelegate((success) =>
127-
{
128-
this.EvictEntityRegions();
129-
this.EvictCollectionRegions();
130-
});
126+
return new AfterTransactionCompletionProcess(
127+
(success) =>
128+
{
129+
EvictEntityRegions();
130+
EvictCollectionRegions();
131+
},
132+
async (success, cancellationToken) =>
133+
{
134+
await EvictEntityRegionsAsync(cancellationToken);
135+
await EvictCollectionRegionsAsync(cancellationToken);
136+
}
137+
);
131138
}
132139
}
133140

src/NHibernate/Action/CollectionAction.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,34 @@ public virtual void BeforeExecutions()
104104
/// <summary>Execute this action</summary>
105105
public abstract void Execute();
106106

107-
public virtual BeforeTransactionCompletionProcessDelegate BeforeTransactionCompletionProcess
107+
public virtual IBeforeTransactionCompletionProcess BeforeTransactionCompletionProcess
108108
{
109109
get
110110
{
111111
return null;
112112
}
113113
}
114114

115-
public virtual AfterTransactionCompletionProcessDelegate AfterTransactionCompletionProcess
115+
public virtual IAfterTransactionCompletionProcess AfterTransactionCompletionProcess
116116
{
117117

118118
get
119119
{
120120
// Only make sense to add the delegate if there is a cache.
121121
if (persister.HasCache)
122122
{
123-
return new AfterTransactionCompletionProcessDelegate((success) =>
124-
{
125-
CacheKey ck = new CacheKey(key, persister.KeyType, persister.Role, Session.Factory);
126-
persister.Cache.Release(ck, softLock);
127-
});
123+
return new AfterTransactionCompletionProcess(
124+
(success) =>
125+
{
126+
var ck = new CacheKey(key, persister.KeyType, persister.Role, Session.Factory);
127+
persister.Cache.Release(ck, softLock);
128+
},
129+
async (success, cancellationToken) =>
130+
{
131+
var ck = new CacheKey(key, persister.KeyType, persister.Role, Session.Factory);
132+
await persister.Cache.ReleaseAsync(ck, softLock, cancellationToken).ConfigureAwait(false);
133+
}
134+
);
128135
}
129136
return null;
130137
}

src/NHibernate/Action/CollectionUpdateAction.cs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33
using NHibernate.Cache;
4+
using NHibernate.Cache.Access;
45
using NHibernate.Cache.Entry;
56
using NHibernate.Collection;
67
using NHibernate.Engine;
@@ -113,49 +114,66 @@ private void PostUpdate()
113114
}
114115
}
115116

116-
public override BeforeTransactionCompletionProcessDelegate BeforeTransactionCompletionProcess
117+
public override IBeforeTransactionCompletionProcess BeforeTransactionCompletionProcess
117118
{
118119
get
119120
{
120121
return null;
121122
}
122123
}
123124

124-
public override AfterTransactionCompletionProcessDelegate AfterTransactionCompletionProcess
125+
public override IAfterTransactionCompletionProcess AfterTransactionCompletionProcess
125126
{
126127
get
127128
{
128129
// Only make sense to add the delegate if there is a cache.
129130
if (Persister.HasCache)
130131
{
131-
// NH Different behavior: to support unlocking collections from the cache.(r3260)
132-
return new AfterTransactionCompletionProcessDelegate((success) =>
132+
return new CollectionCacheUpdate(this);
133+
}
134+
return null;
135+
}
136+
}
137+
138+
private partial class CollectionCacheUpdate : IAfterTransactionCompletionProcess
139+
{
140+
141+
private CollectionUpdateAction _action;
142+
143+
internal CollectionCacheUpdate(CollectionUpdateAction action)
144+
{
145+
_action = action;
146+
}
147+
148+
public void Execute(bool success)
149+
{
150+
var session = _action.Session;
151+
var persister = _action.Persister;
152+
var cacheLock = _action.Lock;
153+
CacheKey ck = session.GenerateCacheKey(_action.Key, persister.KeyType, persister.Role);
154+
155+
if (success)
156+
{
157+
var collection = _action.Collection;
158+
159+
// we can't disassemble a collection if it was uninitialized
160+
// or detached from the session
161+
if (collection.WasInitialized && session.PersistenceContext.ContainsCollection(collection))
133162
{
134-
CacheKey ck = Session.GenerateCacheKey(Key, Persister.KeyType, Persister.Role);
163+
CollectionCacheEntry entry = new CollectionCacheEntry(collection, persister);
164+
bool put = persister.Cache.AfterUpdate(ck, entry, null, cacheLock);
135165

136-
if (success)
166+
if (put && session.Factory.Statistics.IsStatisticsEnabled)
137167
{
138-
// we can't disassemble a collection if it was uninitialized
139-
// or detached from the session
140-
if (Collection.WasInitialized && Session.PersistenceContext.ContainsCollection(Collection))
141-
{
142-
CollectionCacheEntry entry = new CollectionCacheEntry(Collection, Persister);
143-
bool put = Persister.Cache.AfterUpdate(ck, entry, null, Lock);
144-
145-
if (put && Session.Factory.Statistics.IsStatisticsEnabled)
146-
{
147-
Session.Factory.StatisticsImplementor.SecondLevelCachePut(Persister.Cache.RegionName);
148-
}
149-
}
168+
session.Factory.StatisticsImplementor.SecondLevelCachePut(persister.Cache.RegionName);
150169
}
151-
else
152-
{
153-
Persister.Cache.Release(ck, Lock);
154-
}
155-
});
170+
}
171+
}
172+
else
173+
{
174+
persister.Cache.Release(ck, cacheLock);
156175
}
157-
return null;
158176
}
159177
}
160178
}
161-
}
179+
}

src/NHibernate/Action/EntityAction.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,22 @@ public void BeforeExecutions()
102102

103103
public abstract void Execute();
104104

105-
public virtual BeforeTransactionCompletionProcessDelegate BeforeTransactionCompletionProcess
105+
public virtual IBeforeTransactionCompletionProcess BeforeTransactionCompletionProcess
106106
{
107107
get
108108
{
109109
return NeedsBeforeTransactionCompletion()
110-
? new BeforeTransactionCompletionProcessDelegate(BeforeTransactionCompletionProcessImpl)
110+
? new BeforeTransactionCompletionProcess(BeforeTransactionCompletionProcessImpl, BeforeTransactionCompletionProcessImplAsync)
111111
: null;
112112
}
113113
}
114114

115-
public virtual AfterTransactionCompletionProcessDelegate AfterTransactionCompletionProcess
115+
public virtual IAfterTransactionCompletionProcess AfterTransactionCompletionProcess
116116
{
117117
get
118118
{
119119
return NeedsAfterTransactionCompletion()
120-
? new AfterTransactionCompletionProcessDelegate(AfterTransactionCompletionProcessImpl)
120+
? new AfterTransactionCompletionProcess(AfterTransactionCompletionProcessImpl, AfterTransactionCompletionProcessImplAsync)
121121
: null;
122122
}
123123
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace NHibernate.Action
2+
{
3+
public partial interface IAfterTransactionCompletionProcess
4+
{
5+
/// <summary>
6+
/// Perform whatever processing is encapsulated here after completion of the transaction.
7+
/// </summary>
8+
/// <param name="success">Did the transaction complete successfully? True means it did.</param>
9+
void Execute(bool success);
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Action
2+
{
3+
public partial interface IBeforeTransactionCompletionProcess
4+
{
5+
void Execute();
6+
}
7+
}

0 commit comments

Comments
 (0)