-->
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.  [ 9 posts ] 
Author Message
 Post subject: Help! Memory Leak with SQLServer 2000!
PostPosted: Wed Mar 03, 2004 11:39 am 
Newbie

Joined: Mon Feb 09, 2004 1:45 pm
Posts: 8
Quick Summary of problem: When using MS SQLServer 2000, I am getting java.lang.OutOfMemoryError exceptions in my weblogic 8.1 myserver_server.log. Additionally, in weblogic8.1's myserver_server_error.log I am getting "WARNING: afterTransactionCompletion() was never called
Mar 3, 2004 8:24:39 AM net.sf.hibernate.impl.SessionImpl finalize
WARNING: unclosed connection" repeatedly. Of important note here is that I never get this issue running the same codebase with MySQL v4. I only get these problems with SQLServer 2000.

Hibernate version: 2.1
Database: Microsoft SQLServer 2000
mapping docs: I do not believe the mapping docs are of importance here with regards to the POJOs. I will include hibernate.cfg.xml though:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- <property name="connection.username">root</property>
<property name="connection.password">neadg</property> -->
<property name="show_sql">true</property>

<property name="connection.url">jdbc:bea:sqlserver://localhost;User=abc;Password=xxx;DatabaseName=yyy</property>
<property name="connection.driver_class">weblogic.jdbc.sqlserver.SQLServerDriver</property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>

<mapping resource="neadg/model/hibernate/ChangeRequestSubmission.hbm.xml"/>
<mapping resource="neadg/model/hibernate/CCBVote.hbm.xml"/>
<mapping resource="neadg/model/hibernate/UserAccount.hbm.xml"/>
<mapping resource="neadg/model/hibernate/UserRoleGroup.hbm.xml"/>
<mapping resource="neadg/model/hibernate/GuideManagerFilter.hbm.xml"/>
<mapping resource="neadg/model/hibernate/CCBDecision.hbm.xml"/>
<mapping resource="neadg/model/hibernate/CommentHistory.hbm.xml"/>
</session-factory>

</hibernate-configuration>

java code for my hibernate implementation:
package neadg.services.generic.persistence.impl.hibernate;

import net.sf.hibernate.Session;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.type.Type;

import neadg.services.generic.persistence.PersistenceServiceException;
import neadg.services.generic.persistence.PersistenceService;
import neadg.model.hibernate.Persistable;

import java.util.ArrayList;

public class HibernateService implements PersistenceService {


private static SessionFactory factory;
private static Object obj = new Object();
private static ArrayList list = new ArrayList();


private Session session;
private Transaction tx;

// Initialize Factory
static {
try {
factory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
}
}

// Singleton
private static HibernateService hibernate = null;
private HibernateService() {}
public static HibernateService instance() {
if(hibernate == null)
hibernate = new HibernateService();

return hibernate;
}


private void openSession() throws PersistenceServiceException {

try {
session = factory.openSession();
tx = session.beginTransaction();
System.out.println("... session open ...");
} catch (Exception e) {
throw new PersistenceServiceException(e);
}
}

private void closeSession() throws PersistenceServiceException {

try {
tx.commit();
session.close();
System.out.println("... session closed ...");
} catch (Exception e) {
throw new PersistenceServiceException(e);
}

}


public void create(Persistable model) throws PersistenceServiceException {
try {

this.openSession();
System.out.println("... creating ...");
session.save(model);
this.closeSession();

} catch (Exception e) {
throw new PersistenceServiceException(e);
}
}

public void update(Persistable model) throws PersistenceServiceException {
try {

this.openSession();
System.out.println("... updating ...");
session.update(model);
this.closeSession();

} catch (Exception e) {
throw new PersistenceServiceException(e);
}
}


public Object load(Class className, int id) throws PersistenceServiceException {

try {

this.openSession();
System.out.println("... loading ...");
obj = session.load(className, new Integer(id));
this.closeSession();

return obj;
} catch (Exception e) {
throw new PersistenceServiceException(e);
}
}


public ArrayList find(String query, Object value, Type hibernateType) throws PersistenceServiceException {

try {

this.openSession();
System.out.println("... finding ...");
list = (ArrayList)session.find(query, value, hibernateType);
this.closeSession();

return list;
} catch (Exception e) {
throw new PersistenceServiceException(e);
}


}

public ArrayList find(String query, Object [] values, Type [] hibernateTypes) throws PersistenceServiceException {

try {

this.openSession();
System.out.println("... finding ...");
list = (ArrayList)session.find(query, values, hibernateTypes);
this.closeSession();

return list;
} catch (Exception e) {
throw new PersistenceServiceException(e);
}


}
}

