forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityIdentityInsertAction.cs
137 lines (123 loc) · 4.91 KB
/
EntityIdentityInsertAction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using NHibernate.Engine;
using NHibernate.Event;
using NHibernate.Persister.Entity;
namespace NHibernate.Action
{
using System.Threading.Tasks;
using System.Threading;
public sealed partial class EntityIdentityInsertAction : AbstractEntityInsertAction
{
public override async Task ExecuteAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
IEntityPersister persister = Persister;
object instance = Instance;
bool statsEnabled = Session.Factory.Statistics.IsStatisticsEnabled;
Stopwatch stopwatch = null;
if (statsEnabled)
{
stopwatch = Stopwatch.StartNew();
}
bool veto = await (PreInsertAsync(cancellationToken)).ConfigureAwait(false);
// Don't need to lock the cache here, since if someone
// else inserted the same pk first, the insert would fail
if (!veto)
{
generatedId = await (persister.InsertAsync(State, instance, Session, cancellationToken)).ConfigureAwait(false);
if (persister.HasInsertGeneratedProperties)
{
await (persister.ProcessInsertGeneratedPropertiesAsync(generatedId, instance, State, Session, cancellationToken)).ConfigureAwait(false);
}
//need to do that here rather than in the save event listener to let
//the post insert events to have a id-filled entity when IDENTITY is used (EJB3)
persister.SetIdentifier(instance, generatedId);
}
//TODO from H3.2 : this bit actually has to be called after all cascades!
// but since identity insert is called *synchronously*,
// instead of asynchronously as other actions, it isn't
/*if ( persister.hasCache() && !persister.isCacheInvalidationRequired() ) {
cacheEntry = new CacheEntry(object, persister, session);
persister.getCache().insert(generatedId, cacheEntry);
}*/
await (PostInsertAsync(cancellationToken)).ConfigureAwait(false);
if (statsEnabled && !veto)
{
stopwatch.Stop();
Session.Factory.StatisticsImplementor.InsertEntity(Persister.EntityName, stopwatch.Elapsed);
}
}
private async Task PostInsertAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (isDelayed)
{
Session.PersistenceContext.ReplaceDelayedEntityIdentityInsertKeys(delayedEntityKey, generatedId);
}
IPostInsertEventListener[] postListeners = Session.Listeners.PostInsertEventListeners;
if (postListeners.Length > 0)
{
PostInsertEvent postEvent = new PostInsertEvent(Instance, generatedId, State, Persister, (IEventSource)Session);
foreach (IPostInsertEventListener listener in postListeners)
{
await (listener.OnPostInsertAsync(postEvent, cancellationToken)).ConfigureAwait(false);
}
}
}
private async Task PostCommitInsertAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
IPostInsertEventListener[] postListeners = Session.Listeners.PostCommitInsertEventListeners;
if (postListeners.Length > 0)
{
var postEvent = new PostInsertEvent(Instance, generatedId, State, Persister, (IEventSource) Session);
foreach (IPostInsertEventListener listener in postListeners)
{
await (listener.OnPostInsertAsync(postEvent, cancellationToken)).ConfigureAwait(false);
}
}
}
private async Task<bool> PreInsertAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
IPreInsertEventListener[] preListeners = Session.Listeners.PreInsertEventListeners;
bool veto = false;
if (preListeners.Length > 0)
{
var preEvent = new PreInsertEvent(Instance, null, State, Persister, (IEventSource) Session);
foreach (IPreInsertEventListener listener in preListeners)
{
veto |= await (listener.OnPreInsertAsync(preEvent, cancellationToken)).ConfigureAwait(false);
}
}
return veto;
}
protected override Task AfterTransactionCompletionProcessImplAsync(bool success, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
//TODO Make 100% certain that this is called before any subsequent ScheduledUpdate.afterTransactionCompletion()!!
//TODO from H3.2: reenable if we also fix the above todo
/*EntityPersister persister = getEntityPersister();
if ( success && persister.hasCache() && !persister.isCacheInvalidationRequired() ) {
persister.getCache().afterInsert( getGeneratedId(), cacheEntry );
}*/
if (success)
{
return PostCommitInsertAsync(cancellationToken);
}
return Task.CompletedTask;
}
}
}