0

I have a multithreaded environment (web application) and I have a static Datatable (in my ConcurrentDictionary custom cache object) that I am only using for reading data from.

Is it safe to share the same static datatable to all pages that are reading in loop from this datatable? I guess accessing and reading data from rows via index should be thread safe?

What about if i access my datatable object in the following manner? Or should I use my GetFromCache() method to return datatable.copy() object to each calling thread?

DataTable dtcountrySelector = GetFromCache(some conditions); if (dtcountrySelector != null) { List<MarketPlace> convertedList = (from drCountry in dtcountrySelector.AsEnumerable() select new MarketPlace() { SiteName = Convert.ToString(drCountry["name"] ?? ""), SiteId = Convert.ToInt32(drCountry["siteid"] ?? 0), SiteCode = Convert.ToString(drCountry["sitecode"] ?? "") }).ToList(); return convertedList; 

Thank you.

4
  • Google for datatable thread safety. And read learn.microsoft.com/en-us/dotnet/api/… . Commented Dec 16, 2020 at 11:46
  • Why store the DataTable in cache? Why not store convertedList instead? Commented Dec 16, 2020 at 11:47
  • I am sorry. I went through a lot of articles but simply reading msdn documentation didn't come to my mind. Really thank you for your hint. As for the other comment. This is old legacy CMS software and the only type of cached object is of type Datatable. I totally agree that a more granular approach could further boost performance as I suspect Generic List is less expensive as DataTable is. Thank you for your answer again. Commented Dec 16, 2020 at 12:06
  • 1
    Be aware: web applications already handle separate http requests on their own threads. That is, if your site is active enough to commonly be handling multiple requests at the same time you already get the benefits of multithreading out of the box, and doing more can mess with the thread tuning and actually slow things down. Instead, to improve CPU performance you should look at asynchronous processing, which allow the single thread assigned to an individual http request to queue each of the tasks needed to complete the request and complete them as they are ready, in any order. Commented Dec 16, 2020 at 17:51

1 Answer 1

2

The DataTable class is safe for multithreaded read operations. But you should be aware that the ADO.NET classes are highly interconnected, and operations that may be seemingly harmless can still cause mutations to the internal state of a DataTable. For example calling the DataTable.NewRow method is enough to mutate to the internal state of a DataTable, even without adding the row in the DataTable.Rows collection. Another example: adding rows to another DataTable, when both DataTables are part of the same DataSet, could potentially cause mutations to both DataTables.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.