java - 我必须重新启动我的 tomcat 服务器才能每天早上恢复我的网络服务

标签 java web-services hibernate tomcat

我的网络服务是用 jersy(restful java)制作的,我使用 Hibernate 与 mysql 数据库进行通信。

白天我执行负载测试和其他功能测试,一切正常,我们在晚上 5 点左右离开工作,当我们第二天早上通过 http 500 异常再次测试我的 Web 服务时,当我们重新启动托管它的 tomcat,我的 Web 服务在不对源代码或 hibernate 配置文件进行任何更改的情况下恢复。

我正在使用相同的 Hibernate 配置文件,该文件已被其他工作正常的 Web 服务使用

这是我的 hibernate 配置文件:

    <?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory> 
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/offerstest</property> 
<property name="hibernate.connection.username">root</property> 
<property name="show_sql">true</property> 
<property name="connection.password">password</property> 
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property> 
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.jdbc.batch_size">20</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
<property name="hbm2ddl.auto">update</property>
<property name="dynamic-update">true</property>
<mapping resource="person.hbm.xml"/>
<mapping resource="vendor.hbm.xml"/>
<mapping resource="coupons.hbm.xml"/>
<mapping resource="binding.hbm.xml"/>
</session-factory> 
</hibernate-configuration>

这是我的 web 服务类,其中出现 Http 500 错误

   @Path("/offers")
public class VendorOffers {

    VendorCRUD vendorCRUD;
    BindingCRUD bindingCRUD;

    public VendorOffers() {
        // TODO Auto-generated constructor stub
    }

    @PostConstruct
    public void init() {
        vendorCRUD = CommunicationSingleton.getInstance().getVendorCRUD();
        bindingCRUD = CommunicationSingleton.getInstance().getBindingCRUD();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    // @Produces(MediaType.APPLICATION_XML)
    @Path("{mac}/coupons")
    public Response getOffers(@PathParam("mac") String mac) {

        List<Offer> list = new ArrayList<Offer>();
        // get all the offers (Bindings) for the given mac
        List<Binding> bindings = bindingCRUD.getMACOffers(mac);
        if (bindings != null) {

            for (Binding binding : bindings) {
                Offer offer = new Offer();

                offer.setCoupon_no(binding.getCoupon_no());
                offer.setMac(binding.getMac());
                offer.setStartDate(binding.getStartdate());
                offer.setEndDate(binding.getEnddate());
                long vendorid = binding.getVendor_coupon_id();
                Vendor vendor = new Vendor();

                vendor.setApk("/rest/downloadfile/" + vendorid + "/getapk");// from
                                                                            // service
                vendor.setArtwork1("/rest/downloadfile/" + vendorid
                        + "/getArt1");// from service
                vendor.setArtwork2("/rest/downloadfile/" + vendorid
                        + "/getArt2");// from service
                vendor.setIcon("/rest/downloadfile/" + vendorid + "/getIco");// from
                                                                                // service
                vendor.setRedeem_doc("/rest/downloadfile/" + vendorid
                        + "/getHtm");// from service




                com.learning.CRUDModel.Vendor localvendor = vendorCRUD
                        .getVendorById(vendorid);

                System.out.println("localvendor.getCountry()"+localvendor.getCountry());
                vendor.setCountry(localvendor.getCountry()); // from database
                vendor.setDescription(localvendor.getDescription());// from
                                                                    // database
                vendor.setHeading(localvendor.getHeading());// from database
                vendor.setName(localvendor.getName());// from database
                vendor.setPkg_name(localvendor.getPkg_name());// from database
                vendor.setVendor_coupon_id(vendorid);// from database
                offer.setVendor(vendor);
                list.add(offer);
            }

            GenericEntity<List<Offer>> entity = new GenericEntity<List<Offer>>(list) {};
            Response response = Response.ok(entity).build();
            return response;
        } else {
            Response response = Response.noContent().build();
            return response;
        }

    }
}

产生的错误如下:

    Aug 22, 2013 10:07:02 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
    at com.avilyne.rest.resource.VendorOffers.getOffers(VendorOffers.java:71)
    at sun.reflect.GeneratedMethodAccessor106.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1483)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

第 71 行是这样写的:

com.learning.CRUDModel.Vendor localvendor = vendorCRUD
                        .getVendorById(vendorid);

这里是 vendorCRUD 类:

public class VendorCRUD {
    Session session = HibernateUtil.getSession();

