Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about currentSession and Session-per-request in a Filter

 
Miguel Kojiio Nobre
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt in the way that Hibernate "getCurrentSession" works.

In a scenario where I have two or more requests at the same time requiring a Hibernate session and all of them will update the database.

Consider that I use the Session-Per-Request paradigm implemented via a Filter and don't use any Transaction Manager.

This is a snippet of the doFilter method of my Filter:



How could Hibernate know which request is been committed?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, each client request-response cycle runs in its own thread, so the session would be associated with that particular thread. So, the client is associated with a thread, and the session is associated with that same thread. That's how everything is kept in check.

Is that sorta what you were driving at?

-Cameron McKenzie
 
Miguel Kojiio Nobre
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, each client request-response cycle runs in its own thread, so the session would be associated with that particular thread.


Humm, so the code that I posted before is wrong, considering that I have made no association with the thread, ok?

I could correct that by declaring a local (method) variable of Session type in doFilter method, and my new code will look like this:



Now, with that variable, I have the thread association. Am I right?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I think those two lines of code are pretty much the same. Really, a compound method call has been broken down into two lines, rather than one.

In your hibernate.cfg.xml file, it probably says something about THREAD_LOCAL? That's the part that binds the session to the current thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic