Permalink
Browse files

Update #283 - Make getting the timer strategy safer

  • Loading branch information...
1 parent a862537 commit 5e0124b32a58321037a789c3d33e7786fc334a23 @avanderhoorn avanderhoorn committed Mar 24, 2013
@@ -52,7 +52,7 @@ private IMessageBroker MessageBroker
private IExecutionTimer TimerStrategy
{
- get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetConfiguredTimerStrategy()()); }
+ get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetExecutionTimer()); }
set { timerStrategy = value; }
}
@@ -175,19 +175,19 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
DbDataReader reader;
var commandId = Guid.NewGuid();
- var timer = TimerStrategy.Start();
+ var timer = TimerTrigger();
LogCommandStart(commandId, timer);
try
{
reader = InnerCommand.ExecuteReader(behavior);
}
catch (Exception exception)
{
- LogCommandError(commandId, TimerStrategy.Stop(timer), exception);
+ LogCommandError(commandId, timer, exception);
throw;
}
- LogCommandEnd(commandId, TimerStrategy.Stop(timer), reader.RecordsAffected);
+ LogCommandEnd(commandId, timer, reader.RecordsAffected);
return new GlimpseDbDataReader(reader, InnerCommand, InnerConnection.ConnectionId, commandId);
}
@@ -197,18 +197,18 @@ public override int ExecuteNonQuery()
int num;
var commandId = Guid.NewGuid();
- var timer = TimerStrategy.Start();
+ var timer = TimerTrigger();
LogCommandStart(commandId, timer);
try
{
num = InnerCommand.ExecuteNonQuery();
}
catch (Exception exception)
{
- LogCommandError(commandId, TimerStrategy.Stop(timer), exception);
+ LogCommandError(commandId, timer, exception);
throw;
}
- LogCommandEnd(commandId, TimerStrategy.Stop(timer), num);
+ LogCommandEnd(commandId, timer, num);
return num;
}
@@ -218,18 +218,18 @@ public override object ExecuteScalar()
object result;
var commandId = Guid.NewGuid();
- var timer = TimerStrategy.Start();
+ var timer = TimerTrigger();
LogCommandStart(commandId, timer);
try
{
result = InnerCommand.ExecuteScalar();
}
catch (Exception exception)
{
- LogCommandError(commandId, TimerStrategy.Stop(timer), exception);
+ LogCommandError(commandId, timer, exception);
throw;
}
- LogCommandEnd(commandId, TimerStrategy.Stop(timer), null);
+ LogCommandEnd(commandId, timer, null);
return result;
}
@@ -312,25 +312,35 @@ private void LogCommandStart(Guid commandId, TimeSpan timerTimeSpan)
}
}
- private void LogCommandEnd(Guid commandId, TimerResult timerResult, int? recordsAffected)
+ private void LogCommandEnd(Guid commandId, TimeSpan timer, int? recordsAffected)
{
if (MessageBroker != null)
{
MessageBroker.Publish(
new CommandDurationAndRowCountMessage(InnerConnection.ConnectionId, commandId, recordsAffected)
- .AsTimedMessage(timerResult));
+ .AsTimedMessage(TimerStop(timer)));
}
}
- private void LogCommandError(Guid commandId, TimerResult timerResult, Exception exception)
+ private void LogCommandError(Guid commandId, TimeSpan timer, Exception exception)
{
if (MessageBroker != null)
{
MessageBroker.Publish(
new CommandErrorMessage(InnerConnection.ConnectionId, commandId, exception)
- .AsTimedMessage(timerResult));
+ .AsTimedMessage(TimerStop(timer)));
}
}
+
+ private TimeSpan TimerTrigger()
+ {
+ return TimerStrategy != null ? TimerStrategy.Start() : TimeSpan.Zero;
+ }
+
+ private TimerResult TimerStop(TimeSpan timer)
+ {
+ return TimerStrategy != null ? TimerStrategy.Stop(timer) : null;
+ }
#endregion
}
}
@@ -64,7 +64,7 @@ private IMessageBroker MessageBroker
private IExecutionTimer TimerStrategy
{
- get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetConfiguredTimerStrategy()()); }
+ get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetExecutionTimer()); }
set { timerStrategy = value; }
}
@@ -218,7 +218,7 @@ private void StateChangeHaneler(object sender, StateChangeEventArgs args)
private void OpenConnection()
{
- timerTimeSpan = TimerStrategy.Start();
+ timerTimeSpan = TimerTrigger();
MessageBroker.Publish(
new ConnectionStartedMessage(ConnectionId)
@@ -229,7 +229,17 @@ private void ClosedConnection()
{
MessageBroker.Publish(
new ConnectionClosedMessage(ConnectionId)
- .AsTimedMessage(TimerStrategy.Stop(timerTimeSpan)));
+ .AsTimedMessage(TimerStop(timerTimeSpan)));
+ }
+
+ private TimeSpan TimerTrigger()
+ {
+ return TimerStrategy != null ? TimerStrategy.Start() : TimeSpan.Zero;
+ }
+
+ private TimerResult TimerStop(TimeSpan timer)
+ {
+ return TimerStrategy != null ? TimerStrategy.Stop(timer) : null;
}
}
}
@@ -12,8 +12,7 @@ namespace Glimpse.Ado.AlternateType
{
internal class GlimpseDbDataReader : DbDataReader
{
- private IMessageBroker messageBroker;
- private IExecutionTimer timerStrategy;
+ private IMessageBroker messageBroker;
public GlimpseDbDataReader(DbDataReader dataReader, DbCommand command, Guid connectionId, Guid commandId)
{
@@ -23,11 +22,10 @@ public GlimpseDbDataReader(DbDataReader dataReader, DbCommand command, Guid conn
CommandId = commandId;
}
- public GlimpseDbDataReader(DbDataReader dataReader, DbCommand command, Guid connectionId, Guid commandId, IMessageBroker messageBroker, IExecutionTimer timerStrategy)
+ public GlimpseDbDataReader(DbDataReader dataReader, DbCommand command, Guid connectionId, Guid commandId, IMessageBroker messageBroker)
: this(dataReader, command, connectionId, commandId)
{
- MessageBroker = messageBroker;
- TimerStrategy = timerStrategy;
+ MessageBroker = messageBroker;
}
private DbDataReader InnerDataReader { get; set; }
@@ -46,13 +44,7 @@ private IMessageBroker MessageBroker
{
get { return messageBroker ?? (messageBroker = GlimpseConfiguration.GetConfiguredMessageBroker()); }
set { messageBroker = value; }
- }
-
- private IExecutionTimer TimerStrategy
- {
- get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetConfiguredTimerStrategy()()); }
- set { timerStrategy = value; }
- }
+ }
public override int Depth
{
@@ -22,7 +22,7 @@ public GlimpseDbTransaction(DbTransaction transaction, GlimpseDbConnection conne
if (MessageBroker != null)
{
- timerTimeSpan = TimerStrategy.Start();
+ timerTimeSpan = TimerTrigger();
MessageBroker.Publish(
new TransactionBeganMessage(connection.ConnectionId, TransactionId, transaction.IsolationLevel)
@@ -36,8 +36,12 @@ public GlimpseDbTransaction(DbTransaction transaction, GlimpseDbConnection conne
MessageBroker = messageBroker;
TimerStrategy = timerStrategy;
}
-
+
public GlimpseDbConnection InnerConnection { get; set; }
+
+ public DbTransaction InnerTransaction { get; set; }
+
+ public Guid TransactionId { get; set; }
private IMessageBroker MessageBroker
{
@@ -47,7 +51,7 @@ private IMessageBroker MessageBroker
private IExecutionTimer TimerStrategy
{
- get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetConfiguredTimerStrategy()()); }
+ get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetExecutionTimer()); }
set { timerStrategy = value; }
}
@@ -68,7 +72,7 @@ public override void Commit()
{
MessageBroker.Publish(
new TransactionCommitMessage(InnerConnection.ConnectionId, TransactionId)
- .AsTimedMessage(TimerStrategy.Stop(timerTimeSpan)));
+ .AsTimedMessage(TimerStop(timerTimeSpan)));
}
}
@@ -89,13 +93,18 @@ public override void Rollback()
{
MessageBroker.Publish(
new TransactionRollbackMessage(InnerConnection.ConnectionId, TransactionId)
- .AsTimedMessage(TimerStrategy.Stop(timerTimeSpan)));
+ .AsTimedMessage(TimerStop(timerTimeSpan)));
}
}
+ private TimeSpan TimerTrigger()
+ {
+ return TimerStrategy != null ? TimerStrategy.Start() : TimeSpan.Zero;
+ }
- public DbTransaction InnerTransaction { get; set; }
-
- public Guid TransactionId { get; set; }
+ private TimerResult TimerStop(TimeSpan timer)
+ {
+ return TimerStrategy != null ? TimerStrategy.Stop(timer) : null;
+ }
}
}
@@ -615,6 +615,13 @@ public static Func<IExecutionTimer> GetConfiguredTimerStrategy()
}
[Obsolete("HACK: To support TraceListener with TraceSource via web.config")]
+ public static IExecutionTimer GetExecutionTimer()
+ {
+ var result = GetConfiguredTimerStrategy();
+ return result != null ? result() : null;
+ }
+
+ [Obsolete("HACK: To support TraceListener with TraceSource via web.config")]
public static IMessageBroker GetConfiguredMessageBroker()
{
return messageBroker;

0 comments on commit 5e0124b

Please sign in to comment.