Recently I've been checking my project code and find out that there are places where exception handling is done wrong. In some places I can see the following code:
Code:
ITransaction transaction = session.BeginTransaction();
try
{
if (objectToPersist == null)
{
throw new ArgumentNullException();
}
transaction.Commit();
}
catch (NHibernateException e)
{
transaction.Rollback();
}
It is not exactly like that, because beginning and ending transaction are actually done in the web layer, but in general it looks like that.
So, my warning is, what will happen if the method throws ArgumentNullException? Transaction is already started, but won't be rolled back, since expected exception type is NHibernateException. Will transaction still try to flush data? Will unclosed transaction cause any other side effects? Is there are way to control the behaviour of uncommitted transactions? Of course, I want all uncommitted transactions to be rolled back.
P.S. I know that this code is generally wrong, but it will take time to fix it and I want to be sure that it is still okay to have code like that.