I am using Codesmith for database connectivity, it is throwing the below exception now and then. Also this class have Singleton
What I assume is codesmith handles the opening & closing the connection with database automatically.
Error in *. Ex: System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed. at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource 1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) at System.Data.Linq.SqlClient.SqlProvider.CompiledQuery.Execute(IProvider provider, Object[] arguments) at System.Data.Linq.CommonDataServices.DeferredSourceFactory 1.ExecuteKeyQuery(Object[] keyValues) at System.Data.Linq.CommonDataServices.DeferredSourceFactory 1.Execute(Object instance) at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable 1 source) at System.Data.Linq.EntityRef`1.get_Entity() at Datacontext.Data.DataSource.get_Delimiter() in C:*\DataSource.Generated.cs:line 407 at Datacontext.Data.Managers.DBManager.GetDelimiterOfAnalyser() in C:*\DBManager.cs:line 537
Code:
/// <summary>
/// Returns DelimiterOfAnalyser
/// </summary>
/// <returns></returns>
public Dictionary<string, string> GetDelimiterOfAnalyser()
{
Dictionary<string, string> delimiters = new Dictionary<string, string>();
try
{
foreach (var delimiter in dataContext.DataSource.Where(a => a.TypeId == 1).ToList())
{
if (!delimiters.ContainsKey(delimiter.Name))
{
delimiters.Add(delimiter.Name, delimiter.Delimiter.Value);
}
}
}
catch (Exception ex)
{
logger.Error("Error in GetDelimiterOfAnalyser. Ex: " + ex);
}
return delimiters;
}
public static DBManager GetInstance()
{
try
{
if (dbMgr == null)
{
dbMgr = new DBManager();
}
dataContext = new DataContext();
}
catch (Exception ex)
{
logger.Error(ex);
}
return dbMgr;
}