Search code examples
javatomcatmemorydeploymentcatalina

How to configutre Tomcat (Catalina) to redeploy web app without restarting server?


Every time I am trying to redeploy my application without restarting server I receive PermGen error, and when I'am trying to close server I receive:

Java HotSpot(TM) 64-Bit Server VM warning: Exception java.lang.OutOfMemoryError occurred dispatching signal UNKNOWN to handler- the VM may need to be forcibly terminated.

Edit 1:

Sorry, forgot to add my java code. Here I am using Hibernate + Spring (WebApplicationInitializer), and the error apear only when I am using it.

package spring2;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller("mainControler")
public class Controll {

    @Autowired
    @Qualifier("personValidator")
    Validator validator;
    private Configuration conf;
    private SessionFactory factory;
    private Session session;
    private Transaction transaction;

    public Controll() {
        // TODO Auto-generated constructor stub
        conf = new Configuration();
        conf.configure("hibernate.cfg.xml");

    }

    @RequestMapping("/")
    public String hello() {
        return "hello";
    }


    //Initiation of the model Person
    @ModelAttribute("person")
    public Person createPersonModel() {
        return new Person();
    }

    //Bind validator to the web app data
    @InitBinder
    public void initBinder(WebDataBinder bind) {
        bind.setValidator(validator);
    }

    @RequestMapping(value="/setPerson", method=RequestMethod.POST)
    public String setPerson(@ModelAttribute("person") @Validated Person person, BindingResult res, Model model) {
        if(!res.hasErrors()) {
            model.addAttribute("success", "Successfully added name and surname");

            setPerson(person);
        }
        return "hello";
    }

    @RequestMapping(value="/setPerson", method=RequestMethod.GET)
    public String setPerson() {
        return hello();
    }

    @RequestMapping(value="/deletePerson", method=RequestMethod.POST)
    public String deletePerson(@ModelAttribute("person") @Validated Person person, BindingResult res, Model model) {
        if(!res.hasErrors()) {
            if(deletePerson(person)) model.addAttribute("success", "Successfuly deleted!");
            else model.addAttribute("success", "Unfortunelly antyty was not deleted");
        }
        return "deleted";
    }

    @RequestMapping(value="/deletePerson", method=RequestMethod.GET)
    public String deletePerson() {
        return "deleted";
    }

    private void setPerson(Person person) {

        factory = conf.buildSessionFactory();
        session = factory.openSession();
        transaction = session.beginTransaction();

        session.persist(person);
        transaction.commit();
        session.close();

    }

    private boolean deletePerson(Person person) {
        factory = conf.buildSessionFactory();

        session = factory.openSession();
        transaction = session.beginTransaction();
//      SQLQuery query = session.createSQLQuery("delete from persons where name= \"" + person.getName() + "\"");
//      query.executeUpdate();
//      Person pers = (Person) session.get(Person.class, person.getId());
        Query query = session.createQuery("from Person as p where p.name like :name").setString("name", "%" + person.getName() + "%");
        List<Person> list = query.list();
        if(list.size() > 0) {
            Person pers1 = list.get(0);
            session.delete(pers1);
            transaction.commit();
            session.close();
            return true;
        }
        return false;

    }
}

Solution

  • Please check the MaxPermSize settings including min and max settings eg:-Xms512M -Xmx756m and try increasing those. (if its 256 change to 512 etc)