4

I use Spring 3 and Hibernate 3.6 for developing a webapplication - Im new and Im wondering if I really understand how sessions are working.

Is it correct, that the Sessions between Server and Client, identified by a session id, are different from hibernate sessions?

The session between Server and Client is always a HttpSession.(?) When is it created? When a User logs in or also when an anonymous user requests a page (which is not secured)?

is there any connection between httpsession and hibernate-sessions? Are Hibernate Sessions created by a sessionfactory with no connection to a httpsession? Im not sure to which session hibernate is refering with a command like this:

this.sessionFactory.getCurrentSession().save(object);

this getCurrentSession(): for how long is this hibernate session active? for the whole time a user is logged in? or for just one transaction (which can include multiple data-operations?)

Im sorry for this question which is maybe totally easy to answer, but most documentations are in english and if this is not ones mother tongue understanding is sometimes difficult (mainly because the word "session" is used so often)

thanks for helping me to understand this topic! :-)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
nano7
  • 2,455
  • 7
  • 35
  • 52

1 Answers1

6

Is it correct, that the Sessions between Server and Client, identified by a session id, are different from hibernate sessions?

Yes, completely different.

Reference: (javax.servlet) HttpSession, (Hibernate) Session

The session between Server and Client is always a HttpSession.(?) When is it created? When a User logs in or also when an anonymous user requests a page (which is not secured)?

See Java EE Tutorial > Maintaining Client State

is there any connection between httpsession and hibernate-sessions?

No, although an OpenSessionInViewFilter can make sure there is a Hibernate Session available for every HTTP Request (One Hibernate Session per Request, not per Web Session).

Are Hibernate Sessions created by a sessionfactory with no connection to a httpsession?

Yes, usually.

Im not sure to which session hibernate is refering with a command like this: "this.sessionFactory.getCurrentSession().save(object);"

Hibernate Session

this "getCurrentSession()": for how long is this hibernate session active? for the whole time a user is logged in? or for just one transaction (which can include multiple data-operations?)

See Hibernate Reference > Session and Transaction Scopes

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • thanks for your quick answer. reading through the sources you gave me, I got some more questions: 1. when HttpSession is an interface, how can an "object of HttpSession" than represent a session? Mustn't there be a concrete class which implements HttpSession? – nano7 Mar 21 '11 at 12:48
  • 2. I might be stupid, but did I understand correctly, that a session is created when a user connects for the first time with the server (without having a session id). he must not be logged in for it. the sessions ends when it times out (for example after 30min) or when the user (if he was logged in) logs out – nano7 Mar 21 '11 at 12:48
  • 3. normally, when nothing specific is configured, one hibernate session is created for one requests. is a hibernate session also automatically created for a requests when not database operation is done? thank you :-) – nano7 Mar 21 '11 at 12:49
  • 1) There are concrete classes, but you're not supposed to use them. It's up to your Servlet Container to provide the specific implementation class. If you code against that concrete class, you break compatibility with other app servers. 2) depends on app server configuration 3) is only correct if using OpenSessionInViewFilter. Usually, Hibernate based web apps need an open Hibernate Session for every single request – Sean Patrick Floyd Mar 21 '11 at 12:55
  • thanks! 2) where can I see how it is configured? I use tomcat 6 and I did not make any special configurations.. 3) maybe we are at cross purposes or I dont understand: I thought, without OpenSessionInViewFilter and without any specific configuration, usually One Session Per Request is used, right? -> is there an open source tool where I can monitor hibernate sessions with? (maybe bound to httpsessions?) – nano7 Mar 21 '11 at 14:14
  • 2) I'm not an expert at that, ask a separate question or read the docs 3) no, without OSIVF, hibernate sessions and web requests / sessions don't have anything to do with each other – Sean Patrick Floyd Mar 21 '11 at 14:20