【发布时间】:2017-07-14 09:08:19
【问题描述】:
我有问题)
我尝试在我的 c# 代码中重现几个 sp(存储过程)调用,但我想以异步方式进行。
TSQL 示例:
(Execute sp @key = 15072000173475; Execute sp @key = 15072000173571; ... Execute sp @key = n;)
[TestClass]
public class UnitTestNomenclature {
[TestMethod]
public void ParallelSQLMethod() {
Task scropeTasks = null;
//real amount is more then 1500
long[] keys = new long[] {15072000173475,15072000173571 ... n };
try {
var tasks = keys.Select( i => Task.Run(async () => { await RunStoredProc(i); }));
scropeTasks = Task.WhenAll(tasks);
scropeTasks.Wait();
} catch (Exception ex) {
Debug.WriteLine("Exception: " + ex.Message);
Debug.WriteLine("IsFaulted: " + scropeTasks.IsFaulted);
foreach (var inx in scropeTasks.Exception.InnerExceptions) {
Debug.WriteLine("Details: " + inx.Message);
}
}
Assert.AreEqual(1, 1);
}
public async Task RunStoredProc(long scollNumbParam) {
const string strStoredProcName = @"[dbo].[sp]";
using (SqlConnection conn = new SqlConnection(@"data source=SERVER;initial catalog=Db;integrated security=True;Trusted_Connection=Yes;")) {
await conn.OpenAsync();
Debug.WriteLine("============================================ Connection is open: ==============================================");
// info
Debug.WriteLine(String.Format("Connection: {0}", conn.ClientConnectionId));
Debug.WriteLine(String.Format("State: {0}", conn.State.ToString()));
using (SqlCommand cmd = new SqlCommand(strStoredProcName, conn) { CommandTimeout = 120, CommandType = CommandType.StoredProcedure }) {
SqlParameter scrParam = new SqlParameter() {
ParameterName = "@KEYKRT",
Value = scollNumbParam,
SqlDbType = SqlDbType.BigInt
};
cmd.Parameters.Add(scrParam);
Debug.WriteLine("Start of Proccesing: " + scollNumbParam);
await cmd.ExecuteNonQueryAsync().ConfigureAwait(false);
Debug.WriteLine("End of Proccesing: " + scollNumbParam);
}
}
Debug.WriteLine("============================================ Connection is closed: ==============================================");
}
}
这是我在输出窗口中得到的:
========== Connection is open: ========
Connection: 5be9c681-6eb5-422f-a22c-b49689a2d912
State: Open
Start of Proccesing: 15072000173475
========== Connection is open: ==========
Connection: cfb66041-6646-4b56-be1c-2afb26a18cb8
State: Open
Start of Proccesing: 15072000173571
.....
End of Proccesing: 15072000173475
=========== Connection is closed: =========
End of Proccesing: 15072000173571
=========== Connection is closed: =========
....
A timeout occurred while waiting for memory resources to execute the query in resource pool 'default' (2). Rerun the query.
Actual error number: 8645
Actual line number: 98
还调试说连接池溢出 我认为连接没有适当处理的主要原因,但是我如何通过异步来实现呢?
如果我尝试在声明异步任务之前只打开一个连接并将其传递给我的 RunStoredProc 方法,那么我得到 连接不支持 MultipleActiveResultSets
using (SqlConnection conn = new SqlConnection(@"data source=SERVER;initial catalog=Db;integrated security=True;Trusted_Connection=Yes;)) {
conn.OpenAsync();
var tasks = keys.Select(i => Task.Run(async () => { await RunStoredProc(i, conn); }));
scropeTasks = Task.WhenAll(tasks);
scropeTasks.Wait();
}
Debug.WriteLine("========== Connection is closed: ==========");
这是我在输出窗口中得到的:
Connection: 5be9c681-6eb5-422f-a22c-b49689a2d912
State: Open
Start of Proccesing: 15072000173475
======= Connection is open: =============
Connection: cfb66041-6646-4b56-be1c-2afb26a18cb8
State: Open
Start of Proccesing: 15072000173571
========= Connection is open: =========
【问题讨论】:
-
是什么阻止了您使测试异步?
-
没什么,我做了,得到了以上错误。我很想知道为什么会出现这个错误
-
因为你有 1500 个左右的任务同时执行,并且混合了异步和阻塞调用,这可能导致死锁。你预计会发生什么?
-
我希望得到异步处理。我正在寻找解决方案。我如何在每次迭代后实现 conenction dispose 事件?可能吗?我试图弄清楚 async/await + ado.net 如何协同工作
-
按顺序迭代它们。这将花费更长的时间,但至少连接将得到正确处理,以免资源过载。你也可以考虑分批进行。
标签: c# unit-testing asynchronous ado.net async-await