forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionUpdateAction.cs
159 lines (143 loc) · 4.3 KB
/
CollectionUpdateAction.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Diagnostics;
using NHibernate.Cache;
using NHibernate.Cache.Entry;
using NHibernate.Collection;
using NHibernate.Engine;
using NHibernate.Event;
using NHibernate.Impl;
using NHibernate.Persister.Collection;
namespace NHibernate.Action
{
[Serializable]
public sealed class CollectionUpdateAction : CollectionAction
{
private readonly bool emptySnapshot;
public CollectionUpdateAction(IPersistentCollection collection, ICollectionPersister persister, object key,
bool emptySnapshot, ISessionImplementor session)
: base(persister, collection, key, session)
{
this.emptySnapshot = emptySnapshot;
}
public override void Execute()
{
object id = Key;
ISessionImplementor session = Session;
ICollectionPersister persister = Persister;
IPersistentCollection collection = Collection;
bool affectedByFilters = persister.IsAffectedByEnabledFilters(session);
bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
Stopwatch stopwatch = null;
if (statsEnabled)
{
stopwatch = Stopwatch.StartNew();
}
PreUpdate();
if (!collection.WasInitialized)
{
if (!collection.HasQueuedOperations)
{
throw new AssertionFailure("no queued adds");
}
//do nothing - we only need to notify the cache...
}
else if (!affectedByFilters && collection.Empty)
{
if (!emptySnapshot)
{
persister.Remove(id, session);
}
}
else if (collection.NeedsRecreate(persister))
{
if (affectedByFilters)
{
throw new HibernateException("cannot recreate collection while filter is enabled: "
+ MessageHelper.InfoString(persister, id, persister.Factory));
}
if (!emptySnapshot)
{
persister.Remove(id, session);
}
persister.Recreate(collection, id, session);
}
else
{
persister.DeleteRows(collection, id, session);
persister.UpdateRows(collection, id, session);
persister.InsertRows(collection, id, session);
}
Session.PersistenceContext.GetCollectionEntry(collection).AfterAction(collection);
Evict();
PostUpdate();
if (statsEnabled)
{
stopwatch.Stop();
Session.Factory.StatisticsImplementor.UpdateCollection(Persister.Role, stopwatch.Elapsed);
}
}
private void PreUpdate()
{
IPreCollectionUpdateEventListener[] preListeners = Session.Listeners.PreCollectionUpdateEventListeners;
if (preListeners.Length > 0)
{
PreCollectionUpdateEvent preEvent = new PreCollectionUpdateEvent(Persister, Collection, (IEventSource)Session);
for (int i = 0; i < preListeners.Length; i++)
{
preListeners[i].OnPreUpdateCollection(preEvent);
}
}
}
private void PostUpdate()
{
IPostCollectionUpdateEventListener[] postListeners = Session.Listeners.PostCollectionUpdateEventListeners;
if (postListeners.Length > 0)
{
PostCollectionUpdateEvent postEvent = new PostCollectionUpdateEvent(Persister, Collection, (IEventSource)Session);
for (int i = 0; i < postListeners.Length; i++)
{
postListeners[i].OnPostUpdateCollection(postEvent);
}
}
}
public override BeforeTransactionCompletionProcessDelegate BeforeTransactionCompletionProcess
{
get
{
return null;
}
}
public override AfterTransactionCompletionProcessDelegate AfterTransactionCompletionProcess
{
get
{
return new AfterTransactionCompletionProcessDelegate((success) =>
{
// NH Different behavior: to support unlocking collections from the cache.(r3260)
if (Persister.HasCache)
{
CacheKey ck = Session.GenerateCacheKey(Key, Persister.KeyType, Persister.Role);
if (success)
{
// we can't disassemble a collection if it was uninitialized
// or detached from the session
if (Collection.WasInitialized && Session.PersistenceContext.ContainsCollection(Collection))
{
CollectionCacheEntry entry = new CollectionCacheEntry(Collection, Persister);
bool put = Persister.Cache.AfterUpdate(ck, entry, null, Lock);
if (put && Session.Factory.Statistics.IsStatisticsEnabled)
{
Session.Factory.StatisticsImplementor.SecondLevelCachePut(Persister.Cache.RegionName);
}
}
}
else
{
Persister.Cache.Release(ck, Lock);
}
}
});
}
}
}
}