These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: NHibernate, Stored Procedures and Transactions
PostPosted: Thu Aug 28, 2008 10:54 am 
Beginner
Beginner

Joined: Tue May 30, 2006 10:55 am
Posts: 21
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.