-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: hibernate connection pool release
PostPosted: Wed Mar 03, 2010 6:44 am 
Newbie

Joined: Wed Mar 03, 2010 6:27 am
Posts: 11
Hi everyone,
I am making a web application using hibernate with jsp servlet at tomcat.
My problem is when I refresh a page for example 10-15 times, page crashes.
I use a classic I Hibrnate util in another class as;
Code:
sessionFactory = new Configuration().configure().buildSessionFactory();

then I am making a select query to view results in a page as
Code:
Session session = HibernateUtil.getSessionFactory().openSession();
       
        try {
            Query getMessages = session.createQuery("from Message order by id desc");
            getMessages.setMaxResults(5);
            this.messageList = getMessages.list();
        } finally {
//            session.close();
        }

and my hibernateconfig file

Code:
<session-factory name="sessionOne">
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="hibernate.connection.username">user</property>
    <property name="hibernate.connection.password">pass</property>
    <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">10</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">300</property>
    <!-- Disable second-level cache. -->
    <property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="hibernate.cache.use_query_cache">false</property>
    <property name="hibernate.cache.use_minimal_puts">false</property>
    <property name="hibernate.max_fetch_depth">3</property>
    <!-- Print SQL to stdout. -->
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>
    <!-- Bind the getCurrentSession() method to the thread. -->
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.hbm2ddl.auto">update</property>

    <mapping resource="model/Message.hbm.xml"/>
  </session-factory>

In here I cannot close connection, it doesnt work...
How can I release connection in my application to return it to pool?


Top
 Profile  
 
 Post subject: Re: hibernate connection pool release
PostPosted: Wed Mar 03, 2010 7:06 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
In here I cannot close connection, it doesnt work...


What do you mean with : it doesnt work !????

Does it throw an exception?
Does it block the application?
Does it nothing ( connection still is resulting on the database) ?
Does the JVM crash?

All what you say is that 'the page crashes'

Please be more clear in the way of posing the problem


Top
 Profile  
 
 Post subject: Re: hibernate connection pool release
PostPosted: Wed Mar 03, 2010 7:22 am 
Newbie

Joined: Wed Mar 03, 2010 6:27 am
Posts: 11
I mean,

When I push refreh button, it show loading(refreshing page) but it doesnt.
There is no exception, I am waiting... and it doesnt refrefh page, it seems so many people visiting the site and not responding...
For example when we enter a site which work too slow and takes along time to refreesh page. It is the same.
And in my code posted, When I activate session.close(), Application completely doesnt work.


Top
 Profile  
 
 Post subject: Re: hibernate connection pool release
PostPosted: Wed Mar 03, 2010 10:29 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I suggest you to take a look what your application server is doing.
Maybe you can check for errors in the log-files.
Otherwise is your app-server is stucking perform some
Code:
jstack <app-server-pid>

and analyze where the worker-threads are stucking.


Top
 Profile  
 
 Post subject: Re: hibernate connection pool release
PostPosted: Wed Mar 03, 2010 11:12 am 
Newbie

Joined: Wed Mar 03, 2010 6:27 am
Posts: 11
I think because of I cant close session (session.close())
When I refreh the page, It gets a new connection so there are lots of connections.

If I enable session.close() in my code, then jsp page(view) not rendering.
I my need to close session after jsp page renders. But how can do that?
I use a jsp:include as;
Code:
<%
            boolean populateCurrentMessages = mm.populateCurrentMessages();
            model.Message message = null;
            int count = mm.getMessageList().size();
            Iterator<model.Message> itr = mm.getMessageList().iterator();
         
                while (itr.hasNext()) {
                    message = (model.Message) itr.next();
    %>
    <div class="message" id="message<%=message.getId()%>">
    <p id="message-item">
        <b><%=message.getSender().getUserName()%></b>

    </p>
       
    <p> <%=message.getUser().getUserName()%>   
    </p>

    </div>
   
    <%

        }

    %>


here there is a method (mm.populateCurrentMessages();) requesting hibernate (I posted at the top)

How can I close this connection after page render?

I am looking logs...


Top
 Profile  
 
 Post subject: Re: hibernate connection pool release
PostPosted: Wed Mar 03, 2010 11:29 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Ok, now I think to have understood the problem:
Code:
message.getSender().getUserName()

and/or

message.getUser().getUserName()

is requiring data which has not been eager loaded, so hibernate tries to load this data and for this purpose it needs the session
which you already closed explicitly.

Quote:
How can I close this connection after page render?


This is the right question.
If you close the session after the page is rendered then you should get rid of the problem.
On the other side this question has nothing to do with hibernate and is off-topic here.
You should consult your jsp manual.

Or maybe it would be working with :
Code:
<%
            boolean populateCurrentMessages = mm.populateCurrentMessages();
            model.Message message = null;
            int count = mm.getMessageList().size();
            Iterator<model.Message> itr = mm.getMessageList().iterator();
         
                while (itr.hasNext()) {
                    message = (model.Message) itr.next();
    %>
    <div class="message" id="message<%=message.getId()%>">
    <p id="message-item">
        <b><%=message.getSender().getUserName()%></b>

    </p>
       
    <p> <%=message.getUser().getUserName()%>   
    </p>

    </div>
   
    <%
          mm.closeSession();  // you must implement this method by closing the session you used for making the query
        }

    %>


Top
 Profile  
 
 Post subject: Re: hibernate connection pool release
PostPosted: Thu Mar 04, 2010 10:53 am 
Newbie

Joined: Wed Mar 03, 2010 6:27 am
Posts: 11
I solved the issue,
Thanks very much...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.