Skip to content

Fix possible issue with async code for delayed entity inserts #2233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/NHibernate/Action/DelayedPostInsertIdentifier.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;

namespace NHibernate.Action
{
Expand All @@ -15,20 +16,12 @@ namespace NHibernate.Action
[Serializable]
public class DelayedPostInsertIdentifier
{
[ThreadStatic]
private static long _Sequence = 0;
private static long GlobalSequence = 0;
private readonly long sequence;

public DelayedPostInsertIdentifier()
{
lock (typeof(DelayedPostInsertIdentifier))
{
if (_Sequence == long.MaxValue)
{
_Sequence = 0;
}
sequence = _Sequence++;
}
sequence = Interlocked.Increment(ref GlobalSequence);
}

public override bool Equals(object obj)
Expand Down
16 changes: 3 additions & 13 deletions src/NHibernate/Action/EntityIdentityInsertAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace NHibernate.Action
[Serializable]
public sealed partial class EntityIdentityInsertAction : AbstractEntityInsertAction
{
private readonly object lockObject = new object();
private readonly bool isDelayed;
private readonly EntityKey delayedEntityKey;
//private CacheEntry cacheEntry;
Expand All @@ -19,7 +18,9 @@ public EntityIdentityInsertAction(object[] state, object instance, IEntityPersis
: base(null, state, instance, persister, session)
{
this.isDelayed = isDelayed;
delayedEntityKey = this.isDelayed ? GenerateDelayedEntityKey() : null;
delayedEntityKey = this.isDelayed
? Session.GenerateEntityKey(new DelayedPostInsertIdentifier(), Persister)
: null;
}

public object GeneratedId
Expand All @@ -32,17 +33,6 @@ public EntityKey DelayedEntityKey
get { return delayedEntityKey; }
}

private EntityKey GenerateDelayedEntityKey()
{
lock (lockObject)
{
if (!isDelayed)
throw new HibernateException("Cannot request delayed entity-key for non-delayed post-insert-id generation");

return Session.GenerateEntityKey(new DelayedPostInsertIdentifier(), Persister);
}
}

protected internal override bool HasPostCommitEventListeners
{
get
Expand Down