This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

openSession(conn) thread safety  RSS feed

 
Nadine McKenzie
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One tutorial I came across said "A SessionFactory is threadsafe, many threads can access it concurrently and request Sessions. A Session is a non-threadsafe object that represents a single unit-of-work with the database."
And it goes on to explain how to correct the problem using the ThreadLocal class.

The issue is I'm having is I'm not using
s = sessionFactory.openSession();

Instead, I'm using the openSession that takes a Connection.
s = sessionFactory.openSession(conn);

Since javax.sql.connection is open and closed after every unit of work, I'm not sure how to make session threadsafe.

Any thoughts?
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, why would you need a Session object to be threadsafe. It should be created for one client for one request. So there is no concurrency issues with a Session, unless you are trying to store and save that Session in some instance variabel. In which, I would say don't save the Session.

The ThreadLocal is a solution for being able to have a Session still open when you are in the render phase of a JSP page.

So there is no problem with Session not being threadsafe. Nothing that you need to solve.

Mark
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!