Multiple Configurations for NHibernate (HybridSessionBuilder)

 

I have been using Castle Windsor and NHibernate for some months now, so I am a bit new and still learning. But then again, it is learning developers like me, that asks all the questions.

This post is also written because of a simple question: Does the HybridSessionBuilder support multiple configurations?

Why this question? Well, I have a project where some repositories uses one database and some other uses another database.

Original implementation

My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:

http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/

But as you can see:image

The original implementation used a static session factory and session, so this is does not support multiple configurations.

CodeCampServer/Tarantino

I then found out that the CodeCampServer also was using the HSB. Hoping to find an implementation that supported my request, I then started to read the source code.

But by viewing the source code for CodeCampServer I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa :)

Furthermore I found out that the HSB now is a part of a collection of libraries called Tarantino.

I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.

Test web application

image Then I build a small test web application project. It used Castle Windsor (for dependency injection) and NHibernate. I quickly configured Castle Windsor and made two NHibernate configuration files.

nhibernate.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true

nhibernate2.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true

My windsor.config.xml looks like this:

<component id="firstRepository" type="HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate.cfg.xml</ConfigurationFile>
  </parameters>
</component>
 
<component id="secondRepository" type="HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate2.cfg.xml</ConfigurationFile>
  </parameters>
</component>

It basically just configures the two repositories with each own configuration.

But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:

image

Only the default NHibernate configuration file was used.

Bug or feature?

That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.

I was expecting an object structure like this:

image

But I got something like this:

image  I identified the following method to be the central, when the session was instantiated:

   1: private ISession getExistingOrNewSession(ISessionFactory factory, string configurationFile)
   2:         {
   3:             if (HttpContext.Current != null)
   4:             {
   5:                 var session = GetExistingWebSession();
   6:  
   7:                 if (session == null || !session.IsOpen)
   8:                 {
   9:                     session = openSessionAndAddToContext(factory, configurationFile);
  10:                 }
  11:  
  12:                 return session;
  13:             }
  14:  
  15:             var currentSession = _currentSessions.ContainsKey(configurationFile)
  16:                                     ? _currentSessions[configurationFile]
  17:                                     : null;
  18:             if (currentSession == null || !currentSession.IsOpen)
  19:             {
  20:                 _currentSessions[configurationFile] = OpenSession(factory);
  21:             }
  22:  
  23:             return _currentSessions[configurationFile];
  24:         }

Patch/solution

The problem I identified is line #5. It returns the existing session based on the default configuration, no matter what you specify in the configurationFile parameter.

I changed it to:

   1: var session = GetExistingWebSession(configurationFile);

 

And tadaaa:

image

The second repository now uses the second configuration file :)

Conclusion

You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.

First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.

Second of all, as NHibernate and Castle Windsor is still a bit new to me, one of the best ways to learn its capabilities and how to use it, is to dig in deep. And that is exactly what I have done.

And BTW – I have not just learned about HSB, but also CodeCampServer, Tarantino, S#arp and how it handles multiple configurations, and how it all works together.

4 thoughts on “Multiple Configurations for NHibernate (HybridSessionBuilder)”

  1. Hi, Mr. Mikkel Ovesen

    Your code is very nice. I also need to have Multiple Configurations for NHibernate in my code.

    Your code provides a lot of help. But I am not quite sure how to retrieve ConfigurationFile from windsor.config.xml, and also, I am not quite sure how did you implement HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest. Would you like to show me the all codes related? My email address is: margerate@gmail.com

    Thank you very much,
    Monique

  2. @Monique

    You set the ConfigurationFile from Windsor.config.xml by using parameters. Try looking here:
    http://www.castleproject.org/container/documentation/trunk/manual/windsorconfigref.html

    I don’t have the code used in the example anymore, howevere here is the code for NHibernate base repository. You will see the ConfigurationFile property:

    using NHibernate;

    namespace NHibernateLib
    {
    public class NHibernateRepositoryBase
    {
    private readonly ISessionBuilder _sessionFactory;
    private static object SESSION_LOCK = new object();

    public NHibernateRepositoryBase(ISessionBuilder sessionFactory)
    {
    _sessionFactory = sessionFactory;
    }

    public virtual string ConfigurationFile { get; set; }

    protected ISession GetSession()
    {
    lock (SESSION_LOCK)
    {
    return ((ConfigurationFile == null) ? _sessionFactory.GetSession() : _sessionFactory.GetSession(ConfigurationFile));
    }
    }
    }
    }

    I hope it helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>