Olá pessoal! Estou tendo que fazer umas modificações em um site que faz tempo que eu fis. Hoje eu uso spring com hibernate templates então não preciso ficar abrindo session e fechando. Só que nesse site eu abria e fechava, mas nunca tive problemas. Hoje começou um problema que tenho quase certeza que está na forma de abrir e fechar as session do hibernate. Eu navego normal no site até um tempo, depois começa a ficar lento pra caramba o site e num vai de jeito nenhum, daí depois de poucas navegadas lentas no site eu recebo um OutOfMemoryError: Java heap space. Olhem o código meu de abrir e fechar sessions:
public class HibernateSession {
protected Session session;
private SessionFactory sessionFactory;
public void AbreSession() {
AnnotationConfiguration conf = new AnnotationConfiguration();
conf.configure();
if (sessionFactory == null || sessionFactory.isClosed()) {
sessionFactory = conf.buildSessionFactory();
}
try {
Session s = sessionFactory.getCurrentSession();
session = s;
} catch (HibernateException e) {
session = sessionFactory.openSession();
}
}
public void FechaSession() {
session.close();
try {
sessionFactory.getCurrentSession().close();
} catch (HibernateException e) {}
}
}
Os meus daos extendem dessa classe e no método construtor dos daos eu chamo o AbreSession(). E nos controllers depois de cada result.forwardTo(…)(Uso o VRaptor) eu dou um dao.FechaSession().
Código do Erro:
br.com.caelum.vraptor.InterceptionException: exception raised, check root cause for details: br.com.caelum.vraptor.view.ResultException: org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space
at br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor.intercept(ExecuteMethodInterceptor.java:96)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.core.LazyInterceptorHandler.execute(LazyInterceptorHandler.java:61)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.core.LazyInterceptorHandler.execute(LazyInterceptorHandler.java:61)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at Filtros.LoginInterceptor.intercept(LoginInterceptor.java:30)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.interceptor.InstantiateInterceptor.intercept(InstantiateInterceptor.java:48)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor.intercept(ExceptionHandlerInterceptor.java:71)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:83)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.interceptor.ResourceLookupInterceptor.intercept(ResourceLookupInterceptor.java:69)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.core.EnhancedRequestExecution.execute(EnhancedRequestExecution.java:23)
at br.com.caelum.vraptor.VRaptor$1.insideRequest(VRaptor.java:92)
at br.com.caelum.vraptor.ioc.spring.SpringProvider.provideForRequest(SpringProvider.java:58)
at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:89)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: br.com.caelum.vraptor.view.ResultException: org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space
at br.com.caelum.vraptor.view.DefaultPageResult.forwardTo(DefaultPageResult.java:107)
at br.com.caelum.vraptor.core.AbstractResult.forwardTo(AbstractResult.java:19)
at Controllers.RedirectController.cadastrar(RedirectController.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor.intercept(ExecuteMethodInterceptor.java:61)
... 43 more
Caused by: org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space
at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:548)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at br.com.caelum.vraptor.core.DefaultStaticContentHandler.deferProcessingToContainer(DefaultStaticContentHandler.java:64)
at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:80)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at br.com.caelum.vraptor.view.DefaultPageResult.forwardTo(DefaultPageResult.java:105)
... 50 more
Caused by: javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space
at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:907)
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
at org.apache.jsp.restrito.cadastrar_jsp._jspService(cadastrar_jsp.java:137)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
... 64 more
Caused by: javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space
at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
at org.apache.jsp.templates.footer_jsp._jspService(footer_jsp.java:100)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:343)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.doEndTag(ImportSupport.java:200)
at org.apache.jsp.restrito.cadastrar_jsp._jspx_meth_c_005fimport_005f3(cadastrar_jsp.java:261)
at org.apache.jsp.restrito.cadastrar_jsp._jspService(cadastrar_jsp.java:127)
... 67 more
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Arrays.java:3209)
at java.lang.String.<init>(String.java:216)
at java.lang.StringBuffer.toString(StringBuffer.java:585)
at org.hibernate.util.StringHelper.join(StringHelper.java:63)
at org.hibernate.sql.Update.toStatementString(Update.java:183)
at org.hibernate.persister.collection.OneToManyPersister.generateDeleteRowString(OneToManyPersister.java:158)
at org.hibernate.persister.collection.AbstractCollectionPersister.<init>(AbstractCollectionPersister.java:476)
at org.hibernate.persister.collection.OneToManyPersister.<init>(OneToManyPersister.java:83)
at org.hibernate.persister.PersisterFactory.createCollectionPersister(PersisterFactory.java:104)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:288)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at DAOs.HibernateSession.AbreSession(HibernateSession.java:17)
at DAOs.VisitasDAO.<init>(VisitasDAO.java:9)
at org.apache.jsp.templates.footer_jsp._jspService(footer_jsp.java:78)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:343)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.doEndTag(ImportSupport.java:200)
at org.apache.jsp.restrito.cadastrar_jsp._jspx_meth_c_005fimport_005f3(cadastrar_jsp.java:261)
at org.apache.jsp.restrito.cadastrar_jsp._jspService(cadastrar_jsp.java:127)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
Obrigado!
[quote=esdras_63]Olá pessoal! Estou tendo que fazer umas modificações em um site que faz tempo que eu fis. Hoje eu uso spring com hibernate templates então não preciso ficar abrindo session e fechando. Só que nesse site eu abria e fechava, mas nunca tive problemas. Hoje começou um problema que tenho quase certeza que está na forma de abrir e fechar as session do hibernate. Eu navego normal no site até um tempo, depois começa a ficar lento pra caramba o site e num vai de jeito nenhum, daí depois de poucas navegadas lentas no site eu recebo um OutOfMemoryError: Java heap space. Olhem o código meu de abrir e fechar sessions:
public class HibernateSession {
protected Session session;
private SessionFactory sessionFactory;
public void AbreSession() {
AnnotationConfiguration conf = new AnnotationConfiguration();
conf.configure();
if (sessionFactory == null || sessionFactory.isClosed()) {
sessionFactory = conf.buildSessionFactory();
}
try {
Session s = sessionFactory.getCurrentSession();
session = s;
} catch (HibernateException e) {
session = sessionFactory.openSession();
}
}
public void FechaSession() {
session.close();
try {
sessionFactory.getCurrentSession().close();
} catch (HibernateException e) {}
}
}
Os meus daos extendem dessa classe e no método construtor dos daos eu chamo o AbreSession(). E nos controllers depois de cada result.forwardTo(…)(Uso o VRaptor) eu dou um dao.FechaSession().
Código do Erro:
br.com.caelum.vraptor.InterceptionException: exception raised, check root cause for details: br.com.caelum.vraptor.view.ResultException: org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space
at br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor.intercept(ExecuteMethodInterceptor.java:96)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.core.LazyInterceptorHandler.execute(LazyInterceptorHandler.java:61)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.core.LazyInterceptorHandler.execute(LazyInterceptorHandler.java:61)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at Filtros.LoginInterceptor.intercept(LoginInterceptor.java:30)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.interceptor.InstantiateInterceptor.intercept(InstantiateInterceptor.java:48)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor.intercept(ExceptionHandlerInterceptor.java:71)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:83)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.interceptor.ResourceLookupInterceptor.intercept(ResourceLookupInterceptor.java:69)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
at br.com.caelum.vraptor.core.EnhancedRequestExecution.execute(EnhancedRequestExecution.java:23)
at br.com.caelum.vraptor.VRaptor$1.insideRequest(VRaptor.java:92)
at br.com.caelum.vraptor.ioc.spring.SpringProvider.provideForRequest(SpringProvider.java:58)
at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:89)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: br.com.caelum.vraptor.view.ResultException: org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space
at br.com.caelum.vraptor.view.DefaultPageResult.forwardTo(DefaultPageResult.java:107)
at br.com.caelum.vraptor.core.AbstractResult.forwardTo(AbstractResult.java:19)
at Controllers.RedirectController.cadastrar(RedirectController.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor.intercept(ExecuteMethodInterceptor.java:61)
... 43 more
Caused by: org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space
at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:548)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at br.com.caelum.vraptor.core.DefaultStaticContentHandler.deferProcessingToContainer(DefaultStaticContentHandler.java:64)
at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:80)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at br.com.caelum.vraptor.view.DefaultPageResult.forwardTo(DefaultPageResult.java:105)
... 50 more
Caused by: javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space
at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:907)
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
at org.apache.jsp.restrito.cadastrar_jsp._jspService(cadastrar_jsp.java:137)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
... 64 more
Caused by: javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space
at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
at org.apache.jsp.templates.footer_jsp._jspService(footer_jsp.java:100)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:343)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.doEndTag(ImportSupport.java:200)
at org.apache.jsp.restrito.cadastrar_jsp._jspx_meth_c_005fimport_005f3(cadastrar_jsp.java:261)
at org.apache.jsp.restrito.cadastrar_jsp._jspService(cadastrar_jsp.java:127)
... 67 more
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Arrays.java:3209)
at java.lang.String.<init>(String.java:216)
at java.lang.StringBuffer.toString(StringBuffer.java:585)
at org.hibernate.util.StringHelper.join(StringHelper.java:63)
at org.hibernate.sql.Update.toStatementString(Update.java:183)
at org.hibernate.persister.collection.OneToManyPersister.generateDeleteRowString(OneToManyPersister.java:158)
at org.hibernate.persister.collection.AbstractCollectionPersister.<init>(AbstractCollectionPersister.java:476)
at org.hibernate.persister.collection.OneToManyPersister.<init>(OneToManyPersister.java:83)
at org.hibernate.persister.PersisterFactory.createCollectionPersister(PersisterFactory.java:104)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:288)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at DAOs.HibernateSession.AbreSession(HibernateSession.java:17)
at DAOs.VisitasDAO.<init>(VisitasDAO.java:9)
at org.apache.jsp.templates.footer_jsp._jspService(footer_jsp.java:78)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:343)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.doEndTag(ImportSupport.java:200)
at org.apache.jsp.restrito.cadastrar_jsp._jspx_meth_c_005fimport_005f3(cadastrar_jsp.java:261)
at org.apache.jsp.restrito.cadastrar_jsp._jspService(cadastrar_jsp.java:127)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
Obrigado![/quote]
você falou que seus daos herdam essa classe e no construtor do dao você chama o seu abresession?
perceba que o seu objeto session e o sessionFactory são internos ao objeto, a cada objeto criado, no caso a cada dao criado, você chama o abreSession, sendo assim se você tiver 20 daos instanciados num dado momento você terá 20 sessionFactories abertos e 20 sessões abertas. Aconselho você a (ja que você vai gerenciar isso e não o framework), deixar o sessionFactory estatico, um ThreadLocal para você armazenar a sessão de cada Thread e o fechaSession fechando a sessão que ele pegar do seu ThreadLocal.
Estou usando o myeclipse e criei um HibernateSessionFactory(similar ao HibernateUtils) e ele tem esse controle de sessionFactory na thread local. Olhem as modificações:
HibernateSession(a classe que os daos herdam):
public class HibernateSession {
protected Session session;
public void AbreSession() {
SessionFactory sessionFactory = HibernateSessionFactory.getSessionFactory();
session = sessionFactory.openSession();
}
public void FechaSession() {
session.close();
}
}
E a HibernateSessionFactory que mandei criar:
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file. Location should be on the classpath as
* Hibernate uses #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is in the
* default package. Use #setConfigFile() to update the location of the
* configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new AnnotationConfiguration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize the
* <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
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;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
Vou reiniciar o pc e dar umas navegadas para ver se acaba o problema e já posto os resultados.
Obrigado!
um exemplo que acho se enquadrar no seu caso:
[code]package br.com.triad.util.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtils {
private static ThreadLocal<Session> tl = new ThreadLocal<Session>();
private static SessionFactory sf = null;
private HibernateUtils me;
static{
sf = new AnnotationConfiguration().configure("endereço do seu arquivo de configuração").buildSessionFactory();
}
private HibernateUtils(){
}
public HibernateUtils getInstance(){
if (me == null) me = new HibernateUtils();
return me;
}
/**
* retorna a sessão da thread atual
* @return
*/
public Session getSession(){
//se for o caso você pode colocar algum outro tratamento que você queira aqui
Session s = tl.get();
if (s == null || !s.isOpen() ){
s = openSession();
tl.set(s);
return s;
}else
return s;
}
/**
* abre uma sessão nova ou mantém a mesma caso a thread atual possua alguma aberta
* @return
*/
private Session openSession(){
try {
return sf.openSession();
} catch (Throwable e) {
throw new RuntimeException("erro ao abrir sessão com o banco de dados ", e);
}
}
/**
* fecha a sessão da thread atual
*/
public void closeSession(){
try {
Session s = tl.get();
if (s != null && s.isOpen()){
s.close();
tl.set(null);
}else{
tl.set(null);
}
} catch (Throwable e) {
throw new RuntimeException("erro ao fechar sessão com o banco de dados ", e);
}
}
}
public class DaoPai {
private HibernateUtils utils;
public DaoPai(){
utils = HibernateUtils.getInstance();
}
public Session getSession(){
return utils.getSession();
}
public void closeSession(){
return utils.closeSession();
}
}
[/code]
editei para ficar um código melhor… é melhor você “ter um” do seu utils e não você “ser um”, herdar isso não é é uma boa idéia por diversos motivos, sendo assim deixei ele como um atributo que você pode ter na sua classe que você herda nos seus daos mude a classe que você herda nos seus daos para a classe DaoPai que eu deixei e crie a outra como nesse exemplo. Você ainda vai herdar estes métodos que você usa então sua manutenção vai ser rápida ainda…
[quote=esdras_63]Estou usando o myeclipse e criei um HibernateSessionFactory(similar ao HibernateUtils) e ele tem esse controle de sessionFactory na thread local. Olhem as modificações:
HibernateSession(a classe que os daos herdam):
public class HibernateSession {
protected Session session;
public void AbreSession() {
SessionFactory sessionFactory = HibernateSessionFactory.getSessionFactory();
session = sessionFactory.openSession();
}
public void FechaSession() {
session.close();
}
}
E a HibernateSessionFactory que mandei criar:
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file. Location should be on the classpath as
* Hibernate uses #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is in the
* default package. Use #setConfigFile() to update the location of the
* configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new AnnotationConfiguration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize the
* <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
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;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
Vou reiniciar o pc e dar umas navegadas para ver se acaba o problema e já posto os resultados.
Obrigado![/quote]
do jeito que estava antes pelo que você postou na primeira mensagem eu te diria que só demorava algum tempo para estourar a memória por que o garbage colector devia estar trabalhando bastante na hora de matar os objetos dos seus daos (por causa das sessões e fabricas da classe herdade que ele provavelmente fecha).
te mandei uma mp com algumas dicas quanto ao seu código.
a classe que te deixei ai na outra mensagem assim… o ideal é que você a transforme em um singleton e os seus daos “tenham um” dessa classe e não que “sejam um”…
Acho que resolvi! dei uma boa navegada abrindo e fechando sessions que nem loco e não deu erro. Qualquer coisa posto aqui denovo.
Obrigado Cara!!
Olá! Depois de ter acabado com o outofmemory tive alguns problemas com sessions com certeza. Vou implementar esse código seu na minha aplicação. Não vejo a hora de acabar com isso e voltar para o meu spring hehehehe. Nem tinha visto que você editou a mensagem. Agora ficou bem melhor. Obrigado!