-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathCollectionUpdateAction.cs
144 lines (128 loc) · 3.95 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
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 partial 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 = GetKey();
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.CollectionInfoString(persister, collection, id, session));
}
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);
}
var entry = Session.PersistenceContext.GetCollectionEntry(collection);
entry.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 void ExecuteAfterTransactionCompletion(bool success)
{
// NH Different behavior: to support unlocking collections from the cache.(r3260)
CacheKey ck = Session.GenerateCacheKey(GetKey(), 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 = CollectionCacheEntry.Create(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);
}
}
}
}