我在舞臺系統上看到了這一點,它已經運行了2-3天。與Linq的定期InvalidCastException和「服務器無法恢復交易」
「服務器無法恢復交易,說明:39000000ef。」 (desc:xxx每次增加)。
堆棧跟蹤顯示
System.Data.SqlClient.SqlException: The server failed to resume the transaction. Desc:39000000ef.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
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 ...
在這之後發生的整個服務器去見鬼。似乎元數據在內存中不再正確表示,因爲之後會看到許多InvalidCastException。我猜這是由於系統試圖將字符串反序列化爲int字段,因爲元數據偏移不正確。
即
System.InvalidCastException: Specified cast is not valid.
at System.Data.SqlClient.SqlBuffer.get_Int32()
at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
at Read_Order(ObjectMaterializer`1)
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
這是什麼意思?
我非常確定數據庫沒有在系統後面更新,數據庫一直在線。
從這裏開始,問題依然存在,直到服務器重新啓動,之後一切正常。
我是否需要擁有能夠在數據庫連接失敗時重新建立數據庫連接的代碼?框架本身不處理?
我使用SQL Server 2008,IIS 6,和.Net 3.5SP1
UPDATE: 的代碼結構做這樣的事情:
var dc = new datacontext()
IList<someobject> objs = dc.GetAllItemsToProcess()
var dc2 = new datacontext();
foreach(var o in objs) {
try {
var o2 = dc2.someobjects.SingleOrDefault(x=>x.id = o.id);
// do stuff to o2
dc2.save();
} catch() {
// something failed so restart datacontext()
dc2 = new datacontext();
}
}
哪個服務器重新啓動得到? SQL Server或Web服務器? – Rap 2009-09-16 14:04:39
網絡服務器;或者實際上只是應用程序。 – Soraz 2009-09-17 20:22:59
我們遇到同樣的問題 - 應用程序不時崩潰,直到重新啓動後纔會崩潰。同樣的例外「服務器無法恢復事務」並拋出InvalidCastExceptions。有趣的是,同一個應用程序的另一個實例運行平穩。除了使用兩個(可能干擾的)datacontext之外,它可能與IoC容器中的datacontext實例化方法有關。到目前爲止我找不到問題解決方案,所以如果您有任何更新,我們將不勝感激! – PanJanek 2010-05-11 14:19:03