1
我有收集共享信息和結果寫入數據庫, 的方法我用Parallel.Foreach這就增加了性能,特別是如果掃描100 TBParallel.ForEach遞歸方法
如果我在本地運行這段代碼數據庫,我覺得沒有問題,但沙盒數據庫上我得到噸例外/ innerExceptions
代碼:
private static INodeCollection NodesLookUp(string path, int maximumLevel)
{
var shareCollectionNode = new ShareCollection(path);
shareCollectionNode.GetNodeProperties();
shareCollectionNode.GetPermissionEntires();
WriteNodeToDatabase(shareCollectionNode); // write collected infomation to database
if (maximumLevel <= 0 && _maximumSubLevels != -1)
{
return shareCollectionNode;
}
Parallel.ForEach(Directory.GetDirectories(shareCollectionNode.FullPath), directory =>
{
try
{
lock (shareCollectionNode)
{
shareCollectionNode.AddNode(NodesLookUp(directory, maximumLevel - 1));
}
}
catch (UnauthorizedAccessException unauthorizedAccessException)
{
lock (_shareIssues)
{
_shareIssues.Add(new ShareIssue(TraceStatu.UnauthorizedAccess, directory,
unauthorizedAccessException.Message, dfsId, currentLevel));
}
}
});
return shareCollectionNode;
}
寫入到數據庫:
個private static void WriteNodeToDatabase(ShareCollection shareCollection)
{
var nodeId = Persistence.UpsertShare(shareCollection);
var sharePermissions = new List<IPermissionRight>();
foreach (var permissionEntry in shareCollection.PermissionEntries)
{
permissionEntry.NodeId = nodeId;
var permissionEntryId = Persistence.InsertPermissionEntry(permissionEntry);
permissionEntry.SetPermissions(permissionEntryId);
sharePermissions.AddRange(permissionEntry.Permissions);
}
Persistence.InsertPermissions(sharePermissions);
}
例外:
System.Data.SqlClient.SqlException (0x80131904): Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
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()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
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.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
如果我刪除Parallel.Foreach和使用正常的循環,然後我有一個應用程序需要年齡運行身邊沒有問題。
請按照刪除或至少減少橫向滾動的方式來格式化代碼。 – 2013-04-04 15:56:02
'Parallel.ForEach'以'lock'結尾? – ken2k 2013-04-04 15:57:27
這就是我不喜歡的口袋妖怪異常處理:我們不知道這個異常拋出的位置。任何想法哪個查詢導致此錯誤? – IdeaHat 2013-04-04 16:08:24