简体   繁体   English

在数据库访问应用程序中管理并发的最佳方法是什么?

[英]What's the best way to manage concurrency in a database access application?

A while ago, I wrote an application used by multiple users to handle trades creation. 不久前,我编写了一个供多个用户用来处理交易创建的应用程序。 I haven't done development for some time now, and I can't remember how I managed the concurrency between the users. 我已经有一段时间没有进行开发了,而且我不记得如何管理用户之间的并发性。 Thus, I'm seeking some advice in terms of design. 因此,我正在寻求有关设计的建议。

The original application had the following characteristics: 原始应用程序具有以下特征:

  • One heavy client per user. 每个用户一个重客户端。
  • A single database. 单个数据库。
  • Access to the database for each user to insert/update/delete trades. 访问每个用户的数据库以插入/更新/删除交易。
  • A grid in the application reflecting the trades table. 应用程序中反映交易表的网格。 That grid being updated each time someone changes a deal. 每当有人更改交易时,该网格都会更新。
  • I am using WPF. 我正在使用WPF。

Here's what I'm wondering: 这是我想知道的:

  1. Am I correct in thinking that I shouldn't care about the connection to the database for each application? 我是否认为我不关心每个应用程序与数据库的连接是否正确? Considering that there is a singleton in each, I would expect one connection per client with no issue. 考虑到每个节点都有一个单例,我希望每个客户端一个连接都没有问题。

  2. How can I go about preventing the concurrency of the accesses? 如何防止访问的并发? I guess I should lock when modifying the data, however don't remember how to. 我想我应该在修改数据时锁定,但是不记得要怎么做。

  3. How do I set up the grid to automatically update whenever my database is updated (by another user, for example)? 如何设置网格以在我的数据库更新时自动更新(例如,由另一个用户执行)?

Thank you in advance for your help! 预先感谢您的帮助!

Concurrency is usually granted by the DBMS using locks. 并发通常由DBMS使用锁来授予。 Locks are a type of semaphore that grant the exclusive lock to a certain resource and allow other accesses to be restricted or queued (only restricted in the case you use uncommited reads). 锁是一种信号量,可以将排他锁授予特定资源,并允许其他访问受到限制或排队(仅在使用未提交的读取的情况下才受限制)。

The number of connections itself does not pose a problem while you are not reaching heights where you might touch on the max_connections setting of your DBMS. 当您未达到可能会触及DBMS的max_connections设置的高度时,连接数本身并不构成问题。 Otherwise, you might get a problem connecting to it for maintenance purposes or for shutting it down. 否则,出于维护目的或关闭它而连接到它可能会遇到问题。

DBMSes usually use a concept of either table locks (MyISAM) or row locks (InnoDB, most other DBMSes). DBMS通常使用表锁(MyISAM)或行锁(InnoDB,大多数其他DBMS)的概念。 The type of lock determines the volume of the lock. 锁的类型决定了锁的音量。 Table locks can be very fast but are usually considered inferior to row level locks. 表锁可以非常快,但是通常被认为不如行级锁。

Row level locks occur inside a transaction (implicit or explicit). 行级别锁发生在事务内部(隐式或显式)。 When manually starting a transaction, you begin your transaction scope. 手动启动事务时,您将开始事务范围。 Until you manually close the transaction scope, all changes you make will be attributes to this exact transaction. 在您手动关闭事务范围之前,您所做的所有更改都将属于此确切事务的属性。 The changes you make will also obey the ACID paradigm . 您所做的更改也将遵循ACID范例

Transaction scope and how to use it is a topic far too long for this platform, if you want, I can post some links that carry more information on this topic. 对于该平台来说,交易范围及其使用方法是一个太长的主题,如果您愿意,我可以发布一些链接,其中包含有关此主题的更多信息。

For the automatic updates, most databases support some kind of trigger mechanism, which is code that is run at specific actions on the database (for instance the creation of a new record or the change of a record). 对于自动更新,大多数数据库都支持某种触发机制,该机制是在数据库上的特定操作(例如,创建新记录或更改记录)上运行的代码。 You could post your code inside this trigger. 您可以将代码发布到此触发器中。 However, you should only inform a recieving application of the changes, not really "do" the changes from the trigger, even if the language might make it possible. 但是,即使语言可能使您仅应将更改通知给正在接收的应用程序,而不应真正从触发器中“做”更改。 Remember that the action which triggered the code is suspended until you finish with your trigger code. 请记住,触发代码的操作将被暂停,直到您完成触发代码为止。 This means that a lean trigger is best, if it is needed at all. 这意味着如果完全需要精益触发,那是最好的。

  1. Consider leveraging Connection Pooling to reduce # of connections. 考虑利用连接池来减少连接数。 See: http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx 请参阅: http//msdn.microsoft.com/en-us/library/8xx3tyca.aspx

  2. lock as late as possible and release as soon as possible to maximize concurrency. 尽可能晚地锁定并尽快释放以最大程度地提高并发性。 You can use TransactionScope (see: http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx and http://blogs.msdn.com/b/dbrowne/archive/2010/05/21/using-new-transactionscope-considered-harmful.aspx ) if you have multiple db actions that need to go together to manage consistency or just handle them in DB stored proc. 您可以使用TransactionScope(请参阅: http : //msdn.microsoft.com/zh-cn/library/system.transactions.transactionscope.aspxhttp://blogs.msdn.com/b/dbrowne/archive/2010/05 /21/using-new-transactionscope-considered-harmful.aspx ),如果您有多个数据库操作需要一起管理一致性或仅在数据库存储的proc中处理它们。 Keep your query simple. 保持查询简单。 Follow the following tips to understand how locking work and how to reduce resource contention and deadlock: http://www.devx.com/gethelpon/10MinuteSolution/16488 请遵循以下提示以了解锁定如何工作以及如何减少资源争用和死锁: http : //www.devx.com/gethelpon/10MinuteSolution/16488

  3. I am not sure other db, but for SQL, you can use SQL Dependency, see http://msdn.microsoft.com/en-us/library/a52dhwx7(v=vs.80).aspx 我不确定其他数据库,但是对于SQL,您可以使用SQL依赖关系,请参见http://msdn.microsoft.com/zh-cn/library/a52dhwx7(v=vs.80).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C# 和 sql 中管理应用程序用户选项的最佳方法是什么? - what is the best way to manage application user options in C# and sql? 什么是在一个CMS / MULTIPLE WEBSITE系统中管理数据库的最佳方法 - What is best way to manage database in ONE CMS/MULTIPLE WEBSITE system 什么是管理图片的最佳方法WP 8.1 - What's is th best way to manage pictures WP 8.1 监视桌面应用程序的最佳方法是什么? - What's the best way to watchdog a desktop application? 使Winform应用程序访问网络文件的最佳和安全方法是什么? - What's the best and secured way to enable winform application to access network files? 更改架构后更新我的asp.net应用程序/数据库的最佳方法是什么? - What's the best way to update my asp.net application / database after changing the schema? 管理表单状态的最佳方法是什么? - What is the best way to manage Form state? 管理线程/内存使用的最佳方法是什么 - What is the best way to manage threads/memory usage 管理实体框架上下文的最佳方式是什么? - What is the best way to manage Entity Framework contexts? 什么是访问不同引擎但结构相似的不同数据库的最佳方法 - What is best way access different database on different engine but with similar structure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM