如何在WCF服务中同步数据库访问? [英] How to synchronize database access in a WCF Service?

查看:164
本文介绍了如何在WCF服务中同步数据库访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前使用过WCF服务,现在我有一个新的项目了。
我仍​​然在设计阶段,我想知道什么是最好的方式来处理以下情况。



我将有多个客户端连接在同一个时间到我的WCF服务,在服务上触发不同的方法(操作合同):



A。读取方法(例如GetListOfCustomers)。



B. 一些是被激活的方法是复杂的读取方法(例如GetAllProductsByCustomerId)。
这些方法需要从DB获取客户,
检查他的东西,然后得到他买的所有产品。
(意思是,在这个方法中有两个数据库调用)。



C。例如'RemoveCustomer'或'SetProductOutOfStock')。



我的问题是 - 如何同步所有这些调用,使我不会遇到并发问题?

我不要希望整个服务连续处理呼叫,因为它会损害客户端的性能(某些呼叫可能需要3-4秒处理)。
那么,我的解决方案是什么?



对于具有'多个'线程的所有客户端使用'Single'实例,然后使用锁定对象?这不会只是导致序列吗?



或者我需要一个不同的锁对象为'读'和不同的锁对象的'写'?



或者我需要一个锁来为'写'函数和其他一些东西读读函数?



这是我对StackOverflow的第一个问题。
感谢任何人可以帮助!



更新:我将使用Linq-To-SQL作为我的ORM。

解决方案

您不应担心数据一致性问题,也不应担心在执行数据库查询时的并发性。如果我正确理解你的情况,你需要确保你在执行一系列数据库查询,你想要原子一致地使用事务。我将尝试以一个例子解释它:


  1. 从数据库获取所有客户。

  2. 对于每个客户,执行
    查询更新一些相关数据。

您不希望在此方案是在 c> 之后之后之前的之后之后,来自 2 的查询已完成。例如,如果其中一个客户同时被删除,则更新相关数据没有意义 - 甚至可能导致一些错误。



因此您需要做的只是在 1 COMMIT 之前放置 BEGIN TRANSACTION $ c>之后 2 。查找您使用的SQL方言的确切语法。



这将确保您正在使用的数据不会改变。事实上,它会被你的事务锁定;和所有其他可能使用相同数据的查询正在等待您的事务完成。数据库会智能地做这种锁定,总是尽量锁定尽可能少的数据。


I'm have used WCF services before, and now I have a new project coming up. I am still in the design phase, and I wondered what the best way to handle the following scenario.

I will be having multiple clients connecting at the same time to my WCF service, firing different methods (Operation Contracts) on the service :

A. Some of the methods fired are just pure 'Read' methods (e.g. GetListOfCustomers).

B. Some are the methods fired are complex 'Read' methods (e.g. GetAllProductsByCustomerId). These kind of methods require getting the customer from the DB, checking something on him, and then getting all the products bought by him. (meaning, there are 2 calls to the database in this method).

C. Some are 'Write' methods (e.g. 'RemoveCustomer' or 'SetProductOutOfStock').

My question is - how do I synchronize all these calls so that I don't get concurrency problems?

I don't want the entire service to process calls serially, because it will damage performance on the client side (some calls may require 3-4 seconds to process). So what is my solution ?

Use a 'Single' instance for all clients with 'Multiple' threads and then use a lock object ? Won't this just result to being serial ?

Or do I need a different lock object for the 'read' and a different lock object for the 'write' ?

Or do I need a lock for the 'write' functions and some other thing for the 'read' functions ?

This is my first question ever on StackOverflow. Thanks for anyone who can help !

Update : I am going to use 'Linq-To-SQL' as my ORM.

解决方案

You should not be worried neither about data consistency problems, nor about concurrency while performing database queries. If I understood your situation correctly, all you need to make sure of is that you use transactions consistently while performing a series of database queries which you want to be "atomic". I'll try to explain it on an example:

  1. Get all customers from the database.
  2. For each customer, perform a query updating some related data.

What you don't want to happen in this scenario is to get into a situation when data is changed by another query after the query from 1 returns and before all queries from 2 are finished. For example, if one of the customers gets deleted in the meanwhile, there's no point in updating the related data - that could even lead to some errors.

So what you need to do is to just put something like BEGIN TRANSACTION before 1 and COMMIT after 2. Look up the exact syntax for the SQL dialect you're using.

This will ensure, basically, that the data you're working with does not change. In fact, it will be locked by your transaction; and all the other queries which may work with the same data are waiting for your transaction to finish. Databases do this kind of locking intelligently, always trying to lock as few data as possible.

这篇关于如何在WCF服务中同步数据库访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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