我知道在c#中默认池大小为100,但我想将池大小限制为20,并且必须将固定时间未处理的请求取消后,其余对db的请求必须排队 我尝试了以下连接字符串,并使用邮递员一次发送了100个请求,但它处理了所有请求,并且没有遇到任何异常。
connection.ConnectionString = "Server=servername;Database=dbname;Trusted_Connection=true;Connection Timeout=0;Connection Lifetime=30;Min Pool Size=0;Max Pool Size=20;Pooling=true;"
答案 0 :(得分:0)
使用20个锁的资源锁定形式不起作用吗?例如
static readonly object SomeLock = new object();
using (var lck = new CodeLock())
{
var ok = lck.Lock(SomeLock, timeout)
// do stuff, connect to db
}
public struct CodeLock : IDisposable
{
private object lockedObject;
private bool lockTaken;
public bool Lock(object someLock, int timeout)
{
lockedObject = someLock;
Monitor.TryEnter(someLock, timeout, ref lockTaken);
if (lockTaken == false)
{
// didn't acquire lock within timeout
return false;
}
return true;
}
public void Dispose()
{
if (lockedObject == null) return;
if (lockTaken)
{
lockTaken = false;
Monitor.Exit(lockedObject);
}
}
}