Access Main Thread uncommited data in worker thread
I am running into one issue with the code I am working on. The task is I get a file with Purchase Order details. The purchase order has individual items and each item has its delivery schedule. The creation of PO is atomic (all or nothing). So all these is running under one transaction.
I already have following methods for the sub tasks. 1. CreatePO 2. CreateItem 开发者_开发问答 3. AddDeliveryScheduleToItem(ItemId).
When I execute all this in sequential fashion everything works fine. Sequential fashion means first I create PO. Second I create all Items under PO one by one. Third I add delivery schedule to each line item one by one. Here the Transaction start before creating the PO and commit after the delivery schedule is added to all items. If any error the app roll back the transaction.
While adding delivery schedule to item I do retrieve the Item record from database to retrieve the schedule details thats how current code is written.
So far so good.
Now adding delivery schdule is time consuming task so we decided to add threading and run the adding delivery schedule in parallel. So I created a method called AddDeliveryScheduleInThread() and call AddDeliveryScheduleToItem(ItemId) within this method.
Using ThreadPool I fire AddDeliveryScheduleInThread() in parallel. Now issue arise when AddDeliveryScheduleToItem(ItemId) tries to retrieve the Item record based on item id. Remember we created this item in main thread in transaction but it is not commited yet.
The code currently using Spring.NET transaction and I can't find much help over there.
How to resolve this issue ? Please help me.
I am open to not using the Spring.NET transaction. I am looking into Dependable Transaction and Commitable Transaction right now if that can help.
Thanks
AddDeliveryScheduleToItem(ItemId) tries to retrieve the Item record based on item id. Remember we created this item in main thread in transaction but it is not commited yet.
I can think of only two options.
1) Don't AddDeliveryScheduleToItem(ItemId) until the transaction is committed.
2) Don't put AddDeliveryScheduleToItem(ItemId) on another thread but instead make it very fast so you don't have to put it on another thread.
The easy way to make 2 very fast is to simply add it to a queue rather than do the whole process. Have another process looking at that queue to see if there a new items to Schedule and then schedule do the long running bit.
精彩评论