Skip to content

Commit 7468a00

Browse files
authored
Refactor debug logging in AbstractBatcher (#2382)
1 parent 4529342 commit 7468a00

File tree

2 files changed

+58
-86
lines changed

2 files changed

+58
-86
lines changed

src/NHibernate/AdoNet/AbstractBatcher.cs

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ private DbCommand Generate(CommandType type, SqlString sqlString, SqlType[] para
9393
}
9494

9595
var cmd = _factory.ConnectionProvider.Driver.GenerateCommand(type, sql, parameterTypes);
96-
LogOpenPreparedCommand();
97-
if (Log.IsDebugEnabled())
98-
{
99-
Log.Debug("Building an DbCommand object for the SqlString: {0}", sql);
100-
}
96+
LogOpenPreparedCommand(sql);
10197
_commandsToClose.Add(cmd);
10298
return cmd;
10399
}
@@ -232,7 +228,7 @@ public int ExecuteNonQuery(DbCommand cmd)
232228
}
233229
finally
234230
{
235-
if (Log.IsDebugEnabled() && duration != null)
231+
if (duration != null)
236232
Log.Debug("ExecuteNonQuery took {0} ms", duration.ElapsedMilliseconds);
237233
}
238234
}
@@ -242,37 +238,31 @@ public virtual DbDataReader ExecuteReader(DbCommand cmd)
242238
CheckReaders();
243239
LogCommand(cmd);
244240
Prepare(cmd);
245-
Stopwatch duration = null;
246-
if (Log.IsDebugEnabled())
247-
duration = Stopwatch.StartNew();
248-
DbDataReader reader = null;
241+
242+
var duration = Log.IsDebugEnabled() ? Stopwatch.StartNew() : null;
243+
244+
var reader = DoExecuteReader(cmd);
245+
246+
_readersToClose.Add(reader);
247+
LogOpenReader(duration , reader);
248+
return reader;
249+
}
250+
251+
private DbDataReader DoExecuteReader(DbCommand cmd)
252+
{
249253
try
250254
{
251-
reader = cmd.ExecuteReader();
255+
var reader = cmd.ExecuteReader();
256+
return _factory.ConnectionProvider.Driver.SupportsMultipleOpenReaders
257+
? reader
258+
: NHybridDataReader.Create(reader);
252259
}
253260
catch (Exception e)
254261
{
255262
e.Data["actual-sql-query"] = cmd.CommandText;
256263
Log.Error(e, "Could not execute query: {0}", cmd.CommandText);
257264
throw;
258265
}
259-
finally
260-
{
261-
if (Log.IsDebugEnabled() && duration != null && reader != null)
262-
{
263-
Log.Debug("ExecuteReader took {0} ms", duration.ElapsedMilliseconds);
264-
_readersDuration[reader] = duration;
265-
}
266-
}
267-
268-
if (!_factory.ConnectionProvider.Driver.SupportsMultipleOpenReaders)
269-
{
270-
reader = NHybridDataReader.Create(reader);
271-
}
272-
273-
_readersToClose.Add(reader);
274-
LogOpenReader();
275-
return reader;
276266
}
277267

278268
/// <summary>
@@ -388,8 +378,7 @@ public void CloseReader(DbDataReader reader)
388378
var rsw = reader as ResultSetWrapper;
389379
var actualReader = rsw == null ? reader : rsw.Target;
390380
_readersToClose.Remove(actualReader);
391-
392-
var duration = GetReaderStopwatch(actualReader);
381+
_readersDuration.Remove(actualReader, out var duration);
393382

394383
try
395384
{
@@ -401,24 +390,7 @@ public void CloseReader(DbDataReader reader)
401390
Log.Warn(e, "exception closing reader");
402391
}
403392

404-
LogCloseReader();
405-
LogDuration(duration);
406-
}
407-
408-
private Stopwatch GetReaderStopwatch(DbDataReader reader)
409-
{
410-
var nhReader = reader as NHybridDataReader;
411-
var actualReader = nhReader == null ? reader : nhReader.Target;
412-
413-
_readersDuration.Remove(actualReader, out var duration);
414-
return duration;
415-
}
416-
417-
private static void LogDuration(Stopwatch duration)
418-
{
419-
if (!Log.IsDebugEnabled() || duration == null) return;
420-
421-
Log.Debug("DataReader was closed after {0} ms", duration.ElapsedMilliseconds);
393+
LogCloseReader(duration);
422394
}
423395

424396
public void ExecuteBatch()
@@ -447,7 +419,7 @@ protected void ExecuteBatchWithTiming(DbCommand ps)
447419
duration = Stopwatch.StartNew();
448420
var countBeforeExecutingBatch = CountOfStatementsInCurrentBatch;
449421
DoExecuteBatch(ps);
450-
if (Log.IsDebugEnabled() && duration != null)
422+
if (duration != null)
451423
Log.Debug("ExecuteBatch for {0} statements took {1} ms",
452424
countBeforeExecutingBatch,
453425
duration.ElapsedMilliseconds);
@@ -504,12 +476,13 @@ protected void LogCommand(DbCommand command)
504476
_factory.Settings.SqlStatementLogger.LogCommand(command, FormatStyle.Basic);
505477
}
506478

507-
private void LogOpenPreparedCommand()
479+
private void LogOpenPreparedCommand(SqlString sql)
508480
{
509481
if (Log.IsDebugEnabled())
510482
{
511483
int currentOpenCommandCount = Interlocked.Increment(ref _openCommandCount);
512484
Log.Debug("Opened new DbCommand, open DbCommands: {0}", currentOpenCommandCount);
485+
Log.Debug("Building an DbCommand object for the SqlString: {0}", sql);
513486
}
514487

515488
if (_factory.Statistics.IsStatisticsEnabled)
@@ -532,22 +505,26 @@ private void LogClosePreparedCommand()
532505
}
533506
}
534507

