title: Hibernate ThreadLocal Session management compatible with ForkJoinPool?
body: <p>I usually use the Hibernate ThreadLocal session management pattern in Java web projects:</p>
  
  <blockquote>
    <p>The Thread Local Session pattern makes use of the
    java.lang.ThreadLocal class to create a Session that is accessible
    from a single application thread. This is particularly convenient in
    multithreaded applications, such as web applications.</p>
  </blockquote>
  
  <p>In projects I implement this with</p>
  
  <pre><code>&lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  </code></pre>
  
  <p>in the <code>hibernate.xml</code> and using <code>SessionFactory.getCurrentSession()</code> to get a session whenever I need one.</p>
  
  <p>Now I have a program that is <strong>not</strong> a Servlet, but does heavy parallel computing and database interaction. </p>
  
  <p>I want to implement this with a ForkJoinPool. Now I wonder whether it is a mistake to use Hibernate ThreadLocal session management in this scenario. As far as I understand, a ForkJoinPool uses a smaller number of threads and shares them among running tasks while other tasks are sleeping. (Motivated by stalling/annoying " task in transaction' connections,) I want to close() every Hibernate Session after a unit of work.</p>
  
  <p>So.. when I call HibernateSessionFactory.getThreadLocalSession().close() at the end of my Task - and the Task is run in a ForkJoinPool - will troubles arise? Should I drop the ThreadLocal pattern for heavy parallel calculations and manage the Sessions myself?</p>
  
  <p>Thanks in advance for any answers.</p>