title: Does Hibernate auto-rollback programmatic transactions when unhandled exception occurs?
body: <p>When working with programmatic transactions in Hibernate, is it necessary to explicitly call rollback if an exception occurs, or will the framework take care of calling rollback if there is an unhandled exception? The code below seems like the <strong>safe</strong> approach (albeit ugly code) to ensure rollback, but i'm wondering if there is a more elegant way of coding this, thoughts?</p>
  
  <pre><code>ApplicationContext ac = new ClassPathXmlApplicationContext("hibernate-config.xml");
  SessionFactory factory = (SessionFactory) ac.getBean("sessionFactory");
  Session session = factory.getCurrentSession();
  
  Transaction txn = null;
  try
  {
      txn = session.beginTransaction();
  
      // &lt;insert transaction work here&gt;
  
      txn.commit();
  }
  catch(Exception e)
  {
      try {txn.rollback(); }
      catch (Exception eAny) {  }
      throw(e);
  }
  finally { session.close(); }
  </code></pre>