    public String getVendorApk(String vendor_id){
        long id = 0;
        try {
            id = Long.parseLong(vendor_id);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        String apk_path = "not available";

        System.out.println("Vendor Id Value:" + vendor_id);
        try {
            Query query = session.createQuery("from Vendor where Vendor_Coupon_id = :Vendor_Coupon_id ");
            query.setParameter("Vendor_Coupon_id",id);
            List <Vendor>list = query.list();
            if (list.size() > 0) {

                apk_path = list.get(0).getApk();

            }
        } catch (HibernateException ex) {
            ex.printStackTrace();
        } catch(Exception ex){
            ex.printStackTrace();
        }

        return apk_path;
    }

    public String getVendorgetArt1(String vendor_id){
        long id = 0;
        try {
            id = Long.parseLong(vendor_id);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        String art1_path = "not available";
        System.out.println("Vendor Id Value:" + vendor_id);
        try {
            Query query = session.createQuery("from Vendor where Vendor_Coupon_id = :Vendor_Coupon_id ");
            query.setParameter("Vendor_Coupon_id", id);
            List <Vendor>list = query.list();
            if (list.size() > 0) {
                art1_path = list.get(0).getArtwork1();
            }
        } catch (HibernateException ex) {
            ex.printStackTrace();
        } catch(Exception ex){
            ex.printStackTrace();
        }


        return art1_path;
    }


    public String getVendorgetArt2(String vendor_id){
        long id = 0;
        try {
            id = Long.parseLong(vendor_id);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        String art1_path = "not available";
        System.out.println("Vendor Id Value:" + id);
        try {
            Query query = session.createQuery("from Vendor where Vendor_Coupon_id = :Vendor_Coupon_id ");
            query.setParameter("Vendor_Coupon_id", id);
            List <Vendor>list = query.list();
            if (list.size() > 0) {
                art1_path = list.get(0).getArtwork2();
            }
        } catch (HibernateException ex) {
            ex.printStackTrace();
        } catch(Exception ex){
            ex.printStackTrace();
        }
        return art1_path;
    }

    public String getVendorgetIco(String vendor_id){
        long id = 0;
        try {
            id = Long.parseLong(vendor_id);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        String ico_path = "not available";
        System.out.println("Vendor Id Value:" + vendor_id);
        try {
            Query query = session.createQuery("from Vendor where Vendor_Coupon_id = :Vendor_Coupon_id ");
            query.setParameter("Vendor_Coupon_id", id);
            List <Vendor>list = query.list();
            if (list.size() > 0) {
                ico_path = list.get(0).getIco();
            }
        } catch (HibernateException ex) {
            ex.printStackTrace();
        } catch(Exception ex){
            ex.printStackTrace();
        }
        return ico_path;
    }

    public String getVendorgetHtm(String vendor_id){
        long id = 0;
        try {
            id = Long.parseLong(vendor_id);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        String htm_path = "not available";
        System.out.println("Vendor Id Value:" + vendor_id);
        try {
            Query query = session.createQuery("from Vendor where Vendor_Coupon_id = :Vendor_Coupon_id ");
            query.setParameter("Vendor_Coupon_id", id);
            List <Vendor>list = query.list();
            if (list.size() > 0) {
                htm_path = list.get(0).getRedeem_doc();
            }
        } catch (HibernateException ex) {
            ex.printStackTrace();
        } catch(Exception ex){
            ex.printStackTrace();
        }
        return htm_path;
    }

    public List<Vendor> getVendorsForCountry(String country){
        List<Vendor> vendors = null;
        try {
            Query query = session.createQuery("from Vendor where Country = :Country ");
            query.setParameter("Country", country);
            List <Vendor>list = query.list();
            if (list.size() > 0) {
                vendors = list;
            }
        } catch (HibernateException ex) {
            ex.printStackTrace();
        }catch(Exception ex){
            ex.printStackTrace();
        }

        return vendors;     
    }

    public Vendor getVendorById(long vendorid) {
        Vendor uniquevendor = null;
        try {
            Query query = session.createQuery("from Vendor where Vendor_Coupon_id = :Vendor_Coupon_id ");
            query.setParameter("Vendor_Coupon_id", vendorid);
            List <Vendor>list = query.list();
            if (list.size() > 0) {
                uniquevendor = list.get(0);
            }
        } catch (HibernateException ex) {
            ex.printStackTrace();
        } catch(Exception ex){
            ex.printStackTrace();
        }
        return uniquevendor;
    }

}

最佳答案

如果此行抛出 NPE:

    com.learning.CRUDModel.Vendor localvendor = vendorCRUD
                    .getVendorById(vendorid);

这意味着一件事,只有一件事。抛出 NPE 时,vendorCrud 的值为 null

不可能说这是怎么发生的。但是,我会注意到 vendorCrud 是可变的,并且与您的 VendorOffers 类在同一包中的任何类都可以访问它。所以可能有很多普通的 Java 代码可以改变它。那么有可能是某些东西正在调用 init() 方法......


根据您的后续评论,我的猜测是您的堆栈中的某些内容未处理过期的 MySQL 连接。默认情况下,MySQL 会断开长时间空闲的连接(我认为默认是 10 小时)。处理此问题的一种方法是将超时更改为非常大的值。为此有一个数据库端配置参数。但是,最好弄清楚这如何/为什么会导致 VendorCRUD 引用……并修复它。

这里的关键是,需要理解正在发生的事情,方法是将的 Java 编程和调试技能应用于问题。

关于java - 我必须重新启动我的 tomcat 服务器才能每天早上恢复我的网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18375750/

相关文章:

web-services - 所有子域的单一 SSL 证书

java - Tomcat-Spring-Hibernate Web 应用程序中的 'PermGen out of space' 异常可以做什么?

hibernate - 持久化设计的一般问题

java - 了解 EJB 和 Hibernate 上下文中的事务

java - 为什么 Java 中的 contains() 方法不能按预期工作?

java - PaintComponent() 方法未绘制到 JPanel

java - 无法让 MouseListener 在 Hexagon(生成的多边形)上工作

javascript - 类型错误不是 AngularJS 的函数

iphone - 如何从 iPhone 应用程序调用 Web 服务

java - HashMap 冲突会导致调整大小吗?