title: ASP.NET's ObjectDataSource and NHibernate Session Management
body: <p>Let me describe the context first:</p>
  
  <p>In a .NET C# project, I use NHibernate to make the link between the C# objects and the database model. I have mapped my objects with NHibernate Mapping Attibutes.</p>
  
  <p>I have written data access queries in HQL, all of them being isolated in individual methods, which are decorated with attributes for transaction management. Here's how my data access classes look like:</p>
  
  <pre><code>namespace MyProject.DataAccess
  {
      public class ClientDao
      {
           private ISessionFactory sessionFactory;
           public ISessionFactory SessionFactory
           {
               protected get { return sessionFactory; }
               set { sessionFactory = value; }
           }
  
           protected ISession CurrentSession
           {
               get { return sessionFactory.GetCurrentSession(); }
           }
  
           [Transaction(TransactionPropagation.Required, IsolationLevel.ReadCommitted)]
           public IList&lt;Client&gt; GetAll()
           {
               return CurrentSession.CreateQuery("from Client c").List&lt;Client&gt;();
           }
      }    
  }
  </code></pre>
  
  <p>I have configured Nhibernate session and transaction management with Spring. Here is the xml configuration:</p>
  
  <pre><code>&lt;!-- NHibernate Configuration --&gt;
  &lt;object id="NHibernateSessionFactory" type="GeSuiPro.Abstract.ExtendedSessionFactoryObject, GeSuiPro.Abstract"&gt;
    ...
  
    &lt;property name="HibernateProperties"&gt;
    ...
    &lt;/property&gt;
  
    &lt;!-- provides integation with Spring's declarative transaction management features --&gt;
    &lt;property name="ExposeTransactionAwareSessionFactory" value="true" /&gt;
  &lt;/object&gt;
  
  &lt;!-- Transaction Management Strategy - local database transactions --&gt;
  &lt;object id="transactionManager"
      type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate21"&gt;
  
    &lt;property name="DbProvider" ref="DbProvider"/&gt;
    &lt;property name="SessionFactory" ref="NHibernateSessionFactory"/&gt;
  
  &lt;/object&gt;
  
  &lt;tx:attribute-driven transaction-manager="transactionManager"/&gt;
  
  &lt;!-- Exception translation object post processor --&gt;
  &lt;object type="Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor, Spring.Data"/&gt;
  </code></pre>
  
  <p>Now, when I try to access to the session from the C# code, everything works fine:</p>
  
  <pre><code>IList&lt;Client&gt; list = clientDao.GetAll();
  </code></pre>
  
  <p>However, some calls to the "GetAll" methods are made from the ASP code via ObjectDataSource objects:</p>
  
  <pre><code>&lt;asp:ObjectDataSource ID="odsClient" runat="server" TypeName="MyProject.DataAccess.ClientDao"
              SelectMethod="GetAll" DataObjectTypeName="MyProject.Object.Client" /&gt;
  </code></pre>
  
  <p>When accessing to the "CurrentSession" object in my GetAll method, I get the following error : 
  "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here".
  It seems that something is missing from my configuration.</p>
  
  <p>For information, I use .NET 3.5 Framework with NHibernate 2.1.2. My database is Oracle 11g.</p>
  
  <p>Any help would be appreciated !</p>