From f209c2b08cdcb02abe15fb1e86a82d7f03cdcf45 Mon Sep 17 00:00:00 2001 From: Alexander Zaytsev Date: Fri, 6 Dec 2013 13:59:18 +1300 Subject: [PATCH] NH-3431 - Fix AbstractBatcher.CloseReader method which tried to use reader after it was disposed. --- src/NHibernate/AdoNet/AbstractBatcher.cs | 25 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/NHibernate/AdoNet/AbstractBatcher.cs b/src/NHibernate/AdoNet/AbstractBatcher.cs index 851f6cf3e12..e295c4f5bfe 100644 --- a/src/NHibernate/AdoNet/AbstractBatcher.cs +++ b/src/NHibernate/AdoNet/AbstractBatcher.cs @@ -359,6 +359,8 @@ public void CloseReader(DbDataReader reader) var actualReader = rsw == null ? reader : rsw.Target; _readersToClose.Remove(actualReader); + var duration = GetReaderStopwatch(actualReader); + try { reader.Dispose(); @@ -370,17 +372,24 @@ public void CloseReader(DbDataReader reader) } LogCloseReader(); + LogDuration(duration); + } - if (!Log.IsDebugEnabled) - return; - - var nhReader = actualReader as NHybridDataReader; - actualReader = nhReader == null ? actualReader : nhReader.Target; + private Stopwatch GetReaderStopwatch(DbDataReader reader) + { + var nhReader = reader as NHybridDataReader; + var actualReader = nhReader == null ? reader : nhReader.Target; Stopwatch duration; - if (_readersDuration.TryGetValue(actualReader, out duration) == false) - return; - _readersDuration.Remove(actualReader); + if (_readersDuration.TryGetValue(actualReader, out duration)) + _readersDuration.Remove(actualReader); + return duration; + } + + private static void LogDuration(Stopwatch duration) + { + if (!Log.IsDebugEnabled || duration == null) return; + Log.DebugFormat("DataReader was closed after {0} ms", duration.ElapsedMilliseconds); }