I'm looking into using NHibernate to handle the persistence layer in my business application. Right now, I'm wondering how to handle concurrent edits/updates in NHibernate, or more specifically, how to let multiple, distributed instances of my application know that one user changed a given dataset.
I'm not looking for versioning, i.e. consistency in the face of concurrent edits - I know NHibernate supports optimistic/pessimistic concurrency, and I currently use optimistic concurrency via and handling the StateStateException
.
I just want to know: Given that user A changes a row in the dataset, how to let user B know that a change occured so that the dataset can be reloaded? Right now, I'm lazy loading a large list of customers into a grid using NHibernate. Now, if a new customer is added, I'd like to add it to the grid without reloading the whole data again - the application's first concern is performance.
Also, will NHibernate gracefully handle changes to existing rows? Will it somehow detect changes in the underlying database and update the in-memory .NET objects so that accessing their properties will yield the updated values?
I thought about using an additional table, saving the IDs of updated objects along with a timestamp to refresh items myself, but if NHinbernate offers something of it's own, that would be a much bett choice, obviously...
You need database-level notifications/events for this. That is, the database engine has to provide notifications. For example, SQL Server has this feature. NHibernate runs on the application, so all it could potentially do by itself is polling the database for changes (your idea of using an additional table looks like polling), and we all know that's not good. NHibernate's SysCache2 takes advantage of SqlCacheDependencies to invalidate cache entries when the database raises a notification, but I'm not aware that it can raise any events to be consumed by the app itself (which wouldn't be useful anyway, since 2nd-level caches don't work with whole entities).
Another possible way to implement this would be having a NHibernate event listener place a broadcast message on a bus after any updates, then each application instance would receive this message and re-fetch from database accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With