我想知道是否有人帮助我处理下一个错误。
我有一个app -windows服务,由一个计时器调用一些函数来操作数据库(linq2sql)。
大约在3-5次调用后,我总是收到下一个错误“线程被中止”。在下一个源代码中
var ctx= new myLinq2SqlDataContext();
var res = ctx.SomeData.Where();
我不使用多线程!我在调用函数时使用lock语句来处理数据库。我只有一项服务与上下文一起使用。
这是我的代码段
public partial class DataService : ServiceBase
{
private static object timerProcessLockObject = new object();
static object _receiverTimerLockObject = new object();
void OnStart()
{
if (_receiverTimer == null)
{
lock (_receiverTimerLockObject)
{
if (_receiverTimer == null)
{
_receiverTimer = new Timer(new TimerCallback(ReceiverTimer_TimerCallback), null, 0, 60000);
}
}
}
}
private void ReceiverTimer_TimerCallback(object state)
{
lock (timerProcessLockObject)
{
var receiveService = new ReceiverService();
receiveService.GetData();
}
}
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class ReceiverService : IReceiverService
{
public void GetData()
{
IDataControler myController = new DataControler();
myController.GetData();
}
}
public class DataControler : IDataControler
{
public void GetData()
{
IMyDomainService myDomainService = new MyDomainService();
myDomainService.SomeFunc();
IMyExtraDomainService myExtraService = new MyExtraDomainService();
var data = myExtraService.SomeFuns();
var convertedData = ConvertData(data);
myDomainService.SaveData(convertedData);
DBConnection newConnection = new DBConnection();
myDomainService.AddNewConnection(newConnection);
}
}
public class MyExtraDomainService : IMyExtraDomainService
{
public IList<Entity> SomeFuns()
{
try
{
var myCtx = new MyLinq2SqlDataContext(); //!!!! Here I receive error
return myCtx.SomeEntities.Where(...).ToList();
}
catch (Exception exc)
{
throw;
}
}
}
已更新
这是堆栈跟踪
at SNIReadSync(SNI_Conn* , SNI_Packet** , Int32 )
at SNINativeMethodWrapper.SNIReadSync(SafeHandle pConn, IntPtr& packet, Int32 timeout)
at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
at System.Data.SqlClient.TdsParserStateObject.ReadByte()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
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.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader()
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.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at MY FUNCTION call
是什么原因? 提前致谢!