The full stack trace of exception logged in myserver_server.log:
The system is out of resources.
Consult the following stack trace for details.
java.lang.OutOfMemoryError

at weblogic.servlet.jsp.JspStub.compilePage(JspStub.java:449)
at weblogic.servlet.jsp.JspStub.prepareServlet(JspStub.java:223)
at weblogic.servlet.jsp.JspStub.prepareServlet(JspStub.java:172)
at weblogic.servlet.internal.ServletStubImpl.getServlet(ServletStubImpl.java:518)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:362)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:301)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6350)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3635)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2585)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)
Caused by: java.io.IOException: Compiler failed executable.exec
at weblogic.utils.compiler.CompilerInvoker.compileMaybeExit(CompilerInvoker.java:470)
at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:329)
at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:337)
at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:322)
at weblogic.servlet.jsp.JspStub.compilePage(JspStub.java:422)
... 23 more

The full stack trace in myserver_server_error.log:
Mar 3, 2004 8:24:39 AM net.sf.hibernate.impl.SessionImpl finalize
WARNING: afterTransactionCompletion() was never called
Mar 3, 2004 8:24:39 AM net.sf.hibernate.impl.SessionImpl finalize
WARNING: unclosed connection
Mar 3, 2004 8:24:39 AM net.sf.hibernate.impl.SessionImpl finalize
WARNING: afterTransactionCompletion() was never called
Mar 3, 2004 8:24:39 AM net.sf.hibernate.impl.SessionImpl finalize
WARNING: unclosed connection

Noteworthy comments: These errors only occur when I configure my application to work with MS SQLServer 2000. I do not encounter problems with MySQL v4. I know that data gets pushed and pulled from the database, connections are made, and the weblogic-sqlserver jdbc driver I use seems to work.

Basically, it seems that hibernate sessions are not closed correctly, and that leads me to believe that those memory expensive JDBC connections aren't getting closed.

I think I am doing something incorrectly in my HibernateService class listed above. This "WARNING: afterTransactionCompletion() was never called" in the error log makes me think that I am not utilizing the session API correctly. Also, I assume that I am using Hibernate's version of connection pooling. I use no other Hibernate configuration other than the hibernate.cfg.xml listed above. Perhaps, I need to use the C3P0 connection pooling.

I hope someone can help me here. TIA.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 1:15 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Your session is stored in a singleton, thus if 2 sessions are opened, the first one is lost. Use the Thread Local Session Pattern (see the wiki page)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 2:49 pm 
Newbie

Joined: Mon Feb 09, 2004 1:45 pm
Posts: 8
Thank you for your help, emmanuel. I implemented the ThreadLocal pattern I found on the Wiki patterns page.

Here is how I changed my HibernateService class per emmanuel's comment:

package neadg.services.generic.persistence.impl.hibernate;

import net.sf.hibernate.Session;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.type.Type;

import neadg.services.generic.persistence.PersistenceServiceException;
import neadg.services.generic.persistence.PersistenceService;
import neadg.model.hibernate.Persistable;

import java.util.ArrayList;