535-
private static void LogOpenReader()
508+
private void LogOpenReader(Stopwatch duration, DbDataReader reader)
536509
{
537-
if (Log.IsDebugEnabled())
538-
{
539-
int currentOpenReaderCount = Interlocked.Increment(ref _openReaderCount);
540-
Log.Debug("Opened DbDataReader, open DbDataReaders: {0}", currentOpenReaderCount);
541-
}
510+
if (duration == null)
511+
return;
512+
513+
Log.Debug("ExecuteReader took {0} ms", duration.ElapsedMilliseconds);
514+
_readersDuration[reader] = duration;
515+
516+
int currentOpenReaderCount = Interlocked.Increment(ref _openReaderCount);
517+
Log.Debug("Opened DbDataReader, open DbDataReaders: {0}", currentOpenReaderCount);
542518
}
543519

544-
private static void LogCloseReader()
520+
private static void LogCloseReader(Stopwatch duration)
545521
{
546-
if (Log.IsDebugEnabled())
547-
{
548-
int currentOpenReaderCount = Interlocked.Decrement(ref _openReaderCount);
549-
Log.Debug("Closed DbDataReader, open DbDataReaders :{0}", currentOpenReaderCount);
550-
}
522+
if (duration == null)
523+
return;
524+
525+
int currentOpenReaderCount = Interlocked.Decrement(ref _openReaderCount);
526+
Log.Debug("Closed DbDataReader, open DbDataReaders :{0}", currentOpenReaderCount);
527+
Log.Debug("DataReader was closed after {0} ms", duration.ElapsedMilliseconds);
551528
}
552529

553530
public void CancelLastQuery()

src/NHibernate/Async/AdoNet/AbstractBatcher.cs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public async Task<int> ExecuteNonQueryAsync(DbCommand cmd, CancellationToken can
141141
}
142142
finally
143143
{
144-
if (Log.IsDebugEnabled() && duration != null)
144+
if (duration != null)
145145
Log.Debug("ExecuteNonQuery took {0} ms", duration.ElapsedMilliseconds);
146146
}
147147
}
@@ -152,13 +152,25 @@ public virtual async Task<DbDataReader> ExecuteReaderAsync(DbCommand cmd, Cancel
152152
await (CheckReadersAsync(cancellationToken)).ConfigureAwait(false);
153153
LogCommand(cmd);
154154
await (PrepareAsync(cmd, cancellationToken)).ConfigureAwait(false);
155-
Stopwatch duration = null;
156-
if (Log.IsDebugEnabled())
157-
duration = Stopwatch.StartNew();
158-
DbDataReader reader = null;
155+
156+
var duration = Log.IsDebugEnabled() ? Stopwatch.StartNew() : null;
157+
158+
var reader = await (DoExecuteReaderAsync(cmd, cancellationToken)).ConfigureAwait(false);
159+
160+
_readersToClose.Add(reader);
161+
LogOpenReader(duration , reader);
162+
return reader;
163+
}
164+
165+
private async Task<DbDataReader> DoExecuteReaderAsync(DbCommand cmd, CancellationToken cancellationToken)
166+
{
167+
cancellationToken.ThrowIfCancellationRequested();
159168
try
160169
{
161-
reader = await (cmd.ExecuteReaderAsync(cancellationToken)).ConfigureAwait(false);
170+
var reader = await (cmd.ExecuteReaderAsync(cancellationToken)).ConfigureAwait(false);
171+
return _factory.ConnectionProvider.Driver.SupportsMultipleOpenReaders
172+
? reader
173+
: await (NHybridDataReader.CreateAsync(reader, cancellationToken)).ConfigureAwait(false);
162174
}
163175
catch (OperationCanceledException) { throw; }
164176
catch (Exception e)
@@ -167,23 +179,6 @@ public virtual async Task<DbDataReader> ExecuteReaderAsync(DbCommand cmd, Cancel
167179
Log.Error(e, "Could not execute query: {0}", cmd.CommandText);
168180
throw;
169181
}
170-
finally
171-
{
172-
if (Log.IsDebugEnabled() && duration != null && reader != null)
173-
{
174-
Log.Debug("ExecuteReader took {0} ms", duration.ElapsedMilliseconds);
175-
_readersDuration[reader] = duration;
176-
}
177-
}
178-
179-
if (!_factory.ConnectionProvider.Driver.SupportsMultipleOpenReaders)
180-
{
181-
reader = await (NHybridDataReader.CreateAsync(reader, cancellationToken)).ConfigureAwait(false);
182-
}
183-
184-
_readersToClose.Add(reader);
185-
LogOpenReader();
186-
return reader;
187182
}
188183

189184
/// <summary>
@@ -234,7 +229,7 @@ protected async Task ExecuteBatchWithTimingAsync(DbCommand ps, CancellationToken
234229
duration = Stopwatch.StartNew();
235230
var countBeforeExecutingBatch = CountOfStatementsInCurrentBatch;
236231
await (DoExecuteBatchAsync(ps, cancellationToken)).ConfigureAwait(false);
237-
if (Log.IsDebugEnabled() && duration != null)
232+
if (duration != null)
238233
Log.Debug("ExecuteBatch for {0} statements took {1} ms",
239234
countBeforeExecutingBatch,
240235
duration.ElapsedMilliseconds);

0 commit comments

Comments
 (0)