-->
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: Proble with Commit in NHibernate
PostPosted: Tue Dec 28, 2010 3:59 pm 
Newbie

Joined: Fri Jun 11, 2010 1:48 pm
Posts: 4
Hi,

I have a problem (or maybe I'm the problem).
I've been working with NHibernate for a while now, and I learned it from the book NHibernate In Action.

I was looking for a solution to the problem of opening only one session per request, and that book gives the following solution:

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

private void Application_BeginRequest(object sender, EventArgs e)
{
ISession sesion = NHibernateHelper.AbrirSession();
sesion.BeginTransaction();
CurrentSessionContext.Bind(sesion);
}

private void Application_EndRequest(object sender, EventArgs e)
{
ISession sesion = CurrentSessionContext.Unbind(NHibernateHelper.FabricaSesion);

if(sesion != null)
try
{
sesion.Transaction.Commit();
}
catch (HibernateException ex)
{
sesion.Transaction.Rollback();
throw new Exception(Constantes.ERROR_TRANSACCION, ex);
}
finally
{
sesion.Close();
}
}

This works fine. The other day I was debugging my web application, and found out that when I refreshed de page (by clicking on a button), it entered here to end the request (Application_EndRequest), but it did it like 5 times. Is this correct?? Shouldn't do it only once, and inmediately then enter the Application_BeginRequest method and open one and only one session??

I'm thinking of this as a problem because of the following:

I have a class which contains a list of objects of the same class. I add and remove objects from that list, and then I save de object. Sometimes it works fines, but on others it triggers the following message error: unexpected row count 0 expected 1.
Especially when it happens the following: I delete a Contact from the list, save it (which does OK), and next I add a Contact to the list and save it (throws this error)

Here is the code of my Entity:
Contact
{
//........//
private Contact parentContact;
private IList<Contact> sons = new List<Contact>();

public virtual Contact ParentContact
{
get { return parentContact; }
set { parentContact = value; }
}

public virtual IList<Contact> Sons
{
get { return sons; }
}

public virtual void AddSon(Contact son)
{
son.ParentContact = this;
Sons.Add(son);
}

public virtual void DeleteSon(Contact son)
{
son.ParentContact = null;
Sons.Remove(son);
}
}

And the property mapping is the following:

<bag name="Sons" inverse="true" cascade="all-delete-orphan" lazy="true" access="nosetter.camelcase">
<key column="ParentContact" />
<one-to-many class="Contact" />
</bag>

I use the two convenience methods from above to add and delete contacts from the list. Am I doing something wrong???
If you need more info please let me know.

Thanks a lot!

Sebastian


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.