I am using SMO to script out my objects from Sql server database using .Net code. But as of now I am going through a sequential loop.
foreach(var table in TableCollection)
{
var stringCollection=table.Script();
}
It is working fine. But when I convert the same loop to a Parallel.ForEach loop like:
Parallel.ForEach(TableCollection,table=>
{
var stringCollection=table.Script();
});
It fails to script. Is there anybody who has used the same kind of approach or any other approach to script out objects from Sql server in parallel?
UPDATE :
I haven’t been able to work out Parallel LOOP as of now but I have used below mentioned code :
server.SetDefaultInitFields(true);
It improves performance up-to some extent.
It seems SMO wasn’t built in a thread-safe manner. When you call
Script()on aTable, it uses some shared state from itsServer, so you can’t execute it on two tables from the sameServer. But you can work around that by creating newServerobject for eachTable:This will make your queries parallel, but I have no idea whether it will make them actually faster.
EDIT: If you want to create one
Serverfor each thread, you could use an overload ofParallel.For()that allows thread-local initialization. Something like:This way, each thread will have its own
Serverobject, but only one.