我需要从c#代码(.net 3.5,EF 1)执行存储过程以获取值数组。所以我认为可以使用ConnectionPool
和ThreadPool
。这是我的代码:
DBModel db;
private void generateInvoices(Object retailers)
{
List<my_db_view> ret = (List<my_db_view>)retailers;
foreach (my_db_view retailer in ret)
{
try
{
tblMyItem accDoc = db.pi_ami_add_my_item(retailer.RetailerID, retailer.ContractID, retailer.fru_code, null).FirstOrDefault();
}
catch (Exception e)
{
//handle exception
}
}
}
private void export(DBModel db)
{
this.db = db;
ThreadPool.SetMinThreads(10, 10);
ThreadPool.SetMaxThreads(50, 50);
//both methods returned true
List<my_db_view> retailers = db.my_db_view.ToList();
while (retailers.Count > 0)
{
int count = Math.Min(5, retailers.Count);
List<my_db_view> toProcess = new List<my_db_view>();
toProcess.AddRange(retailers.Take(count));
retailers.RemoveRange(0, count);
ThreadPool.QueueUserWorkItem(new WaitCallback(generateInvoices), toProcess);
}
int availableThreads;
int maxthreads, q, w;
ThreadPool.GetAvailableThreads(out availableThreads, out q);
ThreadPool.GetMaxThreads(out maxthreads, out w);
while (availableThreads != maxthreads)
{
Console.WriteLine("there are {0} threds active", (maxthreads - availableThreads));
Thread.Sleep(5000);
ThreadPool.GetAvailableThreads(out availableThreads, out q);
ThreadPool.GetMaxThreads(out maxthreads, out w);
}
//some code
}
我的连接字符串:
Data Source=192.168.0.1\SQL;Initial Catalog=MY_DATABASE;User id=user;Password=pass;Pooling=true;Min Pool Size=60;Max Pool Size=100;MultipleActiveResultSets=True
我的问题:
拨打db.pi_ami_add_my_item
后,执行此行时,连接将被关闭?
为什么我收到了错误
连接未关闭。连接的当前状态正在连接。
当我运行代码时,最小连接数设置为60并且池中的最大线程数设置为50?
从多个线程使用相同的数据库模型对象时,EF如何管理连接?
我的问题是执行时间。我需要在不到30分钟的时间内执行200,000次存储过程。鉴于存储过程的复杂性,很难使其适应使用基于集合的方法。我注意到如果我使用线程它运行得更快。