内存泄漏,tomcat 7

时间:2015-02-01 15:54:57

标签: java rest tomcat jersey

当你启动tomcat时, 它消耗70mb的内存, 当我提出请求时,它消耗80mbs, 永远不会回到70mbs

Crud代码

package br.com.controlevendas.recurso;


import java.io.IOException;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import br.com.controlevendas.controle.ClienteControle;
import br.com.controlevendas.modelo.Cliente;

@Component
@Path("/cliente")
public class ClienteRecurso {

    @Autowired
    private ClienteControle clienteControle;

    @GET
    @Path("/lista")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Cliente> listar() {


        List<Cliente> clienteList = this.clienteControle.listar();

        try {
            this.clienteControle.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return clienteList;
    }

    @POST
    @Path("")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Cliente inserir(Cliente cliente) {

        cliente = this.clienteControle.salvar(cliente);

        try {
            this.clienteControle.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return cliente;
    }

    @PUT
    @Path("")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Cliente alterar(Cliente cliente) {

        cliente = this.clienteControle.alterar(cliente);

        try {
            this.clienteControle.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return cliente;
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Cliente buscar(@PathParam("id") Integer id) {
        Cliente cliente = new Cliente();
        cliente.setIdcliente(id);
        cliente = this.clienteControle.buscar(cliente);

        try {
            this.clienteControle.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return cliente;
    }

    @DELETE
    @Path("/{id}")
    public void remover(@PathParam("id") Integer id) {

        Cliente cliente = new Cliente();
        cliente.setIdcliente(id);
        cliente = this.clienteControle.remover(cliente);

        try {
            this.clienteControle.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

ClienteControle

package br.com.controlevendas.controle;

import java.io.Closeable;
import java.util.List;

import br.com.controlevendas.modelo.Cliente;

public interface ClienteControle extends Closeable {

    Cliente salvar(Cliente cliente);

    Cliente alterar(Cliente cliente);

    Cliente remover(Cliente cliente);

    Cliente buscar(Cliente cliente);

    List<Cliente> listar();

}

ClienteControleImplementacao

package br.com.controlevendas.controle.implementacao;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.controlevendas.controle.ClienteControle;
import br.com.controlevendas.dao.ClienteDAO;
import br.com.controlevendas.modelo.Cliente;

@Service
public class ClienteControleImplementacao implements ClienteControle {

    @Autowired
    private ClienteDAO clienteDAO;

    @Override
    public void close() throws IOException {
        this.clienteDAO.close();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Cliente salvar(Cliente cliente) {

        return this.clienteDAO.salvar(cliente);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Cliente alterar(Cliente cliente) {

        return this.clienteDAO.alterar(cliente);
    }

    @Override
    public Cliente buscar(Cliente cliente) {

        return this.clienteDAO.buscar(cliente);
    }

    @Override
    public List<Cliente> listar() {

        return this.clienteDAO.listar();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Cliente remover(Cliente cliente) {

        return this.clienteDAO.remover(cliente);
    }    
}

ClienteDAO

package br.com.controlevendas.dao;

import java.io.Closeable;
import java.util.List;

import br.com.controlevendas.modelo.Cliente;

public interface ClienteDAO extends Closeable {

    Cliente salvar(Cliente cliente);

    Cliente alterar(Cliente cliente);

    Cliente remover(Cliente cliente);

    Cliente buscar(Cliente cliente);

    List<Cliente> listar();
}

ClienteDAOImplementacao

package br.com.controlevendas.dao.implementacao;

import java.io.IOException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import br.com.controlevendas.dao.ClienteDAO;
import br.com.controlevendas.modelo.Cliente;

@Repository
public class ClienteDAOImplementacao implements ClienteDAO {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Cliente salvar(Cliente cliente) {

        this.entityManager.persist(cliente);

        return cliente;
    }

    @Override
    public Cliente alterar(Cliente cliente) {

        return this.entityManager.merge(cliente);
    }

    @Override
    public Cliente buscar(Cliente cliente) {
        Query query = this.entityManager.createQuery("from Cliente where idcliente = ?");

        query.setParameter(1, cliente.getIdcliente());

        return (Cliente) query.getSingleResult();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Cliente> listar() {

        Query query = this.entityManager.createQuery("from Cliente");

        return query.getResultList();
    }

    @Override
    public void close() throws IOException {

        this.entityManager.close();
    }

    @Override
    public Cliente remover(Cliente cliente) {

        cliente = this.entityManager.getReference(Cliente.class, cliente.getIdcliente());

        this.entityManager.remove(cliente);

        return cliente;
    }

}

的persistence.xml

   <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
   http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

     <persistence-unit name="controlevendas">

       <!-- provedor/implementacao do JPA -->
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

     </persistence-unit>
    </persistence>

的WebContent / META-INF / context.xml中

<?xml version="1.0" encoding="UTF-8"?>

<Context reladable="true">
    <Resource 
        name="jdbc/vendas"
        auth="Container"
        type="javax.sql.DataSource"
        maxActive="100"
        maxIdle="30"
        maxWait="60"
        username="root"
        password="root"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost/controlevendas"/>

</Context>

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">


        <context:annotation-config /> 

        <context:component-scan base-package="br.com.controlevendas.controle.implementacao" />
        <context:component-scan base-package="br.com.controlevendas.dao.implementacao" />
        <context:component-scan base-package="br.com.controlevendas.recurso" />  


    <!-- <bean id="dataSourceMySQL" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> -->
        <!-- <property name="driverClass" value="com.mysql.jdbc.Driver" />

        <property name="jdbcUrl" value="jdbc:mysql://localhost/controlevendas" />
        <property name="user" value="root" />
        <property name="password" value="root" />
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        <property name="maxStatements" value="50" />
        <property name="checkoutTimeout" value="3000" />
        <property name="idleConnectionTestPeriod" value="3000" />
        <property name="maxIdleTime" value="180" />
        <property name="numHelperThreads" value="5" />
        <property name="acquireIncrement" value="1" />
        <property name="acquireRetryAttempts" value="5" /> -->
    <!-- </bean> -->


    <bean id="dataSource.jndi" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton" lazy-init="true">
        <property name="jndiName" value="java:comp/env/jdbc/vendas" />
    </bean>

    <bean id="entityManagerFactory"    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <!-- <property name="dataSource" ref="dataSourceMySQL" /> -->

        <property name="dataSource" ref="dataSource.jndi" />

        <property name="jpaVendorAdapter">
            <bean
                class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="MYSQL" />
                <property name="showSql" value="true" />
                <property name="generateDdl" value="false" />
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
                <prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
                <prop key="hibernate.jdbc.batch_size">0</prop>
                <prop key="hibernate.connection.release_mode">after_transaction</prop>
                <prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
                <prop key="hibernate.statement_cache.size">0</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>    

    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>ControleVendas</display-name> 

  <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

  <servlet>
        <servlet-name>jersey-spring</servlet-name>
        <servlet-class>
                     com.sun.jersey.spi.spring.container.servlet.SpringServlet
        </servlet-class>
        <init-param>
             <param-name>com.sun.jersey.config.property.packages</param-name>
             <param-value>br.com.controlevendas.recurso</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-spring</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

      <resource-ref>
          <description>DataSource Vendas</description>
          <res-ref-name>jdbc/vendas</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
      </resource-ref>

      <session-config>
          <session-timeout>1</session-timeout>
      </session-config>

</web-app>

hibernate 4.3.8 春天4.14 球衣1.18 杰克逊1.9.13

有人帮忙提示吗?

0 个答案:

没有答案