<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog of developer Mikkel Ovesen &#187; nhibernate</title>
	<atom:link href="http://blog.ovesens.net/tag/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ovesens.net</link>
	<description>My thoughts, stuff I need to remember or things I just want to share with the world</description>
	<lastBuildDate>Thu, 19 Jan 2012 11:55:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NHibernate query optimising with HQL expression IN</title>
		<link>http://blog.ovesens.net/2011/11/nhibernate-query-optimising-with-hql-expression-in/</link>
		<comments>http://blog.ovesens.net/2011/11/nhibernate-query-optimising-with-hql-expression-in/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 20:28:42 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=430</guid>
		<description><![CDATA[A note for me to remember. I have been doing some optimisation of queries done by NHibernate HQL. To optimise I have rewritten some of them to use the HQL IN expression. Point: important to check the collection parameter, if &#8230; <a href="http://blog.ovesens.net/2011/11/nhibernate-query-optimising-with-hql-expression-in/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A note for me to remember.</p>
<p>I have been doing some optimisation of queries done by NHibernate HQL. To optimise I have rewritten some of them to use the HQL IN expression.</p>
<p>Point: important to check the collection parameter, if NULL or Empty, NHibernate criteria will fail.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2011/11/nhibernate-query-optimising-with-hql-expression-in/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NHibernate dirty problem, weird updates in NHprof</title>
		<link>http://blog.ovesens.net/2011/03/nhibernate-dirty-problem-weird-updates-in-nhprof/</link>
		<comments>http://blog.ovesens.net/2011/03/nhibernate-dirty-problem-weird-updates-in-nhprof/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 11:01:40 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nh3]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=352</guid>
		<description><![CDATA[Upgrading to NH3 from NH2.1.2 We have recently update to NHibernate 3 in one of our projects. While testing I was suddenly seeing some weird UPDATE statements in NHprof. It seemed like the NHibernate IsDirty detection had changed. After some &#8230; <a href="http://blog.ovesens.net/2011/03/nhibernate-dirty-problem-weird-updates-in-nhprof/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Upgrading to NH3 from NH2.1.2</h2>
<p>We have recently update to NHibernate 3 in one of our projects. While testing I was suddenly seeing some weird UPDATE statements in NHprof.</p>
<p>It seemed like the NHibernate IsDirty detection had changed.</p>
<p>After some digging it turned out that our mapping files (made in FluentNhibernate) actually contained some minor errors when compared to the DB schema. Minor errors like value typed properties not being nullable when the ClassMap said it should be.</p>
<h2>Debugging of NHibernate projects</h2>
<p>I found that inserting the following into the classmaps made debugging a lot easier:</p>
<pre class="brush: csharp;">DynamicUpdate();</pre>
<p>Now only the properties that are Dirty (according to NHibernate) are used in the SQL UPDATE statement. Here are the description from NHibernate docs:</p>
<ul>
<li><strong><tt>dynamic-update</tt></strong> (optional, defaults to <tt>false</tt>): Specifies that <tt>UPDATE</tt> SQL should be generated at runtime and contain only those columns whose values have changed.</li>
<li><strong><tt>dynamic-insert</tt></strong>: (optional, defaults to <tt>false</tt>): Specifies that <tt>INSERT</tt> SQL should be generated at runtime and contain only the columns whose values are not null.</li>
</ul>
<p>That led me directly to the property in the mapping files that had errors in it, and I could easily fix it.</p>
<h2>Conclusion</h2>
<p>So, in our case, it wasn&#8217;t really the NHibernate dirty detection that changed, NHibernate 3 is just not as fault tolerant, than that of NH2. So the upgrade made us discover some errors that should be corrected.</p>
<p>As such, this is a good thing, but it would have been nicer if NH2 had made us aware of these errors in the first place.</p>
<p>But hey, better late than never <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2011/03/nhibernate-dirty-problem-weird-updates-in-nhprof/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate CompositeId and GetHashCode SELECT N+1 problem</title>
		<link>http://blog.ovesens.net/2011/02/nhibernate-compositeid-and-gethashcode-select-n1-problem/</link>
		<comments>http://blog.ovesens.net/2011/02/nhibernate-compositeid-and-gethashcode-select-n1-problem/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 20:34:59 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=337</guid>
		<description><![CDATA[I was looking in my NHProf log and got a lot of SELECT request. To many and I thought I was having a SELECT N+1 problem. Using the &#8220;Stack Trace&#8221; in NHprof I identified a call being made to GetHashCode() &#8230; <a href="http://blog.ovesens.net/2011/02/nhibernate-compositeid-and-gethashcode-select-n1-problem/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was looking in my NHProf log and got a lot of SELECT request. To many and I thought I was having a SELECT N+1 problem.</p>
<p>Using the &#8220;Stack Trace&#8221; in NHprof I identified a call being made to GetHashCode() of the class (<em>MyClass</em>) holding the composite id. <em>MyClass</em> looked like this:</p>
<pre class="brush: csharp;">public class MyClass
{
    public virtual Key1Class Key1 { get; set; }
    public virtual Key2Class Key2 { get; set; }

    /*
     * Other virtual properties
     * ...
     */

    public override int GetHashCode()
    {
        unchecked
        {
            return ((Key1 != null ? Key1.GetHashCode() : 0) * 397) ^ (Key2 != null ? Key2.GetHashCode() : 0);
        }
    }
}</pre>
<p>Note that Key1 and Key2 are both references, and please take a closer look at the GetHashCode method.</p>
<h2>The problem</h2>
<p>The problem I was facing&#8230; NHibernate called the GetHashCode() of <em>MyClass</em> when doing the query. And the call to <em>MyClass</em>&#8216;s GetHashCode method subsequentily called GetHashCode() of both the Key1 and Key2 classes.</p>
<p>Key1 and Key2 were loaded as proxies and hence a call to something else than theirs Id resulted in a select being fired.</p>
<h2>The solution</h2>
<p>The solution was to rewrite the GetHashCode method to this:</p>
<pre class="brush: csharp;">public override int GetHashCode()
{
    unchecked
    {
        return ((Key1 != null ? Key1.Id : 0) * 397) ^ (Key2 != null ? Key2.Id : 0);
    }
}</pre>
<p>Note that both Id&#8217;s of KeyClass1 and KeyClass2 are integers.</p>
<p>Now a call to <em>MyClass</em> GetHashCode does not result in individual SELECT statements being fired, even though the Keys area lazy loaded.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2011/02/nhibernate-compositeid-and-gethashcode-select-n1-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NHibernate Membase caching provider</title>
		<link>http://blog.ovesens.net/2011/02/nhibernate-membase-caching-provider/</link>
		<comments>http://blog.ovesens.net/2011/02/nhibernate-membase-caching-provider/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 16:22:40 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[membase]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=322</guid>
		<description><![CDATA[Enyim has made a nice client library for both Memcache and Membase. Membase can be used as a free distributed caching platform. With the new provider model introduced in ASP.NET 4, it is now possible to write providers for output &#8230; <a href="http://blog.ovesens.net/2011/02/nhibernate-membase-caching-provider/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://memcached.enyim.com/">Enyim</a> has made a nice client library for both Memcache and Membase. Membase can be used as a free distributed caching platform.</p>
<p>With the new provider model introduced in ASP.NET 4, it is now possible to write providers for output caching and session state. Enyim has done just that with their <a href="https://github.com/enyim/memcached-providers">memcache-provider</a>.</p>
<p>I use NHibernate for some of my projects and being able to use a Membase cache provider for NHibernates second level cache would be very nice.</p>
<p>With help from <a href="http://dalager.com" target="_blank">Christian Dalager</a> and my self, such a library is now available. Take a look here <a href="https://bitbucket.org/ovesen/membasecacheprovider">https://bitbucket.org/ovesen/membasecacheprovider</a></p>
<p>Zipped binaries are provided with mapping against NHibernate 2.0.1, 2.1.0, 2.1.2 and NH3 here <a href="https://bitbucket.org/ovesen/membasecacheprovider/downloads">https://bitbucket.org/ovesen/membasecacheprovider/downloads</a></p>
<p>An example configuration file is provided in the downloads, but else you can see the options here:</p>
<pre class="brush: xml;">&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;configuration&gt;
    &lt;!-- This is a sample configuration file for using MembaseCacheProvider --&gt;

    &lt;configSections&gt;

        &lt;!-- (Required) Default configuration section for Membase --&gt;
        &lt;section name="membase" type="Membase.Configuration.MembaseClientSection, Membase" /&gt;

        &lt;!-- (Optional) An additional configuration section for Membase, can be used to target another bucket or servers --&gt;
        &lt;section name="membaseNhibernate" type="Membase.Configuration.MembaseClientSection, Membase" /&gt;

        &lt;!-- (Optional) A section for the Enyim Membase client, allows loggint to e.g. log4net --&gt;
        &lt;sectionGroup name="enyim.com"&gt;
            &lt;section name="log" type="Enyim.Caching.Configuration.LoggerSection, Enyim.Caching" /&gt;
        &lt;/sectionGroup&gt;

        &lt;!-- (Optional, but recommended) A configuration section that allows specific settings to be set for different NHibernate caching regions --&gt;
        &lt;section name="membaseNhibernateCache" type="MembaseCacheProvider.MembaseNhConfiguration, MembaseCacheProvider" /&gt;

    &lt;/configSections&gt;

    &lt;!-- (Optional) Defines that log4net should be used for Membase client logging, remember to define log4net configuration --&gt;
    &lt;enyim.com&gt;
        &lt;log factory="Enyim.Caching.Log4NetFactory, Enyim.Caching.Log4NetAdapter" /&gt;
    &lt;/enyim.com&gt;

    &lt;!-- (Required) Maps to the configuration section named "membase", defines a single server url and the "default" bucket --&gt;
    &lt;!-- Other configuration settings are available, look in the Enyim documentation and examples --&gt;
    &lt;!-- here http://memcached.enyim.com/ or here https://github.com/enyim/memcached-providers --&gt;
    &lt;membase&gt;
        &lt;servers bucket="default"&gt;
            &lt;add uri="http://localhost:8091/pools/default" /&gt;
        &lt;/servers&gt;
    &lt;/membase&gt;

    &lt;!-- (Optional) Maps to the configuration section name "membaseNhibernate", defines a single server url and the bucket "default_nhibernate" --&gt;
    &lt;membaseNhibernate&gt;
        &lt;servers bucket="default_nhibernate"&gt;
            &lt;add uri="http://localhost:8091/pools/default" /&gt;
        &lt;/servers&gt;

        &lt;!-- (Optional) Sets specific settings for the socket pool --&gt;
        &lt;socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" /&gt;

        &lt;!-- (Optional) Sets a specific locator --&gt;
        &lt;locator type="Enyim.Caching.Memcached.DefaultNodeLocator, Enyim.Caching" /&gt;
    &lt;/membaseNhibernate&gt;

    &lt;!-- (Optional, but recommended) Configuration settings of NHibernate second level cache, Membase provider --&gt;
    &lt;!-- The section attribute defines that the Membase configuration names "membaseNhibernate" should be used --&gt;
    &lt;membaseNhibernateCache section="membaseNhibernate"&gt;

        &lt;!-- Here expiration details are specified for the Nhibernate cache regions LongTerm and ShortTerm --&gt;
        &lt;!-- The expiration are defined in minutes, meaning LongTerm = 60 minutes and ShortTerm = 2 minutes --&gt;
        &lt;cache region="LongTerm" expiration="60" /&gt;
        &lt;cache region="ShortTerm" expiration="2" /&gt;
    &lt;/membaseNhibernateCache&gt;

&lt;/configuration&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2011/02/nhibernate-membase-caching-provider/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Must read and references</title>
		<link>http://blog.ovesens.net/2010/01/must-read-and-references/</link>
		<comments>http://blog.ovesens.net/2010/01/must-read-and-references/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 13:56:11 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[read]]></category>
		<category><![CDATA[reference]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/2010/01/must-read-and-references/</guid>
		<description><![CDATA[Read: http://davybrion.com/blog/2009/12/using-nhibernate-in-your-service-layer/ Reference: http://west-wind.com/weblog/posts/132081.aspx]]></description>
			<content:encoded><![CDATA[<p>Read:<br />
<a href="http://davybrion.com/blog/2009/12/using-nhibernate-in-your-service-layer/" target="_blank">http://davybrion.com/blog/2009/12/using-nhibernate-in-your-service-layer/</a></p>
<p>Reference:<br />
<a href="http://west-wind.com/weblog/posts/132081.aspx" target="_blank">http://west-wind.com/weblog/posts/132081.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2010/01/must-read-and-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NHibernate Fluent Criteria</title>
		<link>http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/</link>
		<comments>http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 09:08:10 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[fluent]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/</guid>
		<description><![CDATA[&#8211; Updated on the 2. January 2010 I have been using NHibernate for some time now, and I am very satisfied with the functionality it provides. However, I do really not like &#8220;magic strings&#8221; as these makes the code resistent &#8230; <a href="http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>&#8211; Updated on the 2. January 2010</em></p>
<p>I have been using NHibernate for some time now, and I am very satisfied with the functionality it provides. However, I do really not like &#8220;magic strings&#8221; as these makes the code resistent to refactoring.</p>
<p>NHibernate makes use of &#8220;magic strings&#8221; when doing a query using HQL or the Criteria API. I tend to use the Criteria API as this, opposed to HQL, limits the logic encompassed in &#8220;magic strings&#8221;.</p>
<p>As I mostly use the Criteria API, I thought, why not do something about it <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Â So I did. Very quickly a Fluent Criteria interface was implemented. Here is how it is used:</p>
<pre class="brush:csharp">var email = "email@email.com";
var customerType = CustomerType.Default;

var result = nhSession.Criteria&lt;Customer&gt;()
             .Add.Equals(x =&gt; x.Email, email)
             .Add.Equals(x =&gt; x.Type, customerType)
             .Order.Desc(x =&gt; x.Created)
             .NHCriteria
                 .SetMaxResults(1)
                 .UniqueResult&lt;Customer&gt;();</pre>
<p>In this line we call the extension method Criteria with Customer as a generic argument:</p>
<pre class="brush:csharp">var result = nhSession.Criteria&lt;Customer&gt;()</pre>
<p>By doing so we of the alternate Fluent Criteria API I have implemented. Currently it is only some of the Restrictions and Order classes that have been implemented, but you are free to use these classes and extend them as you wish.</p>
<p>By calling the NHCriteria property, you get the normal NHibernate criteria API, so here you can set the cache options, max results, etc.</p>
<p>Download the source code here <a href="http://ovesen.codeplex.com/wikipage?title=NHibernateFluentCriteria&amp;referringTitle=Home">http://ovesen.codeplex.com/wikipage?title=NHibernateFluentCriteria&amp;referringTitle=Home</a></p>
<p>Happy New Year btw <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Multiple Configuration files for NHibernate (HybridSessionBuilder)</title>
		<link>http://blog.ovesens.net/2009/01/using-multiple-configuration-files-for-nhibernate-hybridsessionbuilder/</link>
		<comments>http://blog.ovesens.net/2009/01/using-multiple-configuration-files-for-nhibernate-hybridsessionbuilder/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 23:31:55 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">/post/2009/01/25/Using-Multiple-Configuration-files-for-NHibernate-(HybridSessionBuilder).aspx</guid>
		<description><![CDATA[&#160; 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 &#8230; <a href="http://blog.ovesens.net/2009/01/using-multiple-configuration-files-for-nhibernate-hybridsessionbuilder/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>&#160;</h2>
<p>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.</p>
<p>This post is also written because of a simple question: <em>Does the HybridSessionBuilder support multiple configurations</em>?</p>
<p>Why this question? Well, I have a project where some repositories uses one database and some other uses another database.</p>
<h2>Original implementation</h2>
<p>My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:</p>
<p><a title="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/" href="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/">http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/</a></p>
<p>But as you can see:<a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="283" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb.png" width="544" border="0" /></a></p>
<p>The original implementation used a static session factory and session, so this is does not support multiple configurations.</p>
<h2>CodeCampServer/Tarantino</h2>
<p>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.</p>
<p>But by viewing the source code for <a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a> I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Furthermore I found out that the HSB now is a part of a collection of libraries called <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino</a>.</p>
<p>I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.</p>
<h2>Test web application </h2>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_4.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="156" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_1.png" width="198" align="right" border="0" /></a> 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.</p>
<p>nhibernate.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true</em></p>
<p>nhibernate2.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true</em></p>
<p>My windsor.config.xml looks like this:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;</span><span style="color: #800000">component</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;firstRepository&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span>hibernate.cfg.xml<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">component</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;</span><span style="color: #800000">component</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;secondRepository&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span>hibernate2.cfg.xml<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">component</span><span style="color: #0000ff">&gt;</span></pre>
</p></div>
</div>
<p><font face="Lucida Sans Unicode"></font></p>
<p>It basically just configures the two repositories with each own configuration.</p>
<p>But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_6.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="42" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_2.png" width="534" border="0" /></a> </p>
<p>Only the default NHibernate configuration file was used.</p>
<h2>Bug or feature?</h2>
<p>That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.</p>
<p>I was expecting an object structure like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_12.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="178" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_5.png" width="534" border="0" /></a> </p>
<p>But I got something like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_10.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="185" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_4.png" width="534" border="0" /></a>&#160; I identified the following method to be the central, when the session was instantiated:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">private</span> ISession getExistingOrNewSession(ISessionFactory factory, <span style="color: #0000ff">string</span> configurationFile)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span>         {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span>             <span style="color: #0000ff">if</span> (HttpContext.Current != <span style="color: #0000ff">null</span>)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span>             {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   5:</span>                 var session = GetExistingWebSession();</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   7:</span>                 <span style="color: #0000ff">if</span> (session == <span style="color: #0000ff">null</span> || !session.IsOpen)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span>                 {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   9:</span>                     session = openSessionAndAddToContext(factory, configurationFile);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span>                 }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  11:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  12:</span>                 <span style="color: #0000ff">return</span> session;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  13:</span>             }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  14:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  15:</span>             var currentSession = _currentSessions.ContainsKey(configurationFile)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  16:</span>                                     ? _currentSessions[configurationFile]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  17:</span>                                     : <span style="color: #0000ff">null</span>;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  18:</span>             <span style="color: #0000ff">if</span> (currentSession == <span style="color: #0000ff">null</span> || !currentSession.IsOpen)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  19:</span>             {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  20:</span>                 _currentSessions[configurationFile] = OpenSession(factory);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  21:</span>             }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  22:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  23:</span>             <span style="color: #0000ff">return</span> _currentSessions[configurationFile];</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  24:</span>         }</pre>
</p></div>
</div>
<h2>Patch/solution</h2>
<p>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.</p>
<p>I changed it to:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> var session = GetExistingWebSession(configurationFile);</pre>
</p></div>
</div>
<p>&#160;</p>
<p>And tadaaa:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_14.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="42" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_6.png" width="534" border="0" /></a> </p>
<p>The second repository now uses the second configuration file <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.</p>
<p>First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.</p>
<p>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.</p>
<p>And BTW &#8211; 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.</p>
<h3>NHibernateSessionModule</h3>
<p>Please note. Before my change is used, you need to also update the NHibernateSessionModule. It does currently not dispose all the sessions that are opened.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/01/using-multiple-configuration-files-for-nhibernate-hybridsessionbuilder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple Configurations for NHibernate using HybridSessionBuilder</title>
		<link>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-using-hybridsessionbuilder/</link>
		<comments>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-using-hybridsessionbuilder/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 00:29:33 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">/post/2009/01/25/Multiple-Configurations-for-NHibernate-using-HybridSessionBuilder.aspx</guid>
		<description><![CDATA[Â  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 &#8230; <a href="http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-using-hybridsessionbuilder/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Â </h2>
<p>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.</p>
<p>This post is also written because of a simple question: <em>Does the HybridSessionBuilder support multiple configurations</em>?</p>
<p>Why this question? Well, I have a project where some repositories uses one database and some other uses another database.</p>
<h2>Original implementation</h2>
<p>My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:</p>
<p><a title="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/" href="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/">http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/</a></p>
<p>But as you can see:<a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_2.png"><img style="border-width: 0px;" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb.png" border="0" alt="image" width="544" height="283" /></a></p>
<p>The original implementation used a static session factory and session, so this is does not support multiple configurations.</p>
<h2>CodeCampServer/Tarantino</h2>
<p>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.</p>
<p>But by viewing the source code for <a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a> I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Furthermore I found out that the HSB now is a part of a collection of libraries called <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino</a>.</p>
<p>I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.</p>
<h2>Test web application</h2>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_4.png"><img style="border-width: 0px;" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_1.png" border="0" alt="image" width="198" height="156" align="right" /></a> 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.</p>
<p>nhibernate.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true</em></p>
<p>nhibernate2.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true</em></p>
<p>My windsor.config.xml looks like this:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">component</span> <span style="color: #ff0000;">id</span><span style="color: #0000ff;">="firstRepository"</span> <span style="color: #ff0000;">type</span><span style="color: #0000ff;">="HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest"</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">parameters</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ConfigurationFile</span><span style="color: #0000ff;">&gt;</span>hibernate.cfg.xml<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ConfigurationFile</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">parameters</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">component</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">Â </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">component</span> <span style="color: #ff0000;">id</span><span style="color: #0000ff;">="secondRepository"</span> <span style="color: #ff0000;">type</span><span style="color: #0000ff;">="HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest"</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">parameters</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ConfigurationFile</span><span style="color: #0000ff;">&gt;</span>hibernate2.cfg.xml<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ConfigurationFile</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">parameters</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">component</span><span style="color: #0000ff;">&gt;</span></pre>
</div>
</div>
<p><span style="font-family: Lucida Sans Unicode;">Â </span></p>
<p>It basically just configures the two repositories with each own configuration.</p>
<p>But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_6.png"><img style="border-width: 0px;" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_2.png" border="0" alt="image" width="534" height="42" /></a></p>
<p>Only the default NHibernate configuration file was used.</p>
<h2>Bug or feature?</h2>
<p>That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.</p>
<p>I was expecting an object structure like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_12.png"><img style="border-width: 0px;" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_5.png" border="0" alt="image" width="534" height="178" /></a></p>
<p>But I got something like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_10.png"><img style="border-width: 0px;" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_4.png" border="0" alt="image" width="534" height="185" /></a>Â  I identified the following method to be the central, when the session was instantiated:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   1:</span> <span style="color: #0000ff;">private</span> ISession getExistingOrNewSession(ISessionFactory factory, <span style="color: #0000ff;">string</span> configurationFile)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   2:</span>         {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   3:</span>             <span style="color: #0000ff;">if</span> (HttpContext.Current != <span style="color: #0000ff;">null</span>)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   4:</span>             {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   5:</span>                 var session = GetExistingWebSession();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   6:</span>Â </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   7:</span>                 <span style="color: #0000ff;">if</span> (session == <span style="color: #0000ff;">null</span> || !session.IsOpen)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   8:</span>                 {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   9:</span>                     session = openSessionAndAddToContext(factory, configurationFile);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  10:</span>                 }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  11:</span>Â </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  12:</span>                 <span style="color: #0000ff;">return</span> session;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  13:</span>             }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  14:</span>Â </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  15:</span>             var currentSession = _currentSessions.ContainsKey(configurationFile)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  16:</span>                                     ? _currentSessions[configurationFile]</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  17:</span>                                     : <span style="color: #0000ff;">null</span>;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  18:</span>             <span style="color: #0000ff;">if</span> (currentSession == <span style="color: #0000ff;">null</span> || !currentSession.IsOpen)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  19:</span>             {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  20:</span>                 _currentSessions[configurationFile] = OpenSession(factory);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  21:</span>             }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  22:</span>Â </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  23:</span>             <span style="color: #0000ff;">return</span> _currentSessions[configurationFile];</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  24:</span>         }</pre>
</div>
</div>
<h2>Patch/solution</h2>
<p>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.</p>
<p>I changed it to:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   1:</span> var session = GetExistingWebSession(configurationFile);</pre>
</div>
</div>
<p>Â </p>
<p>And tadaaa:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_14.png"><img style="border-width: 0px;" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_6.png" border="0" alt="image" width="534" height="42" /></a></p>
<p>The second repository now uses the second configuration file <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.</p>
<p>First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.</p>
<p>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.</p>
<p>And BTW &#8211; 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.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-using-hybridsessionbuilder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple Configurations for NHibernate (HybridSessionBuilder)</title>
		<link>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-hybridsessionbuilder/</link>
		<comments>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-hybridsessionbuilder/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 19:31:04 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">/post/2009/01/24/Multiple-Configurations-for-NHibernate-(HybridSessionBuilder).aspx</guid>
		<description><![CDATA[&#160; 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 &#8230; <a href="http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-hybridsessionbuilder/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>&#160;</h2>
<p>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.</p>
<p>This post is also written because of a simple question: <em>Does the HybridSessionBuilder support multiple configurations</em>?</p>
<p>Why this question? Well, I have a project where some repositories uses one database and some other uses another database.</p>
<h2>Original implementation</h2>
<p>My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:</p>
<p><a title="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/" href="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/">http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/</a></p>
<p>But as you can see:<a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="283" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb.png" width="544" border="0" /></a></p>
<p>The original implementation used a static session factory and session, so this is does not support multiple configurations.</p>
<h2>CodeCampServer/Tarantino</h2>
<p>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.</p>
<p>But by viewing the source code for <a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a> I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Furthermore I found out that the HSB now is a part of a collection of libraries called <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino</a>.</p>
<p>I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.</p>
<h2>Test web application </h2>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_4.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="156" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_1.png" width="198" align="right" border="0" /></a> 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.</p>
<p>nhibernate.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true</em></p>
<p>nhibernate2.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true</em></p>
<p>My windsor.config.xml looks like this:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;</span><span style="color: #800000">component</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;firstRepository&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span>hibernate.cfg.xml<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">component</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;</span><span style="color: #800000">component</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;secondRepository&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span>hibernate2.cfg.xml<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">component</span><span style="color: #0000ff">&gt;</span></pre>
</p></div>
</div>
<p><font face="Lucida Sans Unicode"></font></p>
<p>It basically just configures the two repositories with each own configuration.</p>
<p>But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_6.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="42" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_2.png" width="534" border="0" /></a> </p>
<p>Only the default NHibernate configuration file was used.</p>
<h2>Bug or feature?</h2>
<p>That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.</p>
<p>I was expecting an object structure like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_12.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="178" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_5.png" width="534" border="0" /></a> </p>
<p>But I got something like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_10.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="185" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_4.png" width="534" border="0" /></a>&#160; I identified the following method to be the central, when the session was instantiated:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">private</span> ISession getExistingOrNewSession(ISessionFactory factory, <span style="color: #0000ff">string</span> configurationFile)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span>         {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span>             <span style="color: #0000ff">if</span> (HttpContext.Current != <span style="color: #0000ff">null</span>)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span>             {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   5:</span>                 var session = GetExistingWebSession();</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   7:</span>                 <span style="color: #0000ff">if</span> (session == <span style="color: #0000ff">null</span> || !session.IsOpen)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span>                 {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   9:</span>                     session = openSessionAndAddToContext(factory, configurationFile);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span>                 }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  11:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  12:</span>                 <span style="color: #0000ff">return</span> session;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  13:</span>             }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  14:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  15:</span>             var currentSession = _currentSessions.ContainsKey(configurationFile)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  16:</span>                                     ? _currentSessions[configurationFile]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  17:</span>                                     : <span style="color: #0000ff">null</span>;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  18:</span>             <span style="color: #0000ff">if</span> (currentSession == <span style="color: #0000ff">null</span> || !currentSession.IsOpen)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  19:</span>             {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  20:</span>                 _currentSessions[configurationFile] = OpenSession(factory);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  21:</span>             }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  22:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  23:</span>             <span style="color: #0000ff">return</span> _currentSessions[configurationFile];</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  24:</span>         }</pre>
</p></div>
</div>
<h2>Patch/solution</h2>
<p>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.</p>
<p>I changed it to:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> var session = GetExistingWebSession(configurationFile);</pre>
</p></div>
</div>
<p>&#160;</p>
<p>And tadaaa:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_14.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="42" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_6.png" width="534" border="0" /></a> </p>
<p>The second repository now uses the second configuration file <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.</p>
<p>First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.</p>
<p>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.</p>
<p>And BTW &#8211; 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.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-hybridsessionbuilder/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NHibernate &#8211; many-to-many relationship with data attached</title>
		<link>http://blog.ovesens.net/2009/01/nhibernate-many-to-many-relationship-with-data-attached/</link>
		<comments>http://blog.ovesens.net/2009/01/nhibernate-many-to-many-relationship-with-data-attached/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 15:09:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">/post/2009/01/07/NHibernate-many-to-many-relationship-with-data-attached.aspx</guid>
		<description><![CDATA[I just found a good blog post about the may-to-many relationship in NHibernate. The configuration sis done in Fluent NHibernate. http://codebetter.com/blogs/kyle.baley/archive/2008/12/24/many-to-many-relationships-with-data-attached-in-nhibernate.aspx]]></description>
			<content:encoded><![CDATA[<p>
I just found a good blog post about the may-to-many relationship in NHibernate.
</p>
<p>
The configuration sis done in Fluent NHibernate.
</p>
<p>http://codebetter.com/blogs/kyle.baley/archive/2008/12/24/many-to-many-relationships-with-data-attached-in-nhibernate.aspx</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/01/nhibernate-many-to-many-relationship-with-data-attached/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

