I have implemented my persistence facility for nhibernate following the castle windsor tutorials along the same lines.
See:
http://docs.castleproject.org/Windsor.Windsor-Tutorial-Part-Six-Persistence-Layer.ashx http://docs.castleproject.org/Windsor.Windsor-Tutorial-Part-Seven-Lifestyles.ashx
I have been noticing in my application that when the pool gets released and I try access it again I initially get errors relating to the session factory being built.
Things like, duplicate key has been added etc.
I think it has less to do with my mapping and more to do with the way or when the facility is getting executed.
Following some more research I see people saying that the session factory should be thread safe?
If this is correct, then my assumption is that I can't rely on Castle Windsor alone to ensure that the code getting executed will only be done once?
Please review my init method below of my persistent facility, should I add a further level of locking in the init method?
And would this be the correct place to do it?
My facility - follows the tutorials:
public class PersistenceFacility : AbstractFacility
{
// ... etc.
protected override void Init()
{
Configuration config = BuildDatabaseConfiguration();
Kernel.Register(Component.For<ISessionFactory>().UsingFactoryMethod(_ => config.BuildSessionFactory()).LifestylePerWebRequest());
Kernel.Register(Component.For<ISession>().UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession()).LifestylePerWebRequest());
}
}
I initialize my container in App_Start, and don't really do anything else special, I simply use ISession from my Repositories, all resolved using CastleWindsor.
Windsor will init every facility exactly as many times as you add the facility to the container.
So if you do it once, it will be initialised once and there's no need to put any locking there.
The only thing that looks weird in your code is the fact you're recreating session factory on each request.