First, before I go in to how to do it, let me say that SqlConnection
is specifically designed to be created and destroyed quickly and repeatedly. When you dispose of the connection it doss not immediately release the resources, it instead waits to shut down and if a new connection is created using the same connection string before a timeout expires it will re-use the old connection. This is called "Connection Pooling" and it is on by default (you must explicitly set special settings in your connection string to disable it.)
I recommend you just create a new connection inside the function (I also corrected a few errors about how to set the degree of parallelism, you need to pass in a ParallelOptions
parameter)
JobObject[] jobs = ...; // init array of jobs
Parallel.For(0,
jobs.Length,
new ParallelOptions { MaxDegreeOfParallelism = PARALLELISM},
(i) =>
{
using(SqlConnection connection = new SqlConnection(_connectionString)
{
jobs[i].Do(connection);
}
});
This also could be re-written as a Parallel.ForEach
if you don't need the index i
other than to access the array.
JobObject[] jobs = ...; // init array of jobs
Parallel.ForEach(jobs,
new ParallelOptions { MaxDegreeOfParallelism = PARALLELISM},
(job) =>
{
using(SqlConnection connection = new SqlConnection(_connectionString)
{
job.Do(connection);
}
});
However, to answer your question if you wanted a re-useable resource that was per-thread you need to use this overload that gives you two more functions you pass in that creates and destroys the thread local resources.
JobObject[] jobs = ...; // init array of jobs
Parallel.For(0,
jobs.Length,
new ParallelOptions { MaxDegreeOfParallelism = PARALLELISM},
() => new SqlConnection(_connectionString), //Thread local init
(i, loopstate, connection) =>
{
jobs[i].Do(connection);
return connection; //Passes the object to the next function that will re-use it.
},
(connection) => connection.Dispose()); //thread local finally.
Once again, I do not recommend you do it this way and instead do it the first way using the short lived SqlConnection
, the class is designed to be used that way.