Windows Phone 数据库并行访问

原文链接:http://queconejo.wordpress.com/2011/09/04/windows-phone-mango-and-concurrent-database-access/

by

Most windows phone developers might already know that the Mango release has a new  API For local database access.

大多数windows phone开发者可能已经了解到Mango已经发布了一系列全新的关于本地数据库访问的API。

Typically, windows phone developers are also familiar with asynchronous programming. E.g. when requesting a resource from the internet, the operation will complete on a different thread than it was started on.

通常windows phone开发者对于异步编程非常熟悉了。 例如,当请求网络资源时,访问操作将在与发起访问不同的县城上完成。

What caught my attention is that the 2 don’t work well together.

这二者工作工作起来并非一帆风顺,这引起了我注意的。

Example

If you download multiple resources from the internet and store these in a local database, it might well be that one operation attempts to store its resource, while another operation is not finished storing another. This can result in one of the exceptions below:

如果你从网络下载多个资源并将其存入本地DB,可能的情况是,你试图将一个存储资源进入本地DB,然而其他存储资源的过程却并没有结束。这种情况下将导致下述异常:

  • InvalidOperationException “The operation cannot be performed because an operation on another thread has not been completed.”
  • InvalidOperationException “The operation cannot be performed during a call to SubmitChanges.”

These exceptions are not limited to storing data in the database, they will also occur when trying to read, update, delete or simply calling SubmitChanges.

不止往本地DB存储数据,当你试图读取,更新,删除或只是SubmitChanges时都会产生上诉异常。

Bottomline

When interacting with data in your local database, It will be up to you as a developer to make sure that you are none of it happens at the same time (=concurrently).

因此,当试图获取本地DB的数据时,你应当负起确保这些操作不会并发执行的责任来。

If you are using a foreach-loop when reading data from the database, there is reason for additional caution. You wont be able to do anything with the database until your foreach loop is completed.

当使用foreach循环读取数据时,应当小心了。在foreach完成之前,不应当进行其他的数据库操作。

UI Thread

One way to unsure(疑为ensure) that operations do not happen concurrently is to execute all of them on the same thread. However, the only thread that allows us to interrupt it and execute something in between other work is the UI Thread. And for performance reasons it doesn’t make sense to borrow time from the UI Thread to solve this problem.

确保不会并发执行的一个方法是使这些操作都在一个线程中执行。然而,唯一允许我们中断它并执行这些操作的线程是UI线程。然而处于性能考虑,实在不应该争抢UI线程的宝贵运行时间。

AutoResetEvent

A reasonably good solution can be implemented using the AutoResetEvent class (from the System.Threading namespace).

另一个比较恰当的解决方案就是使用AutoResetEvent。

The AutoResetEvent works like a revolting door for threads. It guarantees that no 2 threads can be “inside” at the same time: If a thread runs into such an AutoResetEvent, it will first have to wait for its turn and when its done it allows 1 other thread to take a turn.

AutoResetEvent对于线程就像一扇令人讨厌的门。它可以保证同一时刻只会有一个线程同时在门里边。当线程遇到AutoResetEvent,它首先必须等待(wait)。

We’ll have to make sure that all threads will go through the same “revolting door” and therefore it makes sense to create one as a static variable:

我们必须确保所有线程通过这扇令人讨厌的“门”,因此有必要建立一个静态变量:

//true means that the first thread that will be waiting can pass
public static AutoResetEvent OperationOnDatabase = new AutoResetEvent(true); 

Next we’ll need to make sure that any code that will interact with the DataContext will wait for its turn and allow another to take his turn once done.

我们应该确保在访问DataContext之前,必须等待被允许执行。

try
{//wait for my turnOperationOnDatabase.WaitOne();//interact with the database//use 'ToArray' to make sure we are indeed done//with our DataContext before we continuereturn DataContext.Items.ToArray();
}
finally
{//always give my turn away when done.OperationOnDatabase.Set();
}

We should repeat this pattern every time we need to interact with the database.

在进行任何数据库访问时,都应该遵循同样的原则。

Notes

First and foremost, if a thread calls “WaitOne” but never calls “Set” (If a thread takes its turn but doesn’t give it away) no other thread will be able to get past “WaitOne”.

首先,如果线程WaitOne却没有其他线程Set,那就永远无法只需执行了,只能无休止等待。

Just like with a normal revolting door, you could get a bit of a queue if there’s a lot of threads trying to enter at the same time (or threads take a long time “inside”: in between calling “WaitOne” and “Set”).

如果同时又很多线程试图进入的话,那么门外必将排起长龙。

The occasional waiting will not impact the application much, it could easily happen during Application start or shut down and is preferable over unhandled exceptions. But it probably makes more sense to wait on a background thread, rather than using the UI thread for this.

短暂等待不会影响程序太多,它可以很容易地发生在应用程序启动或关闭时,比未处理的异常要好的多。在后台线程等待将比UI线程,更可取。

Relying on this mechanism to perform a large amount of database operations concurrently could have some additional adverse effects. e.g: Other parts of the application could stop working if there is a large number of threads waiting in the threadpool.

依靠这种机制,同时执行大量的数据库操作的能有一些额外的不利影响,例如,尤其当线程池中有太多线程等待的话,程序其他部分可能会暂停工作。

Conclusion

Using the AutoResetEvent we did manage to get around our problem fairly easily.

使用AutoResetEvent可以很轻易解决我们的问题。

The first downside is that we do need to add and maintain extra code when interacting with the database.

第一个不便是,需要在访问数据库是添加并维护额外的代码。

The second downside is the potential of threads unnecessarily waiting for each other. Especially if we are not even really interested in the result.

其二,线程间可能不必要地互斥等待。尤其当不是很在意结果是。

