I have a problem with transactions and calls of stored procedures. Normally I use this code to save an object to a database and all works fine.
Code:
public virtual T Save(T entity)
{
ISession session = null;
ITransaction transaction = null;
try
{
transaction = this.BeginTransaction();
entity = GenericNHibernateDAO<T>.SaveOrUpdate(session, entity);
this.CommitTransaction(transaction);
}
catch (Exception ex)
{
this.RollbackTransaction(transaction);
throw;
}
finally
{
this.ReleaseSession(session);
}
return entity;
}
Now I have to call stored procedures for insert data. I have changed my mapping file but every time I try to save my object I get an error "Not in transaction" after calling the Commit. It works fine without stored procedure calls. And if I change the code in the following way all works.
Code:
public virtual T Save(T entity)
{
ISession session = null;
ITransaction transaction = null;
try
{
session = this.FetchSession();
entity = GenericNHibernateDAO<T>.SaveOrUpdate(session, entity);
session.Flush();
finally
{
this.ReleaseSession(session);
}
return entity;
}
Well, the problem is I have implemented a generic DAO pattern so for all entities should be use the same method independent whether they use stored procedures or native sql commands.
Is the "Not in transaction" a known problem on the usage of stored procedures? And if so, how can I solve this problem.