Skip to content

Commit b6f16b6

Browse files
Document IsDirty side effects.
* Fixes #1449.
1 parent 22bc773 commit b6f16b6

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

doc/reference/modules/manipulating_data.xml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ sess.Lock(pk, LockMode.Upgrade);]]></programlisting>
820820
</orderedlist>
821821

822822
<para>
823-
(An exception is that objects using <literal>native</literal> ID generation are
823+
(An exception is that objects using <literal>identity</literal> ID generation are
824824
inserted when they are saved.)
825825
</para>
826826

@@ -861,6 +861,37 @@ using (ITransaction tx = sess.BeginTransaction())
861861

862862
</sect1>
863863

864+
<sect1 id="manipulatingdata-dirtiness">
865+
<title>Checking dirtiness</title>
866+
867+
<para>
868+
<literal>ISession.IsDirty()</literal> will return whether the session hold any pending
869+
change to flush or not. Be cautious when using this method, its default implementation
870+
may have the following effects:
871+
</para>
872+
873+
<itemizedlist spacing="compact">
874+
<listitem>
875+
<para>
876+
Dirty checks all the loaded entities. NHibernate does not instrument the entities
877+
for being notified of changes done on loaded ones. Instead, it stores their
878+
initial state and compare them to it. If session has loaded a lot of entities,
879+
the dirty checking will have a significant impact.
880+
</para>
881+
</listitem>
882+
<listitem>
883+
<para>
884+
Triggers pending cascade operations. This includes any pending <literal>Save</literal>
885+
of, by example, children added to a collection having the <literal>Save</literal>
886+
cascade enabled. Depending on the entities ID generators (see
887+
<xref linkend="mapping-declaration-id-generator"/>), this may trigger calls to the
888+
database, or even entity insertions if they are using the <literal>identity</literal>
889+
generator.
890+
</para>
891+
</listitem>
892+
</itemizedlist>
893+
</sect1>
894+
864895
<sect1 id="manipulatingdata-endingsession">
865896
<title>Ending a Session</title>
866897

src/NHibernate/Async/ISession.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
using System.Linq;
1515
using System.Linq.Expressions;
1616
using NHibernate.Engine;
17+
using NHibernate.Event;
18+
using NHibernate.Event.Default;
1719
using NHibernate.Stat;
1820
using NHibernate.Type;
1921

@@ -38,9 +40,25 @@ public partial interface ISession : IDisposable
3840
/// <summary>
3941
/// Does this <c>ISession</c> contain any changes which must be
4042
/// synchronized with the database? Would any SQL be executed if
41-
/// we flushed this session?
43+
/// we flushed this session? May trigger save cascades, which could
44+
/// cause themselves some SQL to be executed, especially if the
45+
/// <c>identity</c> id generator is used.
4246
/// </summary>
4347
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
48+
/// <remarks>
49+
/// <para>
50+
/// The default implementation first checks if it contains saved or deleted entities to be flushed. If not, it
51+
/// then delegate the check to its <see cref="IDirtyCheckEventListener" />, which by default is
52+
/// <see cref="DefaultDirtyCheckEventListener" />.
53+
/// </para>
54+
/// <para>
55+
/// <see cref="DefaultDirtyCheckEventListener" /> replicates all the beginning of the flush process, checking
56+
/// dirtiness of entities loaded in the session and triggering their pending cascade operations in order to
57+
/// detect new and removed children. This can have the side effect of performing the <see cref="Save(object)"/>
58+
/// of children, causing their id to be generated. Depending on their id generator, this can trigger calls to
59+
/// the database and even actually insert them if using an <c>identity</c> generator.
60+
/// </para>
61+
/// </remarks>
4462
Task<bool> IsDirtyAsync(CancellationToken cancellationToken = default(CancellationToken));
4563

4664
/// <summary>

src/NHibernate/ISession.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Linq;
55
using System.Linq.Expressions;
66
using NHibernate.Engine;
7+
using NHibernate.Event;
8+
using NHibernate.Event.Default;
79
using NHibernate.Stat;
810
using NHibernate.Type;
911

@@ -179,8 +181,24 @@ public partial interface ISession : IDisposable
179181
/// <summary>
180182
/// Does this <c>ISession</c> contain any changes which must be
181183
/// synchronized with the database? Would any SQL be executed if
182-
/// we flushed this session?
184+
/// we flushed this session? May trigger save cascades, which could
185+
/// cause themselves some SQL to be executed, especially if the
186+
/// <c>identity</c> id generator is used.
183187
/// </summary>
188+
/// <remarks>
189+
/// <para>
190+
/// The default implementation first checks if it contains saved or deleted entities to be flushed. If not, it
191+
/// then delegate the check to its <see cref="IDirtyCheckEventListener" />, which by default is
192+
/// <see cref="DefaultDirtyCheckEventListener" />.
193+
/// </para>
194+
/// <para>
195+
/// <see cref="DefaultDirtyCheckEventListener" /> replicates all the beginning of the flush process, checking
196+
/// dirtiness of entities loaded in the session and triggering their pending cascade operations in order to
197+
/// detect new and removed children. This can have the side effect of performing the <see cref="Save(object)"/>
198+
/// of children, causing their id to be generated. Depending on their id generator, this can trigger calls to
199+
/// the database and even actually insert them if using an <c>identity</c> generator.
200+
/// </para>
201+
/// </remarks>
184202
bool IsDirty();
185203

186204
/// <summary>

0 commit comments

Comments
 (0)