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.  [ 4 posts ] 
Author Message
 Post subject: NHibernate code review
PostPosted: Thu Jun 29, 2006 4:03 pm 
Newbie

Joined: Wed Jun 28, 2006 4:42 pm
Posts: 2
Hi All,

I'm forwarding my first application written using NHibernate. I'll appreciate if someone can look into the code and provide me the feedback if its an optimal solution from performance point of view.

Thanks,
-newuser.

Code:
   
            int numBatches = 20;
            int customersPerBatch = 50;
            int custId = 1;

            ISessionFactory sf = new Configuration().Configure().BuildSessionFactory();
            ISession session = sf.OpenSession();

                // Inserting Customer Objects
                for (int j = 0; j < numBatches; j++)
                {
                    ITransaction transaction = session.BeginTransaction();
                        for (int i = 0; i < customersPerBatch; i++)
                        {
                            // Creating new customer objects
                            Customer cust = new Customer(custId, "user", 1000.00);

                            // Tell NHibernate that this object should be saved
                            session.Save(cust);

                            custId++;
                        }
                    transaction.Commit();
                }
                   
                // Querying customers in batch size of custoersPerQuery
                IQuery query = session.CreateQuery("from Customer c where c.custId > ? and c.custId <= ?");
                low = 0;

                for (int j = 0; j < numBatches; j++)
                {
                    high = low + customersPerBatch;

                    ITransaction transaction = session.BeginTransaction();
                     
                        query.SetInt32(0, low);
                        query.SetInt32(1, high);
                        System.Collections.IList customers = query.List(); 
                     
                    transaction.Commit();

                    low = high;
                }

            session.Close();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 4:35 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
What I'm doing in the batch update application that I have is I just wrap the entire update in a transaction and commit it all at once. I don't know that doing it this way is totally ideal, because I think it locks the database while the transaction is open.

I tried doing something similar to what you are doing awhile back. I found that if you do it that way, you need to call ISession.Clear() after you have processed a batch of objects, or, do something similar with ISession.Evict(). Otherwise, more and more objects will be in the Session cache and everytime you flush the session, which happens everytime you commit the transaction, it has to check all those objects to see what has changed (even the ones from the previous batch). The goal is to keep the number of objects in the session to a small number, otherwise, it becomes very inefficient.

Where you're doing the queries, you don't need to wrap the query in a transaction as far as I know.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 4:36 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
Also, I know in the case of Hibernate for Java, there is a property that you can set for JDBC batch update size. I don't know if NHibernate has this or not. It might not hurt to check.


Top
 Profile  
 
 Post subject: Re: NHibernate code review
PostPosted: Mon Jul 03, 2006 3:00 am 
Regular
Regular

Joined: Fri Feb 18, 2005 3:34 am
Posts: 88
Location: Poland/Wrocław
newuser wrote:
Hi All,

I'm forwarding my first application written using NHibernate. I'll appreciate if someone can look into the code and provide me the feedback if its an optimal solution from performance point of view.


First of all, you should embed transactions in the following template:

Code:
ITransaction tx = sess.BeginTransaction();
try
{
  // Doo your job here...
  tx.Commit();
}
catch(Exception)
{
  tx.Rollback();
  throw;
}


Otherwise on any NH unrecoverable error you have no possibility to handle it.

I also have following pattern for handling sessions:

Code:
using(ISession sess = factory.OpenSession())
{
  // Do your job here...
}


This assures that I close session properly in case of errors. It also closes it just after job is completed what enables reuse of connection (from the pool) and avoids creating a new one while the existing is going to be closed anyway.

I also prefer ICriteria over IQuery since, in my opinion, minimizing the use of strings (in query) leads to potential bugs minimizing (You can make typing mistake, i.e. type FRON instead of FROM, and then spend hours with debugger...). It would be perfect to getting rid of strings at all, but I did not invent anything instead of them in case of property names in the expressions being provided to the criteria objects.

Hope this can help you :)
Regards,

_________________
Please rate this post if you've found it helpfull
Roland


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.