Linq to SQL - Dirty Reads after Updating - WCF Service

微笑、不失礼 提交于 2019-12-25 12:21:32

问题


I have a dbml file (linq to sql) to connect to my database. I have a manager that uses a generic repository (for each entity) to talk to the dbml. A manager uses the Repo to do work (CRUD operations). A WCF wraps the manager and exposes the it to the outside world (so my application can use it).

DB <--> IRepository< Entity, DataContext> <--> IManager(IRepo, IRepo...) <--> WCFService(IManager)

I've tested the manager, it works fine every time. The problem is the WCF. I'll read data (e.g. GetAllLocations), update the data (e.g. change a Location name), but then at some random point later, I'll read dirty data. E.g. if I've been changing a location name to from "BC 1", to "BC 2", to "BC 3", sometimes I'll read it and get older values. Like after I've changed it to "BC 3" and I am expecting to read "BC 3", I get "BC 1" (which doesn't make sense since the value before the update was "BC 2" anyway). WCF does not have caching by default, so why is this happening on my WCF and NOT on my manager? All the WCF does is pass values to the manager and get values from the manager, it's a very basic wrapper class.

NOTE: I am using StructureMap to automatically resolve DependencyInjection and IoC stuff.

Problem methods: anything that's reading (e.g. GetLocationById and GetAllLocations). They just don't always return the latest data from the database. I know it's not the manager because I created a simple project to test both the Manager and WCF independently, and only the WCF had dirty reads after updating data (specifically the Location). I have not testing all the other entities yet.

One final note: I kept getting the "System.Data.Linq.ChangeConflictException: Row not found or changed." exception. I changed the DBML designer Location Entity's properties (except the PK) to Update Check: Never. The dirty reads were happening before this change anyway and the manager works fine (and it uses the DBML). So I have no reason to believe this is causing the dirty reads.

Also, the Location Entity has a trigger in the database, but I'm eliminated this as the cause because I disabled it and that didn't help, and once again, the manager works fine.


回答1:


There are a couple of things that could be causing this:

  • The Isolational level of the ambient transaction scope, could be different
  • One difference going over WCF is that going through IIS could cause things to run concurrently

The things that you could try are:

  • Set the isolation level of the transaction scope
  • make sure that your connection string enables MARS - Multiple Active Result Sets



回答2:


Turns out this was a caching issue caused by using WCF and Structure Map. I'd changed the default structure map caching when registering my data contexts in the data registry.

For<MyDataContext>()
   //.HybridHttpOrThreadLocalScoped()
   .Use(() => new MyDataContext());

The default was per call which is what I needed.



来源:https://stackoverflow.com/questions/8262021/linq-to-sql-dirty-reads-after-updating-wcf-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!