哪些 ADO.NET 数据集/数据表方法对于多个读取器线程是安全的? [英] Which ADO.NET DataSet/DataTable Methods Are Safe For Multiple Reader Threads?

查看:37
本文介绍了哪些 ADO.NET 数据集/数据表方法对于多个读取器线程是安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为相对静态且定期从数据库更新的数据创建自定义缓存对象.我选择使用强类型数据集来存储缓存数据.现在,通过 ReaderWriterLockSlim 同步对自定义缓存对象的读取和刷新(客户端无法写入缓存,只能刷新)访问.但是,我想确保缓存的客户端不会通过同时修改其组成对象来破坏强类型数据集中的数据(DataTables、DataRows 等),即使客户端 ~不应该~ 更改数据.因此,我的方法是,在查找缓存项时,克隆强类型 DataSet 并用所需行及其相关父/子行的副本填充它,然后将其返回给客户端.基本上,将不可变缓存数据的副本返回给客户端,这样即使他们尝试修改它,也不会影响其他线程.

I am creating a custom cache object for data that is relatively static and is periodically updated from the DB. I have chosen to use a strongly-typed DataSet to store the cached data. Now, access for reading and refreshing (clients cannot write to the cache, only refresh it) to the custom cache object is synchronized via ReaderWriterLockSlim. HOWEVER, I want to ensure that clients of the cache cannot corrupt the data (DataTables, DataRows, etc.) within the strongly-typed data set by concurrently modifying its constituent objects, even though clients ~shouldn't~ change the data. So, my approach is, upon looking up a cache item, clone the strongly typed DataSet and populate it with copies of the required row and its related parent/children rows and return that to the client. Basically, return a copy of the immutable cache data to the client so even if they try to modify it, no other threads will be affected.

我的问题是,这可以在 ReaderWriterLockSlim 读锁中安全地完成吗?更直接地说,诸如 DataSet.Clone、DataTable.ImportRow 之类的方法对于读取器线程来说是天生安全的,即它们是对克隆/复制对象的只读操作吗?考虑一下 MSDN 文档中有关 DataSet、DataTable 等的注释.

My question is, can this safely be done within the ReaderWriterLockSlim read lock? More directly, are methods like DataSet.Clone, DataTable.ImportRow, inherently safe for reader threads, i.e. are they read-only operations on the cloned/copied object? Consider this note from the MSDN documentation for DataSet, DataTable, etc.

这种类型对于多线程读取操作是安全的.您必须同步所有写入操作."

"This type is safe for multithreaded read operations. You must synchronize any write operations."

推荐答案

是的.DataSet 和 DataTable 及相关项在读取时是线程安全的.正如您自己的报价所述.

Yes. The DataSet and DataTable and related items are threadsafe when it comes down to reading. As is stated by your own quote.

所以只要每个威胁克隆你的缓存项然后修改克隆,你就不用担心了.

So as long as each threat clones your cache-items and then modifies the clones, you have nothing to worry.

是的,这一切都可以在 ReaderWriterLockSlim 的范围内安全地完成,它用于同步多个读取操作和一个写入操作.

And yes, this can all be done safely withing the scope of a ReaderWriterLockSlim, which is made for synchronizing multiple read operations and one write operation.

这篇关于哪些 ADO.NET 数据集/数据表方法对于多个读取器线程是安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