Next post we’ll look try to find a better solution to our problem and dive some deeper into thread synchronization.

下次,我将试图寻找一更好的方案并深入剖析下线程同步。(原链接自September 2011未有更新,或许可以继续期待吧)

转载于:https://www.cnblogs.com/songtzu/archive/2013/01/24/2874623.html

Windows Phone 数据库并行访问【转】相关推荐

  1. spark集群访问mysql_一种Spark并行访问MPP数据库的方法与流程

    本发明涉及数据库 技术领域: :,具体来说,涉及一种Spark并行访问MPP数据库的方法. 背景技术: ::从关系数据库读取数据,Spark可以使用DataFrame和JdbcRDD等方法.这些方法在 ...

  2. 基于C API的MySQL数据库多线程访问方法

    说明:如何生成线程式客户端 客户端库总是线程安全的.最大的问题在于从套接字读取的net.c中的子程序并不是中断安全的.或许你可能希望用自己的告警中断对服务器的长时间读取,以此来解决问题.如果为SIGP ...

  3. Oracle字符串转BooIean,利用Java的多线程技术实现数据库的访问.pdf

    利用Java的多线程技术实现数据库的访问.pdf 第 卷第 期 计算机应用 22 12 Voi .22 , No . 12 年 月 2002 12 Computer Appiications Dec ...

  4. 数据库防火墙——实现数据库的访问行为控制、危险操作阻断、可疑行为审计...

    转自百度百科 数据库防火墙系统,串联部署在数据库服务器之前,解决数据库应用侧和运维侧两方面的问题,是一款基于数据库协议分析与控制技术的数据库安全防护系统.DBFirewall基于主动防御机制,实现数据 ...

  5. windows系统共享文件夹访问无需验证(输入用户名和密码)即可进入

    windows系统共享文件夹访问无需验证(输入用户名和密码)即可进入 转载自:http://www.jb51.net/os/windows/90835.html 有时就会出现当其他人访问共享文件夹时, ...

  6. 如何查询当前表空间下所有实例_详解人大金仓MPP数据库并行查询技术

    什么是MPP数据库? 人大金仓MPP数据库的 并行查询技术原理是什么? 如何实现并行查询?性能如何? 且听以下详细分解~ 01 什么是人大金仓MPP数据库? KingbaseAnalyticsDB(简 ...

  7. oracle 查看并行数据库,Oracle数据库并行查询出错的解决方法

    Oracle的并行查询是使用多个操作系统级别的Server Process来同时完成一个SQL查询,本文讲解Oracle数据库并行查询出错的解决方法如下: 1.错误描述 ORA-12801: 并行查询 ...

  8. 资源放送丨《并行不悖——Oracle数据库并行的是是非非》PPT视频

    前段时间,墨天轮邀请Oracle的资深专家 杨廷琨 老师分享了<并行不悖--Oracle数据库并行的是是非非>,在这里我们将课件PPT和实况录像分享出来,供大家参考学习. 并行执行是Ora ...

  9. 明晚来墨天轮直播间,听杨长老聊聊Oracle数据库并行的是是非非

    并行不悖--Oracle数据库并行的是是非非-04.01 并行执行是Oracle应对大数据量处理的强大能力之一,而由于其内部的复杂性,很多DBA对于并行执行的特点甚至是如何看懂并行执行计划都不是很清楚 ...

  10. 阿里云下mysql远程访问被拒绝_记一次MySQL数据库拒绝访问的解决过程

    最近在折腾wordpress博客,连接MySQL数据库时提示拒绝访问.经过排查,解决了问题.这里记录下解决问题的方案,以及解决的思路.如有遇到类似问题的读者可以参考下. 用wordpress搭博客,数 ...

最新文章

  1. 最短路算法总结(入门版)
  2. RCNN (Regions with CNN) 目标物检测
  3. 线程和进程之间的联系----基本概念
  4. activity的启动窗口
  5. 不平等博弈问题学习记录(三)(对于超实数在博弈下左大右小以及多堆情况的扩充)
  6. java 偏移符号_java中的移位运算符总结
  7. 新方法-根据上排给出十个数,在其下排填出对应的十个数
  8. JJWT签发与验证token
  9. matlab确定位置,Hurlin 的PSTR模型包,怎样确定位置参数个数
  10. 数据库优化 - 多列索引经典题目
  11. 30 岁后,哪些职业瓶颈阻碍了你的成长?
  12. 基于GEE平台实现湖泊水位与水体面积关系分析
  13. Android计分器课程设计,课程设计题八:篮球比赛计分器
  14. 360实习之--技术基础H卷
  15. 找到数据库中最大数据量的表
  16. php phpmailer 发送邮件
  17. html5图片无限循环播放,原生js实现无限循环轮播图效果
  18. 从一台 Windows 10 上共享文件夹到Docker中的Volume卷
  19. 第16集丨阳明心学量子力学
  20. jQuery取值和赋值的基本方法

热门文章

  1. fedora与win双系统 设置win为默认启动
  2. imindmap12新版本 思维导图软件
  3. 用40年前的电脑打开女神图片,这你敢信?
  4. 求求你,别再叫我 X 工了!!!
  5. 知乎面试官:为什么不建议在 MySQL 中使用 UTF-8?
  6. Spring Boot + PageHelper 实现分页,总结得很全了!
  7. 凌晨!腾讯紧急宣布再度延期复工时间到24号;上班感染肺炎算工伤;小米VIVO完成统一推送适配...
  8. 蘑菇街移动端混合开发体系的研发与实践
  9. 史上最强Dubbo面试26题和答案:核心组件+服务治理+架构设计等
  10. 如何查看android虚拟机的目录及文件