Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate - Two SessionFactories accessing the same database

This is related to a previous question I asked, regarding splitting a asp.net mvc web application into two apps - one public, and one admin. Both of them would be sharing the same database of course.

With two web apps I'll have two host applications and so two seperate Nihbernate session factories. My concern is that their seperate data caches will cause all sorts of problems.

While posting this, I looked at this question, which covers a similar problem (for a different reason). I really don't want to have to set up some kind of distributed cache just for a low use admin application.

How can this be resolved? Is separating the admin section into it's own application not possible with NHibernate without drastic measures?

Thanks.

like image 805
UpTheCreek Avatar asked Nov 14 '22 04:11

UpTheCreek


1 Answers

We run this successfully although some discrepancy in the data is always a problem. However, since the 2nd level cache is configurable per site, you can disable as well as turn it down for specific cache areas on your manager.

The 2nd level cache will only be used for reading, since explicit updates will be flushed down and persisted directly.

If your concern is that content on the site will be "old" once modified, some sort of trigger will be needed to instruct the site to evict the cache. NHibernate will then evict all 2nd level cache for a specific entity type if I remember it correctly.

I think your problem with concurrency will be minimal if your site vs your admin will update different entities. For example in a webshop:

Site will create orders, modify customers etc but only read products, prices and categories

Admin will modify orders, products, prices and categories but only read customers

You can however instruct NHibernate to only update modified fields/properties on your objects for entities that you are concerned about concurrency issues with dynamic-update="true" on your mapping. This won't fully solve your problem, but minimize concurrency issues.

like image 99
jishi Avatar answered Dec 23 '22 09:12

jishi