Skip to content

Commit

Permalink
Add more information on unclosed session
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericDelaporte committed Feb 25, 2024
1 parent 2ecb8d4 commit 9c93f48
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions src/NHibernate.Test/DebugSessionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ public partial class DebugSessionFactory : ISessionFactoryImplementor
/// it debug or not.
/// </summary>
public DebugConnectionProvider DebugConnectionProvider
=> _debugConnectionProvider ??
(_debugConnectionProvider = ActualFactory.ConnectionProvider as DebugConnectionProvider);
=> _debugConnectionProvider ??= ActualFactory.ConnectionProvider as DebugConnectionProvider;
public ISessionFactoryImplementor ActualFactory { get; }

public EventListeners EventListeners => ((SessionFactoryImpl)ActualFactory).EventListeners;

[NonSerialized]
private readonly ConcurrentBag<ISessionImplementor> _openedSessions = new ConcurrentBag<ISessionImplementor>();
private readonly ConcurrentQueue<ISessionImplementor> _openedSessions = new();
private static readonly ILog _log = LogManager.GetLogger(typeof(DebugSessionFactory).Assembly, typeof(TestCase));

public DebugSessionFactory(ISessionFactory actualFactory)
Expand All @@ -63,29 +62,43 @@ public DebugSessionFactory(ISessionFactory actualFactory)
public bool CheckSessionsWereClosed()
{
var allClosed = true;
var number = 1;
foreach (var session in _openedSessions)
{
// Do not inverse, we want to close all of them.
allClosed = CheckSessionWasClosed(session) && allClosed;
var wasClosed = CheckSessionWasClosed(session);
// No early exit out of the loop: we want to close all forgotten sessions.
if (!wasClosed)
{
_log.ErrorFormat("Test case didn't close session {0}, n°{1} of {2}, closing.",
session.SessionId, number, _openedSessions.Count);
}
allClosed = wasClosed && allClosed;

// Catches only session opened from another one while sharing the connection. Those
// opened without sharing the connection stay un-monitored.
foreach (var dependentSession in session.ConnectionManager.DependentSessions.ToList())
{
allClosed = CheckSessionWasClosed(dependentSession) && allClosed;
wasClosed = CheckSessionWasClosed(dependentSession);
if (!wasClosed)
{
_log.ErrorFormat("Test case didn't close dependent session {0} of the session {3}, n°{1} of {2}, closing.",
dependentSession.SessionId, number, _openedSessions.Count, session.SessionId);
}
allClosed = wasClosed && allClosed;
}
number++;
}

return allClosed;
}

private bool CheckSessionWasClosed(ISessionImplementor session)
private static bool CheckSessionWasClosed(ISessionImplementor session)
{
session.TransactionContext?.Wait();

if (!session.IsOpen)
return true;

_log.Error($"Test case didn't close session {session.SessionId}, closing");
(session as ISession)?.Close();
(session as IStatelessSession)?.Close();
return false;
Expand All @@ -101,7 +114,7 @@ ISession ISessionFactory.OpenSession(DbConnection connection)
#pragma warning disable CS0618 // Type or member is obsolete
var s = ActualFactory.OpenSession(connection);
#pragma warning restore CS0618 // Type or member is obsolete
_openedSessions.Add(s.GetSessionImplementation());
_openedSessions.Enqueue(s.GetSessionImplementation());
return s;
}

Expand All @@ -110,7 +123,7 @@ ISession ISessionFactory.OpenSession(IInterceptor sessionLocalInterceptor)
#pragma warning disable CS0618 // Type or member is obsolete
var s = ActualFactory.OpenSession(sessionLocalInterceptor);
#pragma warning restore CS0618 // Type or member is obsolete
_openedSessions.Add(s.GetSessionImplementation());
_openedSessions.Enqueue(s.GetSessionImplementation());
return s;
}

Expand All @@ -119,14 +132,14 @@ ISession ISessionFactory.OpenSession(DbConnection conn, IInterceptor sessionLoca
#pragma warning disable CS0618 // Type or member is obsolete
var s = ActualFactory.OpenSession(conn, sessionLocalInterceptor);
#pragma warning restore CS0618 // Type or member is obsolete
_openedSessions.Add(s.GetSessionImplementation());
_openedSessions.Enqueue(s.GetSessionImplementation());
return s;
}

ISession ISessionFactory.OpenSession()
{
var s = ActualFactory.OpenSession();
_openedSessions.Add(s.GetSessionImplementation());
_openedSessions.Enqueue(s.GetSessionImplementation());
return s;
}

Expand All @@ -138,14 +151,14 @@ IStatelessSessionBuilder ISessionFactory.WithStatelessOptions()
IStatelessSession ISessionFactory.OpenStatelessSession()
{
var s = ActualFactory.OpenStatelessSession();
_openedSessions.Add(s.GetSessionImplementation());
_openedSessions.Enqueue(s.GetSessionImplementation());
return s;
}

IStatelessSession ISessionFactory.OpenStatelessSession(DbConnection connection)
{
var s = ActualFactory.OpenStatelessSession(connection);
_openedSessions.Add(s.GetSessionImplementation());
_openedSessions.Enqueue(s.GetSessionImplementation());
return s;
}

Expand All @@ -158,7 +171,7 @@ ISession ISessionFactoryImplementor.OpenSession(
#pragma warning disable CS0618 // Type or member is obsolete
var s = ActualFactory.OpenSession(connection, flushBeforeCompletionEnabled, autoCloseSessionEnabled, connectionReleaseMode);
#pragma warning restore CS0618 // Type or member is obsolete
_openedSessions.Add(s.GetSessionImplementation());
_openedSessions.Enqueue(s.GetSessionImplementation());
return s;
}

Expand Down Expand Up @@ -429,7 +442,7 @@ public SessionBuilder(ISessionBuilder actualBuilder, DebugSessionFactory debugFa
ISession ISessionBuilder<ISessionBuilder>.OpenSession()
{
var s = _actualBuilder.OpenSession();
_debugFactory._openedSessions.Add(s.GetSessionImplementation());
_debugFactory._openedSessions.Enqueue(s.GetSessionImplementation());
return s;
}

Expand Down Expand Up @@ -504,7 +517,7 @@ public StatelessSessionBuilder(IStatelessSessionBuilder actualBuilder, DebugSess
IStatelessSession IStatelessSessionBuilder.OpenStatelessSession()
{
var s = _actualBuilder.OpenStatelessSession();
_debugFactory._openedSessions.Add(s.GetSessionImplementation());
_debugFactory._openedSessions.Enqueue(s.GetSessionImplementation());
return s;
}

Expand Down

0 comments on commit 9c93f48

Please sign in to comment.