views:

151

answers:

3

Hello everyone.

Let me describe the context first:

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.

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:

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<Client> GetAll()
         {
             return CurrentSession.CreateQuery("from Client c").List<Client>();
         }
    }    
}

I have configured Nhibernate session and transaction management with Spring. Here is the xml configuration:

<!-- NHibernate Configuration -->
<object id="NHibernateSessionFactory" type="GeSuiPro.Abstract.ExtendedSessionFactoryObject, GeSuiPro.Abstract">
  ...

  <property name="HibernateProperties">
  ...
  </property>

  <!-- provides integation with Spring's declarative transaction management features -->
  <property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>

<!-- Transaction Management Strategy - local database transactions -->
<object id="transactionManager"
    type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate21">

  <property name="DbProvider" ref="DbProvider"/>
  <property name="SessionFactory" ref="NHibernateSessionFactory"/>

</object>

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

<!-- Exception translation object post processor -->
<object type="Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor, Spring.Data"/>

Now, when I try to access to the session from the C# code, everything works fine:

IList<Client> list = clientDao.GetAll();

However, some calls to the "GetAll" methods are made from the ASP code via ObjectDataSource objects:

<asp:ObjectDataSource ID="odsClient" runat="server" TypeName="MyProject.DataAccess.ClientDao"
            SelectMethod="GetAll" DataObjectTypeName="MyProject.Object.Client" />

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.

For information, I use .NET 3.5 Framework with NHibernate 2.1.2. My database is Oracle 11g.

Any help would be appreciated !

A: 

Have you tried the solution on the thread Spring / Hibernate / JUnit - No Hibernate Session bound to Thread, specifically the first answer?

Rafael Belliard
Thanks for your reply. I don't think this solution is appropriate, as I have already defined a transaction manager. I may be wrong, but it seems that this solution is to work with JUnit only. Moreover, my problem is to access the NHibernate session with a call from the ASP.NET code, as it works fine from the C# code.
Hal
A: 

Hi, just to be sure, your ClientDao class is Spring managed right? So when you use it from C# all is OK. But your ObjectDadaSource will instantiate a new ClientDao object using reflection (outside Spring) so you don't have what you want (no DI, no AOP Proxy for transactions).

You can

  • use the event ObjectCreating of the ObjectDataSource class to get the object from the ApplicationContext (more here: http://www.rain-works.com/wpblog/index.php/2009/07/how-to-use-aspnet-object-data-source-with-ioc-spring-framework/)

  • wrap the calls to the DAO object in static methods of a static class that "knows" about Spring and than use these static methods fron the ObjectDataSource (no object is created by the DataSourceObject with static methods)

  • write your own custom "Spring" ObjectDataSource control that will automatically get the dao object from the ApplicationContext. This way is very flexible and you can also manage paging, sorting in a very efficient way when linking to a gridview. You can define a lot of way to pass arguments to your dao methods for querying data (from session, spring expression, other spring services, etc.). But it's harder and requires a bit of coding first...

Hope this helps

Marcello
Weird behavior...
Hal
A: 

Ok, I think I've found what's going wrong. The fact is I have not started to code from scratch. I had an already working project, but I had to re-built data access in order to make the project work with several SGBDs. Thus, I used NHibernate. As I'm not the person who started the project, I have not a 100% knowledge of what has been done so far, so there was some piece of code that NHibernate didn't like. For example, there was a case where a object with a given Id was loaded by the NHibernate session, and somewhere else in the code, the object was re-created and then updated :

MyObject a = session.createQuery("from MyObject m where m.Id = 0");

And somewhere else in the code:

MyObject a = new MyObject();
a.Id = 0;
session.Update(a);

This is making NHibernate sick because the same session holds two entities of the same type with the same Id.

Hal