I need to establish a coding idiom for use with Hibernate. The following method has an extra level of try-catch in a finally block.
Is this required or can I simply put the session.close() in the first block? Will Hibernate automatically close a session if there is an exception thrown?
Code:
private List findDomainObjects( String query )
{
List result = java.util.Collections.EMPTY_LIST;
try
{
Session session = sessionFactory.openSession( conn );
result = session.find( query );
}
catch ( HibernateException he )
{
cat.warn( "Hibernate error during query", he );
}
catch ( Exception e )
{
cat.fatal( "Serious internal error.", e );
}
finally
{
try
{
session.close();
}
catch ( Throwable t )
{
cat.fatal( "Serious internal error", t );
}
}
return result;
}