-->
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.  [ 2 posts ] 
Author Message
 Post subject: Transaction not rolling back with session/request
PostPosted: Thu Jun 16, 2005 7:54 pm 
Newbie

Joined: Thu Jun 16, 2005 5:53 pm
Posts: 9
Hello,

I have implemented session/request with an http handler inside my DAL as follows...

Code:
public class SessionHandler : IHttpModule
{
   private static readonly string KEY = "NHibernateSession";

   public void Init(HttpApplication context)
   {
      context.EndRequest += new EventHandler(context_EndRequest);
   }

   public void Dispose()
   {
   }

   private void context_EndRequest(object sender, EventArgs e)
   {
      HttpApplication application = (HttpApplication)sender;
      HttpContext context = application.Context;

      ISession session = context.Items[KEY] as ISession;
      if (session != null)
      {
         try
         {
            session.Flush();
            session.Close();
         }
         catch (Exception ex)
         {
            // log error.
            throw ex;
         }
      }

      context.Items[KEY] = null;
   }

   public static ISession CurrentSession
   {
      get
      {
         HttpContext currentContext = HttpContext.Current;
         ISession session = currentContext.Items[KEY] as ISession;

         if (session == null)
         {
            session = NHibernateUtil.GetSession();
            currentContext.Items[KEY] = session;
         }

         return session;
      }
   }
}



In my DAL I have code like the following...

Code:
public virtual void Save(object obj)
{
   ITransaction tx = null;
   try
   {
      tx = session.BeginTransaction();
      session.Save(obj);
      tx.Commit();
   }
   catch (Exception ex)
   {
      tx.Rollback();
      throw ex;
   }
}


The problem I have is that my DAL class rolls back the transaction by calling tx.Rollback() (which works fine) and then the http handler calls session.Flush() and session.Close() which seems to put the data back into the database.

I am guessing that session.Flush() is doing this? Has anyone else had this problem or know what I am doing wrong here?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 17, 2005 5:52 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
If you use transactions, you don't need to call Flush. It's called automatically on Commit (unless disabled). So just remove the call and it should work.


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

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.