Method response problem with WCF
I have two methods from which first i am using to insert data and second to get current count of ins开发者_JS百科erted data. Both are WCF service methods. I need to insert bulk records in database so parallely i want to check how many records are inserted. For that i am using my second method.
But unfortunately until and unless first process give response second is not giving response. I want to execute both methods independently. I am using Ajax-enabled WCF services. I am not able to track my problem please help me.
Thanks.
if your insert operation does it s work in a single transaction, then the other operation cannot see the new records untill the transaction is complete.
you can choose to run the select (second) operation in read uncomitted
isolation level, if you dont mind the results not beeing 100% accurate and stable.
if this isnt acceptable, you can write your progress to another table, every few rows - in a seperate transaction, (new not nested transaction scope), so that the select operation can read that instead.
You are calling this from within SQL Server?
Why not just have the bulk update run in a separate thread, and just return back quickly that it has been queued and is being processed.
I have done this to prevent using too many database connections, I just put the data into a queue, and every ten seconds I process whatever is in the queue.
Then your second function can check on the status.
UPDATE:
I have a static class that does the processing, and the code is at the end. Basically, the call comes in and I return immediately that it was successfully put in a queue. The queue just runs in the background, checking every ten seconds and processing whatever is in there.
I use a ConcurrentQueue
for my queue so that I can write and process at the same time.
So, you can use this to start updating, then another call can see how many have been processed and return that information back.
public string[] UpdateJobs(Jobs[] jobs)
{
WebServiceControllerStatic.JobQueue = jobs;
WebServiceControllerStatic.ProcessUpdateJobQueue();
List<String> s = new List<String>();
for (int t = 0; t < jobs.Length; t++)
{
s.Add("Success");
}
return s.ToArray();
}
These are in WebServiceControllerStatic class:
public static Jobs[] JobQueue
{
set
{
try
{
value.AsParallel().ForAll(i => jobqueue.Enqueue(i));
}
catch (Exception e)
{
writeToLog("..\r\n" + e.Message + "\r\n" + e.StackTrace);
}
}
}
public static void ProcessUpdateJobQueue()
{
Action UpdateJob = () =>
{
if (jobqueue.IsEmpty)
return;
Parallel.For(0, jobqueue.Count, new ParallelOptions() { MaxDegreeOfParallelism = 1 },
(j) =>
{
// Do updates here
});
};
if (processUpdateJobThread == null)
{
processUpdateJobThread = new System.Threading.Thread(() =>
{
while (true)
{
UpdateJob();
System.Threading.Thread.Sleep(10000);
}
});
processUpdateJobThread.Start();
}
}
The second operation can not finish until the first one is done, you can generate an operation key for each bulk insert, and the second operation can use that key to get the number of inserted records.
The contract should like like this:
Guild DoBulkInsert(string commands);
int? GetCount(Guid bulkId);
+1 on the @Menahem answer.
In addition, you should also consider using SqlBulkCopy instead of inserting them one by one. In my experience this will greatly improve performance.
精彩评论