如何配置Tomcat(Catalina)来部署Web应用程序而无需重新启动服务器?

时间:2014-12-24 08:24:12

标签: java tomcat memory deployment catalina

每次尝试重新部署应用程序而不重新启动服务器时,我都会收到PermGen error,当我尝试关闭服务器时,我会收到:

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.
  

编辑1:

抱歉,忘记添加我的java代码了。这里我使用的是Hibernate + Spring(WebApplicationInitializer),只有当我使用它时才会出现错误。

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;

    }
}

2 个答案:

答案 0 :(得分:0)

请检查MaxPermSize设置,包括最小和最大设置,例如:-Xms512M -Xmx756m并尝试增加它们。 (如果它的256改为512等)

答案 1 :(得分:0)

如果你转到yourTomcatFolder/conf/server.xml

<Host>标记中设置属性autodeploy="true",然后tomcat将重新部署您的应用,而无需重新启动您的服务器。