> I was reading in the Hibernate forums that when you have a WebApp > DAO
> Hibernate > DB design, and you open/close each Hibernate session at
> every single DAO method call (what I do now) its actually quite
> expensive,

This is fairly expensive, but not that much. The *SessionFactory* is
expensive, a single Session isn't that bad.

> and is suggested that you attempt to maintain a Hibernate
> Session in conjunction with a user Session to increase performance.

You have definitely misread something. This is not officially
recommended anywhere.

> I had a question for anyone that has done this (without using
> SpringFramework). My gut-reaction to this was to add a
> HttpSessionListener to my webapp that created and stored a Hibernate
> session in the user's session, and then close it when the Session
> expired... will this not work? Anyone else have a good solution for
> this?

No, this is not going to work. A Session is similar in its scope to a
transaction - it is what the Hibernate developers call an "Application
Transaction", i.e. a unit of work from the application's point of
work, that may span several database transactions, but should NOT be
kept open during user think-time, i.e. across requests. The biggest
problem with this is that you would keep a db connection open with
this as well. If you use the disconnect()/reconnect() methods, you
wouldn't have the open connection, but since the Session is also a
local cache you would very quickly see stale data.

The best way to do it, IMO, is to use for example a request filter and
generate a fresh Session when the request begins, and close it at the
end of the request. That way you always have fresh data and don't
unnecessarily keep open connections.

HTH
Carl-Eric


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to