Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring.net with NHibernate and the "No Hibernate Session bound to thread error"

I am attempting to use spring.net and nihibernate for my data layer.

I have a simple DAO object that includes the following code:

[Transaction]
public long Save(Request entity)
{
   return (long)CurrentSession.Save(entity);    
}

Whenever this code is called I get the following error:

"No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here"

My DAO layer has the following configuration which is referenced in my web.config:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
         xmlns:tx="http://www.springframework.net/tx"
         xmlns:db="http://www.springframework.net/database"
         xmlns:aop="http://www.springframework.net/aop"
         >

  <!-- Referenced by main application context configuration file -->
  <description>
    The Northwind object definitions for the Data Access Objects.
  </description>

  <!-- Property placeholder configurer for database settings -->
  <object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
    <property name="ConfigSections" value="databaseSettings"/>
  </object>

  <!-- Database and NHibernate Configuration -->
  <db:provider id="DbProvider"
                   provider="SqlServer-2.0"
                   connectionString="Data Source=ME-LT;Initial Catalog=SupplyAndDemand;Integrated Security=True"/>

  <object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate21">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="MappingAssemblies">
      <list>
        <value>SAD.Providers.NHibernate</value>
      </list>
    </property>
    <property name="HibernateProperties">
      <dictionary>

        <entry key="connection.provider"
               value="NHibernate.Connection.DriverConnectionProvider"/>

        <entry key="dialect"
               value="NHibernate.Dialect.MsSql2005Dialect"/>

        <entry key="connection.driver_class"
               value="NHibernate.Driver.SqlClientDriver"/>

      </dictionary>
    </property>

    <property name="ExposeTransactionAwareSessionFactory" value="true" />
  </object>


  <object id="transactionManager"
        type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate21">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="SessionFactory" ref="NHibernateSessionFactory"/>

  </object>

  <!-- Data Access Objects -->
  <object id="RequestDao" type="SAD.Providers.Nhibernate.NHibernateRequestDao, SAD.Providers.Nhibernate">
    <property name="SessionFactory" ref="NHibernateSessionFactory" />
  </object>


<tx:attribute-driven transaction-manager="transactionManager"/>

</objects>

In my web.config I have included the reference to the parser:

<parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data"/>

and referenced my dao.xml as an assembly resource.

This DOA is injected into another spring configured object from where Save is called.

Any ideas what I'm doing wrong? As you can see I am including the config

<tx:attribute-driven transaction-manager="transactionManager"/> 

I attached to the spring code and place a break point in the invoke method of the TransactionInterceptor - it never gets called - so perhaps a proxy isnt being created for my dao objects?

Here is the full stack trace:

<StackTrace><![CDATA[at Spring.Data.NHibernate.SpringSessionContext.CurrentSession() in F:\Spring.NET\Spring.NET\src\Spring\Spring.Data.NHibernate12\Data\NHibernate\SpringSessionContext.cs:line 70
   at NHibernate.Impl.SessionFactoryImpl.GetCurrentSession()
   at sad.Providers.Nhibernate.NHibernateDao.get_CurrentSession() in F:\PersonnalProjects\sad\trunk\src\sad\sad.Providers.Nhibernate\nHibernateDao.cs:line 29
   at sad.Providers.Nhibernate.NHibernateRequestDao.Save(Request entity) in F:\PersonnalProjects\sad\trunk\src\sad\sad.Providers.Nhibernate\NHibernateRequestDao.cs:line 41
   at sad.Messaging.RequestManager.RequestManager.ProcessRequest(UserCredentials userCredentials, Request request) in F:\PersonnalProjects\sad\trunk\src\sad\sad.Messaging.RequestManager\RequestManager.cs:line 39
   at sad.Messaging.UI.Web.RequestManagerService.ProcessRequest(UserCredentials userCredentials, Request request) in F:\PersonnalProjects\sad\trunk\src\sad\sad.Messaging.UI.Web\RequestManagerService.cs:line 28
   at requestManagerService.ProcessRequest(UserCredentials userCredentials, Request request)
   at SyncInvokeProcessRequest(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
like image 713
iasksillyquestions Avatar asked Nov 05 '22 16:11

iasksillyquestions


1 Answers

I know this is an old question, but have you got the Open Session In View module defined in your web.config file?

For IIS6, it should be something like:

<httpModules>
    <!-- Other modules here -->
    <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate21"/>
</httpModules>

Also, I may be wrong about this, but I think Open Session In View looks for a session factory named (you've guessed it) SessionFactory, & so you may also need to add this to web.config as well:

<appSettings>
    <!-- Other App Settings -->
    <add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="NHibernateSessionFactory"/>
</appSettings>
like image 193
Simon Rice Avatar answered Nov 14 '22 22:11

Simon Rice