Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7811357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:07:59+00:00 2026-06-02T04:07:59+00:00

I usually use the Hibernate ThreadLocal session management pattern in Java web projects: The

  • 0

I usually use the Hibernate ThreadLocal session management pattern in Java web projects:

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.

In projects I implement this with

<property name="current_session_context_class">thread</property>

in the hibernate.xml and using SessionFactory.getCurrentSession() to get a session whenever I need one.

Now I have a program that is not a Servlet, but does heavy parallel computing and database interaction.

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.

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?

Thanks in advance for any answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:08:02+00:00Added an answer on June 2, 2026 at 4:08 am

    Using ThreadLocalSessionContext could be problematic for you, but it depends on what your tasks are doing.

    A ForkJoinPool (javadocs) is meant for use in cases where tasks spawn other tasks (the fork), and wait for them to complete (the join). While waiting, the Thread that was executing the parent task may be re-used to execute a child task. According to the javadocs for ThreadLocalSessionContext, the Session gets closed when you commit a transaction you get from it (ie there’s only ever one transaction per Session).

    So, if you have a ‘parent’ task that calls sessionFactory.getCurrentSession(), and does some stuff, then calls commit(), the Session gets closed and there’s no danger of inappropriate interaction.

    However, if you spawn children tasks AFTER calling .getCurrentSession() and BEFORE calling .commit(), you may run in to issues because other tasks may execute on this Thread, and .getCurrentSession() would return the session used by the parent task. This is almost certainly not what you want, because the children tasks should presumably be doing the same thing as each other, and you wouldn’t want one arbitrarily sharing Session state with the parent while others don’t.

    So in summary, you should:

    • Not call session.close() if you get the Session from .getCurrentSession(), because it’s the responsibility of the CurrentSessionContext to deal with that.
    • Be in a committed relationship before having children…. I mean, call .commit() before spawning children tasks.

    As a footnote, I found this wiki page to be a helpful read on the subject as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I usually use web applications in Visual Studio and the MSBUILD taks in Nant
Usually I'm a Hibernate user and for my new project we use JPA 2.0.
I usually use a Page -> Service -> Repository pattern in my applications putting
I usually use database first appraoch to develop my asp.net mvc web application and
I usually use the UUID class to generate unique IDs. This works fine if
I've been learning how to use the Spring framework for hibernate transaction management, and
I usually use this: <html lang=en> . However, I am working on a website
I usually use C++ stdlib map whenever I need to store some data associated
I usually use IIS in conjunction with the OutSystems development platform, which does code-generation
I usually use Visual Studio Team System 2008 Source Control Explorer with TFS, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.