介绍:文件上传功能,使用wcf提供 服务 , 数据 库使用EntityFramework4 访问 。 问题现象:在wcf有一个文件块上传方法 /// summary /// 上传文件 /// /summary /// param name="block" 文件块信息 /param /// returns 上传文件的信息状态 /returns public o
介绍:文件上传功能,使用wcf提供服务,数据库使用EntityFramework4访问。
问题现象:在wcf有一个文件块上传方法
<p><span>///</span><span><summary></summary></span><span><br> </span><span>///</span><span> 上传文件<br> </span><span>///</span><span></span><span><br> </span><span>///</span><span><param name="block"></span><span>文件块信息</span><span></span><span><br> </span><span>///</span><span><returns></returns></span><span>上传文件的信息状态</span><span></span><span><br></span><span>public</span><span>override</span><span> FileTransportAck UpLoadFile(FileBlock block)<br> {<br> </span><span>lock</span><span> (lockobj)<br> {<br> </span><span>//</span><span>查找文件是否存在</span><span><br></span><span> tab_FileInfo theFileInfo </span><span>=</span><span> m_NetBidEnt.tab_FileInfo<br> .Where(o </span><span>=></span><span> o.FileID.Equals(block.FileID))<br> .FirstOrDefault();<br><br> </span><span>if</span><span> (theFileInfo </span><span>==</span><span>null</span><span>)<br> </span><span>throw</span><span>new</span><span> Exception(</span><span>"</span><span>文件未初始化,上传失败</span><span>"</span><span>);<br><br> </span><span>//</span><span>返回传输结果</span><span><br></span><span> FileTransportAck result </span><span>=</span><span>new</span><span> FileTransportAck();<br> </span><span>//</span><span>写入文件块</span><span><br></span><span>if</span><span> (FileSysIO.WriteBlock(theFileInfo, block))<br> {<br> theFileInfo.UploadedSize </span><span>+=</span><span> block.Size;<br> theFileInfo.FileSize </span><span>=</span><span> block.Total;<br> theFileInfo.UpdateDate </span><span>=</span><span> DateTime.Now;<br> m_NetBidEnt.SaveChanges();<br> }<br> </span><span>else</span><span><br> </span><span>throw</span><span>new</span><span> Exception(</span><span>"</span><span>写入文件块失败</span><span>"</span><span>);<br> </span><span>//</span><span>写入返回值</span><span><br></span><span> result.FileId </span><span>=</span><span> theFileInfo.FileID;<br> result.Offset </span><span>=</span><span> (</span><span>long</span><span>)theFileInfo.UploadedSize;<br> </span><span>if</span><span> (theFileInfo.UploadedSize </span><span>>=</span><span> block.Total)<br> result.TransportStat </span><span>=</span><span> FileTransportStat.Finished;<br> </span><span>else</span><span><br> result.TransportStat </span><span>=</span><span> FileTransportStat.ResumeBreakPoint;<br> </span><span>return</span><span> result;<br> }<br> }</span></p>
此方法会在上传文件时循环调用,基本是每10K传一次,即会调用此访问一次,但很奇怪的问题是如果文件在10M以上时,传输过程中会出现停止。。此时查看数据库服务整体就像是锁死了一样,使用ssms也无法连接,报出的异常都是连接超时。
后来在wcf的方法内增加了日志用于记录异常,记录的异常如下:
<p><span><strong>Exception Message</strong><br>2011</span><span>-</span><span>07</span><span>-</span><span>28</span><span>00</span><span>:</span><span>38</span><span>:</span><span>26</span><span>,</span><span>451</span><span> An error occurred </span><span>while</span><span> starting a transaction on the provider connection. See the inner exception </span><span>for</span><span> details.<br></span><strong>StackTrace</strong><br><span>2011</span><span>-</span><span>07</span><span>-</span><span>28</span><span>00</span><span>:</span><span>38</span><span>:</span><span>26</span><span>,</span><span>467</span><span> at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)<br> at System.Data.EntityClient.EntityConnection.BeginTransaction()<br> at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)<br> at System.Data.Objects.ObjectContext.SaveChanges()<br> at NetBid.FileServices.Impl.FileSysFileService.UpLoadFile(FileBlock block)<br> at NetBid.FileServices.Wcf.FileServiceWCF.UploadFileBlock(FileBlock block, Dictionary`</span><span>2</span><span> parameters)<br><strong>Inner Exception:</strong><br></span><span>2011</span><span>-</span><span>07</span><span>-</span><span>28</span><span>00</span><span>:</span><span>38</span><span>:</span><span>26</span><span>,</span><span>467</span><span> Inner Exception:System.Data.SqlClient.SqlException (</span><span>0x80131904</span><span>): Timeout expired. The timeout period elapsed prior to completion of the operation or the server </span><span>is</span><span> not responding.<br> at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)<br> at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()<br> at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)<br> at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)<br> at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()<br> at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()<br> at System.Data.SqlClient.TdsParserStateObject.ReadByte()<br> at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)<br> at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)<br> at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)<br> at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)<br> at System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName)<br> at System.Data.SqlClient.SqlInternalConnection.BeginTransaction(IsolationLevel iso)<br> at System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel isolationLevel)<br> at System.Data.Common.DbConnection.BeginTransaction(IsolationLevel isolationLevel)<br> at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)<br></span><span>2011</span><span>-</span><span>07</span><span>-</span><span>28</span><span>00</span><span>:</span><span>38</span><span>:</span><span>26</span><span>,</span><span>467</span><span>=======================================================</span><span>分割线</span></p>
异常看上去像是事务问题,但有一个奇怪的现象是:上传过程中一旦出现停止现象后,整个数据库都无法访问,只能将数据库的服务停止并重新启动。