public class HibernateService implements PersistenceService {

private static Object obj = new Object();
private static ArrayList list = new ArrayList();

public static final ThreadLocal session = new ThreadLocal();
private Transaction tx;


public static Session currentSession() throws HibernateException {

Session s = (Session) session.get();
if (s == null) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
s = sf.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {

Session s = (Session) session.get();
session.set(null);
if (s != null) s.close();
}

private void close() throws PersistenceServiceException {

try {

this.closeSession();

} catch (HibernateException he) {
throw new PersistenceServiceException(he);
}

}


private void rollBack(HibernateException hibernateException) throws PersistenceServiceException {

try {

if (tx!=null) tx.rollback();
throw new PersistenceServiceException(hibernateException);

}catch (HibernateException he) {

throw new PersistenceServiceException(he);

}


}

public void create(Persistable model) throws PersistenceServiceException {


try {

//session = factory.openSession();
tx = currentSession().beginTransaction();

System.out.println("... creating ...");

currentSession().save(model);
tx.commit();


} catch (HibernateException he) {

this.rollBack(he);

} finally {

this.close();
}
}

public void update(Persistable model) throws PersistenceServiceException {

try {

//session = factory.openSession();
tx = currentSession().beginTransaction();

System.out.println("... updating ...");

currentSession().update(model);
tx.commit();


} catch (HibernateException he) {

this.rollBack(he);

} finally {

this.close();
}

}


public Object load(Class className, int id) throws PersistenceServiceException {

try {

//session = factory.openSession();
tx = currentSession().beginTransaction();

System.out.println("... loading ...");

obj = currentSession().load(className, new Integer(id));
tx.commit();



} catch (HibernateException he) {

this.rollBack(he);

} finally {

this.close();
}

return obj;
}


public ArrayList find(String query, Object value, Type hibernateType) throws PersistenceServiceException {

try {

//session = factory.openSession();
tx = currentSession().beginTransaction();

System.out.println("... finding ...");

list = (ArrayList)currentSession().find(query, value, hibernateType);
tx.commit();


} catch (HibernateException he) {

this.rollBack(he);

} finally {

this.close();
}

return list;

}

public ArrayList find(String query, Object [] values, Type [] hibernateTypes) throws PersistenceServiceException {

try {

//session = factory.openSession();
tx = currentSession().beginTransaction();

System.out.println("... finding ...");

list = (ArrayList)currentSession().find(query, values, hibernateTypes);
tx.commit();


} catch (HibernateException he) {

this.rollBack(he);

} finally {

this.close();
}

return list;
}
}

I will continue to experiment to see if I can reproduce any of my errors from my original post.

One last question:
I assume that I am not using any type of connection pooling here. I read in the documentation that one should not depend on hibernate's rudimentary connection pooling implementation. Instead, I should use something like C3P0 connection pooling. I think adding this connection pooling is as simple as adding the correct C3P0 properties to my hibernate.cfg.xml and ensuring that the necessary C3P0 jar files are in my app's lib directory. Correct?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 3:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you are running inside an application server, you should really use an appserver provided datasource, where the server does the pooling.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 3:05 pm 
Newbie

Joined: Mon Feb 09, 2004 1:45 pm
Posts: 8
michael wrote:
If you are running inside an application server, you should really use an appserver provided datasource, where the server does the pooling.


Point taken. Thanks for all of your help. This hibernate team seems very responsive and informative. It's appreciated from my end very much.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 11:35 am 
Newbie

Joined: Mon Feb 09, 2004 1:45 pm
Posts: 8
emmanuel wrote:
Your session is stored in a singleton, thus if 2 sessions are opened, the first one is lost. Use the Thread Local Session Pattern (see the wiki page)


I'm noticing some performance issues since I went to this Thread Local design pattern. You mentioned that my previous problem was related to keeping the hibernate session in a singleton. I may have gone overboard and created a new instance of SessionFactory on every hibernate call in my system.

Is it ok to keep the SessionFactory static so that I don't have to get the configuration file for hibernate everytime I want to make a hibernate call?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 11:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Of course this is okay


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 5:13 pm 
Newbie

Joined: Mon Feb 09, 2004 1:45 pm
Posts: 8
Sorry to ask such seemingly simple questions, but I was really looking for sanity checks. It's not been to often that I've had to chase a memory leak. It seems that my errant implementation of hibernate had been causing it, and as such, I thought I would show the solution to my problem.

The following class is being posted to "wrap up" this thread in case anyone may run into the same situation down the road.

Entirity of my HibernateService implementation class:

package neadg.services.generic.persistence.impl.hibernate;

import net.sf.hibernate.Session;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.type.Type;

import neadg.services.generic.persistence.PersistenceServiceException;
import neadg.services.generic.persistence.PersistenceService;
import neadg.model.hibernate.Persistable;

import java.util.ArrayList;

public class HibernateService implements PersistenceService {


private static SessionFactory sf;
private static Object obj = new Object();
private static ArrayList list = new ArrayList();

public static final ThreadLocal session = new ThreadLocal();
private Transaction tx;


// Initialize Factory
static {
try {
sf = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
}
}

public static Session currentSession() throws HibernateException {

Session s = (Session) session.get();
if (s == null) {

s = sf.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {

Session s = (Session) session.get();
session.set(null);
if (s != null) s.close();
}

private void close() throws PersistenceServiceException {

try {

this.closeSession();

} catch (HibernateException he) {
throw new PersistenceServiceException(he);
}

}


private void rollBack(HibernateException hibernateException) throws PersistenceServiceException {

try {

if (tx!=null) tx.rollback();
throw new PersistenceServiceException(hibernateException);

}catch (HibernateException he) {

throw new PersistenceServiceException(he);

}


}

public void create(Persistable model) throws PersistenceServiceException {


try {

tx = currentSession().beginTransaction();

System.out.println("... creating ...");

currentSession().save(model);
tx.commit();


} catch (HibernateException he) {

this.rollBack(he);

} finally {

this.close();
}
}

public void update(Persistable model) throws PersistenceServiceException {

try {

tx = currentSession().beginTransaction();

System.out.println("... updating ...");

currentSession().update(model);
tx.commit();


} catch (HibernateException he) {

this.rollBack(he);

} finally {

this.close();
}

}


public Object load(Class className, int id) throws PersistenceServiceException {

try {

tx = currentSession().beginTransaction();

System.out.println("... loading ...");

obj = currentSession().load(className, new Integer(id));
tx.commit();



} catch (HibernateException he) {

this.rollBack(he);

} finally {

this.close();
}

return obj;
}


public ArrayList find(String query, Object value, Type hibernateType) throws PersistenceServiceException {

try {

tx = currentSession().beginTransaction();

System.out.println("... finding ...");

list = (ArrayList)currentSession().find(query, value, hibernateType);
tx.commit();


} catch (HibernateException he) {

this.rollBack(he);

} finally {

this.close();
}

return list;

}

public ArrayList find(String query, Object [] values, Type [] hibernateTypes) throws PersistenceServiceException {

try {

tx = currentSession().beginTransaction();

System.out.println("... finding ...");

list = (ArrayList)currentSession().find(query, values, hibernateTypes);
tx.commit();


} catch (HibernateException he) {

this.rollBack(he);

} finally {

this.close();
}

return list;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 7:55 am 
Newbie

Joined: Wed May 07, 2008 3:59 am
Posts: 1
Location: india
package com.nic.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;


public class HibernateSessionFactoryTalukOnline {


private static String CONFIG_FILE_LOCATION = "/hibernatetalukonline.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;


private HibernateSessionFactoryTalukOnline() {
}


public static Session getSession() throws HibernateException {

try {
System.out.println("Second TimeSecondSecond TimeSecondSecond TimeSecondSecond TimeSecondSecond TimeSecondSecond TimeSecond ");


sessionFactory = configuration.buildSessionFactory();
System.out.println("Second TimeSecondSecond TimeSecondSecond TimeSecondSecond TimeSecondSecond TimeSecondSecond TimeSecond ="+sessionFactory);


} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}

Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

public static Session getSession(String d_uname, String d_pass) throws HibernateException {



configuration.setProperty("hibernate.connection.username", d_uname);
configuration.setProperty("hibernate.connection.password", d_pass);

System.out.println(d_uname + " in hibernate session factory");
System.out.println(d_pass + " in hibernate session factory");

sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");

System.out.println("this is in hibernate getSession " + e);
e.printStackTrace();
}

Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
//rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}


public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}


public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}


public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}


public static void setConfigFile(String configFile) {
HibernateSessionFactoryTalukOnline.configFile = configFile;
sessionFactory = null;
}


public static Configuration getConfiguration() {
return configuration;
}


/* private static final SessionFactory sessionFactory;

static {
try {

// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure("/hibernateeservices.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}*/

}
this is my sesssion Classs


This Way i am getting record
public List<MstGrievanceMajor> getGrievanceMajor()
{


Session session =null;
List<MstGrievanceMajor> list=null;
try
{
session= HibernateSessionFactoryTalukOnline.getSession();
list = session.createQuery("from MstGrievanceMajor as major").list();

}
catch(Exception e)
{
System.out.println(e);
}
finally
{
if(session != null ||session.isOpen())
{
session.clear();
session.close();
}
}
return list;
}




Still Memmory Leak is there,

we are using tomcat6.0 and jre6.0
and
we have set
jvm size as 512m
and permGenSize 512
now its going well

if its reaches near 512 the application going well

but if i am going to undeploy the application and deploy the new war
PermGenSpace error si coming


i am new for Hibernate.
If any one found the solution kindly quote


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