0

I've got a Windows Service and a ASP.NET MVC 2 application that both connect to the same database using Fluent nHibernate (nHibernate 3).

One of the entities (called Profile) has a string property that contains the url to an image.

This property randomly looses it's value and I can't figure out what is causing it. The users update this via the MVC front end but my gut feeling is the windows service is overwriting its value with an older instance of the entity (the windows service updates another property on the same class).

Can someone please explain / help with how to either debug this or the best way to manage the concurrency, I've read up on the subject but am getting confused about optimistic and pessimistic locking in nHibernate.

Kind regards

Sam

Sam Lad
  • 267
  • 5
  • 14

1 Answers1

0

An option could be using <version/> with Optimistic Locking. With this you have no performance drawback, and you are sure the result is what you want. If one agent try to update a record another agent updated before, that agent will fail. Of course you should handle somehow that exception, but at least you don't have dirty updates/reads. To use <version/> tou should add a column in your table to mantain the version. In standard NH mapping the documentation is:

<version
        column="version_column"                            
        name="PropertyName"                                
        type="typename"                                    
        access="field|property|nosetter|ClassName"         
        unsaved-value="null|negative|undefined|value"      
        generated="never|always"                           
/>
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • So when the Windows Service does an update and a StaleObjectException gets thrown I could manage this by doing another read (to get the latest version and re-apply the changes?) if so how could I do this in a generic Repository? Would I just use reflection to update each value that's different? – Sam Lad Nov 02 '12 at 16:00
  • @SamLad Don't use reflection, make the repository able to handle such a situation – Felice Pollano Nov 02 '12 at 16:10
  • Thanks Felice, So if I make the repositories that use the generic repository underneath manage the concurrency as each method is aware of what properties have changed. Bigger job than I hoped but it makes sense. – Sam Lad Nov 02 '12 at 16:17