• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

openSession(conn) thread safety

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic