title: How JPA (Hibernate) deal with transaction when fetching Object from database
body: <p>I'm currently developping an application in java using Hibernate as a persistence manager and JPA as an abstraction of the persistence manage hibernate. </p>
  
  <p>I'd like to know the impact of wrapping a result query around a transaction. I know the entity manager must stay open for lazily fetched field bug what about transaction in all this ?</p>
  
  <p>Here is a code example with transaction activation/desactivation ability.</p>
  
  <pre><code>public List&lt;Exportdata&gt; get(Integer max, EntityManager em, Boolean withTransaction) {
      EntityTransaction tx = null;
      try {
          if (withTransaction) {
              tx = em.getTransaction();
              tx.begin();
          }
  
          Query query = em.createQuery("from Exportdata");
          query.setMaxResults(10);
          List&lt;Exportdata&gt; list = query.getResultList();
  
          if (withTransaction)
              tx.commit();
  
          return list;
      } catch (RuntimeException re) {
          if (withTransaction)
              if (tx != null &amp;&amp; tx.isActive())
                  tx.rollback();
  
          throw re;
      }
  }
  </code></pre>
  
  <p>What is the difference between enabling or disabling withTransaction when this function is called ?</p>
  
  <p>Thanks all,
  Fred</p>