We have a custom connection string provider that connects to different databases based on the current context.
Doing so uses the same SessionFactory for all databases.
Assuming we may have entities with the same identifiers (i.e. primary key) in different databases, is this taken into account when an item is retrieved from either the first or second level caches?
So if Entity-1 for Db-1 is in the second level cache and I request Entity-1 for Db-2 will NHibernate load the entity from cache (incorrect) or the Db-2 database (correct).
After a bit more research (namely here):
The database name/schema is not used by either the first or second level caches to retrieve entities.
Whilst the second level cache is tied to the session factory, the cache provider you use is not guaranteed to be partitioned (see here).
The solution to this (assuming the cache provider supports it) is to set the
RegionPrefixfor the cache to be unique per tenant.