views:

252

answers:

2

As far as I've gathered (read: measured), building the configuration and the sessionfactory by far takes the most time in executing a query using nhibernate. Is there anything against making the sessionfactory static, so it will only be configured once per appDomain?

I know there are locking and racing issues when using this approach, but personally I don't see where this would break my application when using this approach on the sessionfactory.

The reason I am asking this is because it's really hard to test for possible threading issues, as it doesn't occur all the time.

+4  A: 

Session factory should be started at the application start indeed. You could check the best practices here.

Petr Macek
Thanks. That link is very nice as well, finally a good explanation of good and bad singleton implementations ;-)
Erik van Brakel
A: 

The SessionFactory is intended to be cached - only the Session should be generated per-request. Initializing is not thread-safe, so you'll have to lock{} it if your startup code can have more than one thread come through it.

Isaac Cambron