Results 1 to 11 of 11
  1. #1
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default session.close() doesnt end database session.

    Hi,

    I have created a Dashboard in my application. This is suppose to query database at regular intervals and populate data in the grids in the dashboard.

    My application is using session factory. That is one session per node.

    Now when i created initially created the dashboard, I was using the same session to fetch data from the database. But it was later identified that the data being shown is stale. It was fetching data from the hibernate cache. Only new rows were properly fetched and deleted rows were eliminated but the same primary key rows details were coming from cache resulting in stale data when such rows are updated from other nodes or from back-end.

    After doing some research, i found that to fetch fresh data always i will have to use new sessions. I did that using openSession() and fresh data is being fetched now. But there is only one problem, when this session is opened, it creates a new database session which is not closed after session.close() as well. After session.close(), if i check session.isConnected(), it returns false. But the database session still exist.

    This is pretty urgent, i have already missed my deadline because of this.

    Thanking in advance,
    Ali.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default Re: session.close() doesnt end database session.

    This is why it is recommended that you have a single Hibernate server sitting in front of the database.
    All your nodes then interact with that server.
    This is because Hibernate caches...that's part of its job.

    Anyway, for the session, is there a connection pool of any sort at play here?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default Re: session.close() doesnt end database session.

    thanx for the prompt reply.
    yes, theres a connection pool.

  4. #4
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default Re: session.close() doesnt end database session.

    and i didnt get what do you mean by single hibernate server sitting in front of the db.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default Re: session.close() doesnt end database session.

    Hibernate likes to control everything happening in the database, largely because of the cacheing.
    That tends to mean a single Hibernate instance.
    Which (to take yor basic web-app) means a single instance of an application, with Hibernate running in it.

    As for the connection pool, all the close() does on 'Session' is return the connection back to the pool.
    It does not close the connection.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default Re: session.close() doesnt end database session.

    hhmm... got it.
    But then whats the workaround for my problem. Should i create a new session-factory before firing my selects and use sessions from the newly created session-factory so that after my work is complete i can close the session factory and thereby release my db sessions.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default Re: session.close() doesnt end database session.

    Just ensure you close the session.
    I was thinking about this, and that's really the only thing you need to do.
    Since that fixes the problem, then you can safely ignore my wittering about Hibernate cacheing, since that's clearly not a problem. My blurb about that above may well be out of date...:)

    You have a connection pool, the whole point of which is to maintain a certain amount of open connections to the database. Depending on the properties of your pool it may hold onto them for x amount of time before finally closing them off. But that's where you need to look if you want to manage the connections properly.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default Re: session.close() doesnt end database session.

    Thanx for the reply.

    I have thought of a work around for my requirement. I open a new Session Factory with one connection, open a new session from that Session Factory, execute my select, fetch my data and then close the session and also the SF in finally.
    Multiple DB sessions issues solved by this but now my tomcat server is running out of memory raising java.lang.OutOfMemoryError: PermGen space
    How do i go about handling this. I tried with increasing -XX:MaxPermSize=128m , the error got delayed but eventually it does come.
    The error is because of many Session Factory object getting created, but i also close the same as soon as my data is fetched. I am not able to understand how to debug this.

    Ali

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default Re: session.close() doesnt end database session.

    I still don't understand your DB session "problem".
    That's the whole point of a connection pool, to maintain a pool of connections.

    Adjust the pool, if it doesn't meet your needs, but don't try and hack around it...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default Re: session.close() doesnt end database session.

    ok i will try to explain my DB session issue.

    I have a jsp page with multiple apps(this is also user defined which user to be shown which apps thru the rights module) fetching data at different intervals(intervals are user defined for each app). Each app has its own DAO method to fetch data using HQL. These methods are called respectively when their associated app is refreshed. Things were fine till the time we realized the data is stale as it is being fetched from the cache and data changed from backend does not update the Hibernate cache.

    Then on searching we found that if we open a new session and fire HQL in the new session then we can get fresh data as there will be no cached data in that new session. We tried that and we started getting fresh data. But this lead to a new problem that every session we open, it creates a new session in Database. So every app on my page creates a new session in the Database and when these apps are refreshed, more new DB sessions gets generated.
    So after a point of time, there are so many DB sessions, that the Database slows down and raises various other issues.

    So my requirement is to create a new session fetch the data and close the session. When i created the sessions through session factory, i was able to achieve this as it closes all the database connections as soon as you close the session factory. But for doing this, i create session factory for every app and for every users and for every refresh of individual app which eventually raises PermGen Space error.

    Hope I have made it clear now. Need help urgently

    Ali

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default Re: session.close() doesnt end database session.

    No.
    This is what I mean by "sort out your connection pool".
    This has nothing to do with the Hibernate sessions, and everything to do with the settings you have given to the connection pool.
    Sort them out (ie max connections, min connections, connection timeouts) and that will sort out your problem. At the moment it sounds like your pool has a long timeout mixed with no max connections, or a max that exceeds your database limit.

    As you have seen, creating new SessionFactories is simply wrong, as they regenerate all the classes Hibernate uses to manage the ORM side of things.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. javax.mail.Session cannot be cast to javax.mail.Session
    By JustinCase in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 03-26-2012, 05:43 PM
  2. listen session is close in spring mvc
    By javastuden in forum Advanced Java
    Replies: 0
    Last Post: 11-02-2011, 12:04 PM
  3. Replies: 1
    Last Post: 04-21-2009, 11:20 PM
  4. Replies: 1
    Last Post: 03-10-2009, 01:12 PM
  5. Replies: 2
    Last Post: 12-23-2008, 06:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •