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,