Skip to content

Additional check if child session has already been closed. #470

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

Closed
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
7 changes: 6 additions & 1 deletion src/NHibernate/Engine/ISessionImplementor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,12 @@ public interface ISessionImplementor
/// </returns>
bool IsClosed { get; }

void Flush();
/// <summary>
/// Close the session and release all resources.
/// </summary>
IDbConnection Close();

void Flush();

/// <summary>
/// Does this <tt>Session</tt> have an active Hibernate transaction
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/IStatelessSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public interface IStatelessSession : IDisposable
ISessionImplementor GetSessionImplementation();

/// <summary>Close the stateless session and release the ADO.NET connection.</summary>
void Close();
IDbConnection Close();

/// <summary>Insert an entity.</summary>
/// <param name="entity">A new transient instance</param>
Expand Down
4 changes: 3 additions & 1 deletion src/NHibernate/Impl/AbstractSessionImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ public bool IsClosed
get { return closed; }
}

protected internal virtual void CheckAndUpdateSessionStatus()
public abstract IDbConnection Close();

protected internal virtual void CheckAndUpdateSessionStatus()
{
ErrorIfClosed();
EnlistInAmbientTransactionIfNeeded();
Expand Down
17 changes: 10 additions & 7 deletions src/NHibernate/Impl/SessionImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public sealed class SessionImpl : AbstractSessionImpl, IEventSource, ISerializab
private readonly ISession rootSession;

[NonSerialized]
private IDictionary<EntityMode, ISession> childSessionsByEntityMode;
private IDictionary<EntityMode, ISessionImplementor> childSessionsByEntityMode;

[NonSerialized]
private readonly bool flushBeforeCompletionEnabled;
Expand Down Expand Up @@ -351,7 +351,7 @@ public bool ShouldAutoClose
/// Close() is not aware of distributed transactions
/// </remarks>
/// </summary>
public IDbConnection Close()
public override IDbConnection Close()
{
using (new SessionIdLoggingContext(SessionId))
{
Expand All @@ -372,9 +372,12 @@ public IDbConnection Close()
{
if (childSessionsByEntityMode != null)
{
foreach (KeyValuePair<EntityMode, ISession> pair in childSessionsByEntityMode)
foreach (KeyValuePair<EntityMode, ISessionImplementor> pair in childSessionsByEntityMode)
{
pair.Value.Close();
if (!pair.Value.IsClosed)
{
pair.Value.Close();
}
}
}
}
Expand Down Expand Up @@ -2243,10 +2246,10 @@ public ISession GetSession(EntityMode entityMode)

CheckAndUpdateSessionStatus();

ISession rtn = null;
ISessionImplementor rtn = null;
if (childSessionsByEntityMode == null)
{
childSessionsByEntityMode = new Dictionary<EntityMode, ISession>();
childSessionsByEntityMode = new Dictionary<EntityMode, ISessionImplementor>();
}
else
{
Expand All @@ -2260,7 +2263,7 @@ public ISession GetSession(EntityMode entityMode)
childSessionsByEntityMode.Add(entityMode, rtn);
}

return rtn;
return (ISession)rtn;
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/NHibernate/Impl/StatelessSessionImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,24 +439,25 @@ public ISessionImplementor GetSessionImplementation()
}

/// <summary> Close the stateless session and release the ADO.NET connection.</summary>
public void Close()
public override IDbConnection Close()
{
using (new SessionIdLoggingContext(SessionId))
{
ManagedClose();
return ManagedClose();
}
}

public void ManagedClose()
public IDbConnection ManagedClose()
{
using (new SessionIdLoggingContext(SessionId))
{
if (IsClosed)
{
throw new SessionException("Session was already closed!");
}
ConnectionManager.Close();
var dbConection = ConnectionManager.Close();
SetClosed();
return dbConection;
}
}

Expand Down