First, of course, a separate ISessionFactory per database/tenant/connection.
Creating ISessionFactory is time consuming operation, so I would not
build factories for all tenants at startup in one round/cycle, but
build them "on demand" combined with some kind of "cleanup" service
that will dispose factories if they weren't used for some time.
To see how you can manage multiple ISessionFactories, take a look at
NHibernateIntegration facility from Castle stack. That's of course if
you're using Castle & Windsor container. NHibernateIntegration will
give you next API:
ISessionManager.OpenSession(factoryId);
where "factoryId" can be very well your "tenant/tenantId".
To use it for building factories dynamically you'll have to override
how the facility is initialized to avoid reading of configuration from
the XML.
NHibernateIntegration also will "help" you manage transactions and
will keep your sessions in Web context.
Also, for a sample how you can manage multiple ISessionFactories you
can look to this post about session factory providers:
http://fabiomaulo.blogspot.com/2009/09/configure-sessionfactory-providers.html
Feel free to ask if you have questions. We have pretty same design in our app...
Cheers,
On Thu, Oct 8, 2009 at 2:27 PM, Michael Delaney <[email protected]> wrote:
>
> Hi,
>
> I have a multi-tenant web app using NH. Tenants are added and removed
> at runtime and i'm going with the database per tenant approach.
>
> Basically a request comes in like this: http://tenant1.host.com/whatever
>
> I have a HttpModule which takes the "tenant1" part and looks up the
> Tenant specific config in the Host database. The Host db will contain
> a table of Tenants.
> This will then validate that the Tenant exists etc.
>
> At this point i need to access the Tenant database. So i need a
> SessionFactory to create a Session.
>
> My question is, how should i approach this.
> At startup:
> I could access the Host db and list out all the Tenants,
> Create a session factory for each one and then put them in a
> dictionary (to keep the example simple).
>
> Then access like:
> ISession session = sessionFactories[TenantName].CurrentSession();
>
> But if I end up with 1000's of Tenants would that be inefficient.
>
> Would I instead:
> Use one SessionFactory for the Tenants (they are all using the same
> schema after all),
> Create a Tenant specific connection and pass it to the SessionFactory,
> i.e. Factory.OpenSession(TenantSpecificConnection, ......)
>
> But this complicates the 2nd level cache management.
>
> Any advice???
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---