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 !
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 :
And somewhere else in the code:
This is making NHibernate sick because the same session holds two entities of the same type with the same Id.