-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to remove sleep process in mysql server using hibernate?
PostPosted: Tue Jan 31, 2012 11:49 pm 
Newbie

Joined: Tue Jan 31, 2012 11:26 pm
Posts: 3
Hi,
I'm new to hibernate framework. Need help please!

How to remove sleep process in mysql server using hibernate after doing a session?
Everytime i insert a record using hibernate the process will remain in the
process list of my database (a sleep process).

Is it ok to ignore that sleeping process?

Thanks in advance..


Top
 Profile  
 
 Post subject: Re: How to remove sleep process in mysql server using hibernate?
PostPosted: Thu Feb 02, 2012 4:11 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
This is because of session pooling. Usually hibernate uses a session pool implementation,
that means when you close a session then regarding connection to the database get not closed too,
but it is placed in a pool, in order to be reused when you need a session again.


Top
 Profile  
 
 Post subject: Re: How to remove sleep process in mysql server using hibernate?
PostPosted: Thu Feb 02, 2012 10:51 pm 
Newbie

Joined: Tue Jan 31, 2012 11:26 pm
Posts: 3
Thanks for your fast reply.

If you don't mind sir, can you give me a sample code on how to close a database connection using hibernate.

I tried the session.close() method but it gives me an error "Session was already closed"

here is my code
//////////////////////////////////////////////////////////////////////////////////////////
try {
AnnotationConfiguration config = new AnnotationConfiguration();
config.configure("configuration/hibernate.cfg.xml");
config.addAnnotatedClass(Student.class);
Session session = config.buildSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
try {
for (int row = 0; row < jtableStudent.getRowCount(); row++) {
Student student = new Student();
student.setStudentId(Integer.valueOf(jtableStudent.getValueAt(row, 0).toString()));
student.setFirstName(jtableStudent.getValueAt(row, 1).toString());
student.setMiddleName(jtableStudent.getValueAt(row, 2).toString());
student.setLastName(jtableStudent.getValueAt(row, 3).toString());
student.setAddress(jtableStudent.getValueAt(row, 4).toString());
session.save(student);
}
tx.commit();
isSuccess = true;
} catch (Exception e) {

} finally {
session.close();
}

} catch (Exception e) {
e.printStackTrace();
}

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: How to remove sleep process in mysql server using hibernate?
PostPosted: Fri Feb 03, 2012 3:19 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
I tried the session.close() method but it gives me an error "Session was already closed"


This is because when you use getCurrentSession() to obtain a session

Code:
Session session = config.buildSessionFactory().getCurrentSession();


then the session is already implicitely closed when ending the transaction

Code:
tx.commit();


But ending the hibernate-session does not automatically mean, that also the regarding connection to the database get closed.
As already stated in my last post, there's a session-pool which puts the connection in pool for being reused.

Please read some documentation about session pooling, to understand the architecture of sessions/connections.

If you want to close all connections at a certain point,
you can for example do it by closing the sessionfactory (or entityManagerFactory if you use the JPA approach)

Code:
sessionFactory.close();  // or entityManagerFactory.close();


this closes also the session-pool which releases then all connections.
After this on mysql there should not be any hibernate related process anymore.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.