• Post Reply Bookmark Topic Watch Topic
  • New Topic

openSession(conn) thread safety

 
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
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!