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: Delete and Save in the same transaction when using
PostPosted: Sat Jun 23, 2007 1:05 am 
Newbie

Joined: Tue Mar 13, 2007 9:00 pm
Posts: 5
Hi,

I've got a question relating to NHibernate's 1.2.0GA transaction handling.

I've encountered the following issue:
I have a table and an object that that is mapped on to it using NHibernate. The promary key for the table is generated using increment generator.

<id name="Id" type="Int64" column="Id">
<generator class="increment" />
</id>

Now there is an ocasional time when i need to delete all of the rows from the table and load a new set of values into it. I need this to be done as part of a single transation - so incase something goes wrong the original values are retained.

First i loop though all of the current object in the table deleting them. Then i call a Flush() method on the session object. Then when it comes to the code that saves the rist new object to the database - a timeout exception occurs. The exception it self comes from the method that attempts to generate the new Id for the object.

Anyhow, code is below:
public static void BatchLoad(IList<string[]> values, Lookup look)
{
ISession session = StaticReference.SessionFactory.OpenSession();
ITransaction tran = null;
try
{
tran = session.BeginTransaction();
IList<LookupValue> valuesToDelete = GetLookupValues(look);
foreach (LookupValue val in valuesToDelete)
{
session.Delete(val);
}
session.Flush();

foreach (string[] arr in values)
{
LookupValue newLookup = new LookupValue();
newLookup.DateActiveFrom = Convert.ToDateTime("1/1/2007");
newLookup.DateActiveTo = Convert.ToDateTime("1/1/2100");
newLookup.FromValue = arr[0];
newLookup.IsActive = true;
newLookup.Lookup = look;
newLookup.ToValue = arr[1];

session.Save(newLookup); // Timeout error occurs on this line.
session.Flush();
}

tran.Commit();
}
catch (System.Exception ex)
{
if (tran != null) tran.Rollback();
throw;
}
finally
{
session.Flush();
session.Close();
}
}

As always, any help will be greatly appreciated.

Thanks in advance,
Nick Goloborodko


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 23, 2007 9:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
The ID generator "increment" does a SELECT MAX(ID) on your table. When you do the deletes, part of your table is locked in the transaction. The ID generator is then waiting for the transaction to complete, thus cause the timeout.

You may be able to work around it if you can somehow do the insert before the deletes.

_________________
Karl Chu


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.