Skip to content

Commit

Permalink
improving error log and add CommandTimeout prop
Browse files Browse the repository at this point in the history
  • Loading branch information
jesuslpm committed May 7, 2014
1 parent b8c75aa commit 9877548
Show file tree
Hide file tree
Showing 75 changed files with 384 additions and 1,870 deletions.
Binary file modified EntityLite.v12.suo
Binary file not shown.
1 change: 1 addition & 0 deletions Samples/DataLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ public void RaiseProductPrices(Decimal? rate)
{
var executor = new StoredProcedureExecutor(this.DataService, true)
{
CommandTimeout = 10,
GetCommandFunc = () =>
{
var proc = Samples.Entities.StoredProcedures.CreateRaiseProductPricesProcedure(this.DataService.Connection, this.DataService.EntityLiteProvider.ParameterPrefix);
Expand Down
3 changes: 2 additions & 1 deletion Samples/DataLayer.tt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ To generate the datalayer for MySQL you need to:
{
ProcedureName = "RaiseProductPrices",
ResultSetKind = ProcedureResultSetKind.None,
RelatedEntityName = "Product"
RelatedEntityName = "Product",
CommandTimeout = 10
}
//new ProcedureSetting
//{
Expand Down
5 changes: 5 additions & 0 deletions Samples/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ private class NativeMethods
public static extern int UuidCreateSequential(out Guid guid);
}

partial void OnCreated()
{
this.CommandTimeout = 40;
}

protected override Guid NewGuid()
{
Guid guid;
Expand Down
5 changes: 4 additions & 1 deletion Samples/Entities/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public int RaiseProductPrices(int? categoryId, decimal rate)
DefaultSchema = this.DataService.EntityLiteProvider.DefaultSchema,
Rate = rate
};
var cmd = new TemplatedCommand(this.DataService, template);
var cmd = new TemplatedCommand(this.DataService, template)
{
CommandTimeout = 10
};
return cmd.ExecuteNonQuery();
}
}
Expand Down
3 changes: 3 additions & 0 deletions Samples/EntityLite.ttinclude/EntityGeneration.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ void RenderProcedureMethodInRepository(DataLayerGeneration generation, EntitySet
} #>
var executor = new StoredProcedureExecutor(this.DataService, true)
{
<#+ if (procedure.CommandTimeout >= 0) { #>
CommandTimeout = <#=procedure.CommandTimeout #>,
<#+ } #>
GetCommandFunc = () =>
{
var proc = <#= generation.RootNamespace #>.<#= generation.ProceduresClassName #>.<#= procedure.GetCreateProcedureMethodName(generation.DefaultSchema) #>(this.DataService.Connection, this.DataService.EntityLiteProvider.ParameterPrefix);
Expand Down
2 changes: 1 addition & 1 deletion Samples/EntityLite.ttinclude/EntityLite.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<#@ assembly name="Microsoft.CSharp" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="$(SolutionDir)packages\EntityLite.Core.1.4.1\lib\net35-client\inercya.EntityLite.dll" #>
<#@ assembly name="$(SolutionDir)packages\EntityLite.Core.1.5.0\lib\net35-client\inercya.EntityLite.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Data" #>
Expand Down
3 changes: 3 additions & 0 deletions Samples/EntityLite.ttinclude/ProcedureSetting.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class ProcedureSetting

public MemberVisibility MemberVisibility { get; set; }

public int CommandTimeout { get; set; }

public string GetFullProcedureName()
{
if (string.IsNullOrEmpty(ProcedureName)) return string.Empty;
Expand Down Expand Up @@ -55,6 +57,7 @@ public class ProcedureSetting

public ProcedureSetting()
{
this.CommandTimeout = -1;
}

public string GetReturnTypeName(string entityName)
Expand Down
Binary file modified Samples/Northwind.mdf
Binary file not shown.
Binary file modified Samples/Northwind_log.ldf
Binary file not shown.
332 changes: 168 additions & 164 deletions Samples/Program.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Samples/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityLite" version="1.4.1" targetFramework="net40" />
<package id="EntityLite.Core" version="1.4.1" targetFramework="net40" />
<package id="EntityLite" version="1.5.0" targetFramework="net40" />
<package id="EntityLite.Core" version="1.5.0" targetFramework="net40" />
<package id="Microsoft.SqlServer.Types" version="10.50.1600.1" targetFramework="net40" />
<package id="MySql.Data" version="6.7.4" targetFramework="net40" />
<package id="NLog" version="2.1.0" targetFramework="net40" />
Expand Down
15 changes: 11 additions & 4 deletions inercya.EntityLite/AbstractQueryLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public abstract class AbstractQueryLite : IQueryLite
private IQueryBuilder _queryBuilder;
public IQueryBuilder QueryBuilder { get { return _queryBuilder; } set { _queryBuilder = value; } }

public int CommandTimeout { get; set; }


protected abstract IEnumerable NonGenericToEnumerable();

Expand Down Expand Up @@ -116,7 +118,8 @@ public int GetCount()
{
var cmd = new CommandExecutor(this.DataService, true)
{
GetCommandFunc = GetCountCommand
GetCommandFunc = GetCountCommand,
CommandTimeout = this.CommandTimeout
};
return Convert.ToInt32(cmd.ExecuteScalar());
}
Expand All @@ -127,6 +130,7 @@ protected AbstractQueryLite()
this.Sort = new List<SortDescriptor>();
this.Options = new List<string>();
this.FieldList = new List<string>();
this.CommandTimeout = -1;
}

public DataTable Pivot(params Extensions.PivotColumn[] pivotColumns)
Expand All @@ -138,7 +142,8 @@ public DataTable Pivot(Comparison<Extensions.PivotedColumn> pivotedColumnCompari
{
var cmd = new CommandExecutor(this.DataService, true)
{
GetCommandFunc = GetSelectCommand
GetCommandFunc = GetSelectCommand,
CommandTimeout = this.CommandTimeout
};

var properties = this.EntityType.GetEntityMetadata().Properties;
Expand Down Expand Up @@ -175,7 +180,8 @@ public virtual IEnumerable<TEntity> ToEnumerable()
{
var cmd = new CommandExecutor(this.DataService, true)
{
GetCommandFunc = GetSelectCommand
GetCommandFunc = GetSelectCommand,
CommandTimeout = this.CommandTimeout
};
return cmd.ToEnumerable<TEntity>();
}
Expand All @@ -184,7 +190,8 @@ public virtual IEnumerable<TEntity> ToEnumerable(int fromIndex, int toIndex)
{
var cmd = new CommandExecutor(this.DataService, true)
{
GetCommandFunc = () => GetSelectCommand(fromIndex, toIndex)
GetCommandFunc = () => GetSelectCommand(fromIndex, toIndex),
CommandTimeout = this.CommandTimeout
};
return cmd.ToEnumerable<TEntity>();
}
Expand Down
4 changes: 2 additions & 2 deletions inercya.EntityLite/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ limitations under the License.
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.4.1.0")]
[assembly: AssemblyFileVersion("1.4.1.0")]
[assembly: AssemblyVersion("1.5.0.0")]
[assembly: AssemblyFileVersion("1.5.0.0")]
105 changes: 91 additions & 14 deletions inercya.EntityLite/CommandLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ public abstract class AbstractCommand
public readonly DataService DataService;
public readonly bool DisposeCommand;

public int CommandTimeout { get; set; }

protected AbstractCommand(DataService dataService, bool disposeCommand)
{
if (dataService == null) throw new ArgumentNullException("dataService");
this.DataService = dataService;
this.DisposeCommand = disposeCommand;
this.CommandTimeout = -1;
}

protected abstract DbCommand GetCommand();
Expand Down Expand Up @@ -80,24 +83,89 @@ public IDataReader ExecuteReader()
DbCommand command = null;
try
{
int maxRetries = DataService.IsActiveTransaction ? 0 : DataService.MaxRetries;
command = GetConfiguredCommand();
Func<IDataReader> func = () => command.ExecuteReader();
var watch = Stopwatch.StartNew();
var reader = func.ExecuteWithRetries(
maxRetries, DataService.InitialMillisecondsRetryDelay,
(ex, willRetry) => DataService.NotifyErrorOcurred(ex, willRetry));

Stopwatch watch;
IDataReader reader = null;
try
{
int maxRetries = DataService.IsActiveTransaction ? 0 : DataService.MaxRetries;
command = GetConfiguredCommand();
Func<IDataReader> func = () => command.ExecuteReader();
watch = Stopwatch.StartNew();
reader = func.ExecuteWithRetries(
maxRetries, DataService.InitialMillisecondsRetryDelay,
(ex, willRetry) => DataService.NotifyErrorOcurred(ex, willRetry));
}
catch (Exception ex)
{
if (command == null)
{
Log.ErrorException("Couldn't get data reader from command", ex);
}
else
{
Log.ErrorException(string.Format("Couldn't get data reader from command\r\n{0}\r\n{1}", command.CommandText, command.GetParamsAsString()), ex);
}
throw;
}
using (reader)
{
var factory = reader.GetFactory(typeof(T));
while (reader.Read())
Func<IDataReader, object> factory = null;
try
{
factory = reader.GetFactory(typeof(T));
}
catch (Exception ex)
{
if (command == null)
{
Log.ErrorException(string.Format("Couldn't materialize data reader beacuse GetFactory({0}) failed", typeof(T).Name), ex);
}
else
{
Log.ErrorException(string.Format("Couldn't materialize data reader beacuse GetFactory({0}) failed\r\n{1}\r\n{2}", typeof(T).Name, command.CommandText, command.GetParamsAsString()), ex);
}
throw;
}
while (true)
{
T entity;
try
{
if (reader.Read()) entity = (T)factory(reader);
else break;
}
catch (Exception ex)
{
if (command == null)
{
Log.ErrorException(string.Format("Couldn't materialize entity from data reader", typeof(T).Name), ex);
}
else
{
Log.ErrorException(string.Format("Couldn't materialize entity from data reader\r\n{0}\r\n{1}", command.CommandText, command.GetParamsAsString()), ex);
}
throw;
}
yield return entity;
}
}
try
{
SetOutPutParameters(command);
ProfilerLite.LogCommandExecution(command, DataService, watch.ElapsedMilliseconds);
}
catch (Exception ex)
{
if (command == null)
{
Log.ErrorException(string.Format("Error ocurred executing ToEnumerable", typeof(T).Name), ex);
}
else
{
yield return (T)factory(reader);
Log.ErrorException(string.Format("Couldn't materialize entity from data reader\r\n{0}\r\n{1}", typeof(T), command.CommandText, command.GetParamsAsString()), ex);
}
throw;
}
SetOutPutParameters(command);
ProfilerLite.LogCommandExecution(command, DataService, watch.ElapsedMilliseconds);
}
finally
{
Expand All @@ -116,6 +184,8 @@ private DbCommand GetConfiguredCommand()
this.DataService.OpenConnection();
var command = GetCommand();
command.Connection = DataService.Connection;
if (this.CommandTimeout >= 0) command.CommandTimeout = this.CommandTimeout;
else if (this.DataService.CommandTimeout >= 0) command.CommandTimeout = this.DataService.CommandTimeout;
if (DataService.IsActiveTransaction)
{
command.Transaction = DataService.Transaction;
Expand Down Expand Up @@ -143,7 +213,14 @@ private T ExecuteCommand<T>(Func<DbCommand, T> executeCommandFunc)
}
catch (Exception ex)
{
Log.ErrorException("Couldn't execute command", ex);
if (command == null)
{
Log.ErrorException("Couldn't execute command", ex);
}
else
{
Log.ErrorException(string.Format("Couldn't execute command\r\n{0}\r\n{1}", command.CommandText, command.GetParamsAsString()), ex);
}
throw;
}
finally
Expand Down
4 changes: 4 additions & 0 deletions inercya.EntityLite/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public partial class DataService : IDisposable
public SpecialFieldNames SpecialFieldNames { get; set; }

private IEntityLiteProvider _entityLiteProvider;

public int CommandTimeout { get; set; }

public IEntityLiteProvider EntityLiteProvider
{
get
Expand Down Expand Up @@ -326,6 +329,7 @@ private void Initialize()
this.InitialMillisecondsRetryDelay = 20;
this.IsAutomaticAuditDateFieldsEnabled = true;
this.IsAutomaticAuditUserFieldsEnabled = true;
this.CommandTimeout = -1;
}

public DataService(string connectionStringName)
Expand Down
46 changes: 46 additions & 0 deletions inercya.EntityLite/Extensions/CommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using System.Globalization;

namespace inercya.EntityLite.Extensions
{
Expand All @@ -44,5 +45,50 @@ public static IDbDataParameter AddWithValue(this DbParameterCollection parameter

}

public static string GetParamsAsString(this DbCommand command)
{
if (command == null) throw new ArgumentNullException("command");
StringBuilder builder = new StringBuilder();
bool firstTime = true;

foreach (DbParameter p in command.Parameters)
{
if (p.Value != null)
{
if (firstTime)
{
firstTime = false;
}
else
{
builder.Append(", ");
}
string paramValueAsString = null;
if (Convert.IsDBNull(p.Value))
{
paramValueAsString = "NULL";
}
else
{
IConvertible convertible = p.Value as IConvertible;
if (convertible != null)
{
paramValueAsString = convertible.ToString(CultureInfo.InvariantCulture);
}
else if (p.Value is byte[])
{
paramValueAsString = "0x" + BitConverter.ToString((byte[])p.Value, 0).Replace("-", string.Empty);
}
else
{
paramValueAsString = p.Value.ToString();
}
}
builder.Append(p.ParameterName).Append(" = ").Append(paramValueAsString);
}
}
return builder.ToString();
}

}
}
6 changes: 6 additions & 0 deletions inercya.EntityLite/Extensions/QueryLiteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,5 +584,11 @@ public static IQueryLite<TEntity> Fields<TEntity>(this IQueryLite<TEntity> query
{
return query.Fields(FieldsOption.IncludeBoth, fields);
}

public static IQueryLite<TEntity> WithTimeout<TEntity>(this IQueryLite<TEntity> query, int timeout)
{
query.CommandTimeout = timeout;
return query;
}
}
}
2 changes: 2 additions & 0 deletions inercya.EntityLite/IQueryLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public interface IQueryLite
DataService DataService { get; set; }
DataTable Pivot(params Extensions.PivotColumn[] pivotColumns);
DataTable Pivot(Comparison<Extensions.PivotedColumn> pivotedColumnComparison, params Extensions.PivotColumn[] pivotColumns);
int CommandTimeout { get; set; }

}

public interface IQueryLite<TEntity> : IQueryLite
Expand Down
Loading

0 comments on commit 9877548

Please sign in to comment.