JSF mysql tomcat内存泄漏

时间:2015-05-21 16:22:46

标签: java mysql tomcat memory-leaks

我是一名基本的程序员,我遇到了一个我以前没见过的错误。我使用MySQL,Tomcat 8和jsf。 这是我的申请(部分):

当我在文本区域写入内容并点击“Mehet”按钮时,它会生成以下内容:(xhtml页面)

<p:commandButton value="Mehet"
                        action="#{alvallalkozoBevitelBean.add()}" update="@form :alvallalkozoFrm" />

“alvallalkozoFrm”形式内的数据表。 Java代码:

public void add() throws SQLException {

    String sql = "INSERT INTO alvallalkozo(Ceg_neve, Ceg_szekhelye, Kapcsolattarto_neve, Kapcsolattarto_email, Ceg_telefonsz, Ceg_megjegyzes) VALUES(?,?,?,?,?,?)";

    if (ds == null)
        throw new SQLException("Datasource nem található");
    PreparedStatement ps = null;
    Connection con = ds.getConnection();
    if (con == null)
        throw new SQLException("Nincs adatbázis kapcsolat");
    ps = con.prepareStatement(sql);

    ps.setString(1, alvallalkozo.getCegNeve());
    ps.setString(2, alvallalkozo.getCegSzekhelye());
    ps.setString(3, alvallalkozo.getKapcsolattartoNeve());
    ps.setString(4, alvallalkozo.getKapcsolattartoEmail());
    ps.setString(5, alvallalkozo.getCegTelefonsz());
    ps.setString(6, alvallalkozo.getCegMegjegyzes());
    ps.executeUpdate();

    System.out.println("Data Added Successfully");
}

我只能用它添加7倍的数据,当我想用​​“Mehet”按钮添加第8个数据时,Tomcat冻结,并且chrome显示以下内容:

enter image description here

然后我停止Tomcat因为我无法用它做任何事情,我在Tomcat中得到以下异常:

    WARNING: The web application [eDispatcher] is still processing a request that has yet to finish. This is very likely to create a memory leak. You can control the time allowed for requests to finish by using the unloadDelay attribute of the standard Context implementation. Stack trace of request processing thread:
 sun.misc.Unsafe.park(Native Method)
 java.util.concurrent.locks.LockSupport.park(Unknown Source)
 java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source)
 org.apache.tomcat.dbcp.pool2.impl.LinkedBlockingDeque.takeFirst(LinkedBlockingDeque.java:582)
 org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:439)
 org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:360)
 org.apache.tomcat.dbcp.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:118)
 org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1412)
 com.dangerzone.main.IndexBean.getAlvallalkozok(IndexBean.java:51)
 sun.reflect.GeneratedMethodAccessor59.invoke(Unknown Source)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 java.lang.reflect.Method.invoke(Unknown Source)
 javax.el.BeanELResolver.getValue(BeanELResolver.java:97)
 com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
 com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
 org.apache.el.parser.AstValue.getValue(AstValue.java:167)
 org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
 com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
 javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
 javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
 javax.faces.component.UIData.getValue(UIData.java:732)
 org.primefaces.component.api.UIData.getDataModel(UIData.java:758)
 javax.faces.component.UIData.getRowCount(UIData.java:356)
 org.primefaces.component.api.UIData.calculateFirst(UIData.java:209)
 org.primefaces.component.datatable.DataTableRenderer.preRender(DataTableRenderer.java:115)
 org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:83)
 javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
 javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
 javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
 com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:582)
 com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
 javax.faces.component.UIForm.visitTree(UIForm.java:381)
 javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
 javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
 com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:403)
 com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:322)
 org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
 javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:1004)
 javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
 com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:430)
 com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
 com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
 com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
 com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:655)
 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
 org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
 org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
 org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517)
 org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474)
 java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
 org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
 java.lang.Thread.run(Unknown Source)

有些东西留在后台的内存中,也许是帖子请求。 我的问题:是什么造成了内存泄漏?我该如何解决这个问题?非常感谢和长期代表。

1 个答案:

答案 0 :(得分:1)

您没有关闭Connection因此导致内存泄漏。这可以在stacktrace(强调我的)中注明:

  

sun.misc.Unsafe.park(原生方法)    java.util.concurrent.locks.LockSupport.park(未知来源)    java.util.concurrent.locks.AbstractQueuedSynchronizer $ ConditionObject.await(Unknown Source)    org.apache.tomcat.dbcp.pool2.impl.LinkedBlockingDeque.takeFirst(LinkedBlockingDeque.java:582)    org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:439)    org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:360)    org.apache.tomcat.dbcp.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:118)    的 org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1412)

简而言之:始终关闭资源。

两种方法:

  1. 明确调用方法close

    ps.close();
    con.close();
    
  2. 使用try-with-resources(自Java 7以来可用),无需拨打close,因为尝试会自动为您执行此操作:

    String sql = "...";
    try (Connection con = ds.getConnection();
        PreparedStatement ps = con.prepareStatement(sql)) {
        //...
    } catch (SQLException e) {
        //ALWAYS handle your exceptions as well
    }