2013-08-22 31 views
0

我的web服務是在jersy(restful java)中製作的,我使用Hibernate與mysql數據庫進行通信。我必須重新啓動我的tomcat服務器以恢復我的web服務每天早上

在一天的時間我執行負載測試和其他功能測試,每一件事情的作品,我們離開了我們的工作,下午5點左右在晚上,當我們再次第二天早上通過HTTP 500例外測試Web服務我的web服務,當我們重新啓動它託管的tomcat,我的Web服務恢復,而無需對源代碼或hibernate配置文件進行任何更改。

我使用同一個Hibernate的配置文件,該文件是被其他網絡服務,其工作正常

這裏是我的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; 
    } 

} 
+0

供應商如何提供實例化,看起來像你有你的粗俗線程問題 – NimChimpsky

+0

我不明白你想知道什麼,據我所知,我已經創建單身類從我得到vendorCRUD對象 – focode

+0

我已經做了很多負載測試,從JMeter進行負載測試時沒有問題,只有當我們在晚上離開工作時纔會出現問題,並且在早上我們嘗試點擊服務,它說http 500例外 – focode

回答

0

如果此行拋出一個NPE:

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

是手段一件事,一件事。當NPE被拋出時,vendorCrud的值爲null

這是不可能說這是怎麼發生的。但是,我會注意到vendorCrud是可變的,並且它可以在與VendorOffers類相同的包中被任何類訪問。所以有潛在的很多普通的Java代碼可以對其進行更改。再有就是有些事情是調用init()方法的可能性...


根據您的後續評論,我的猜測是,在東西你堆棧不處理過期的MySQL連接。默認情況下,MySQL會斷開長時間閒置的連接(我認爲默認值爲10小時)。解決這個問題的一種方法是將超時時間改爲非常大的時間。有一個數據庫端配置參數。然而,最好找出/爲什麼這會導致​​引用...並解決這個問題。

這裏的關鍵是,你需要瞭解發生了什麼,通過應用 Java編程和調試技能的問題。

+0

我總是使用Singleton類來獲取我的所有對象CRUD類是這樣做的正確的方法,這可能是問題 – focode

+0

它的東西得到過期,我已經驗證了我的休眠和數據庫他們是沒有問題的,問題在於我的服務,問題只發生在我們離開長時間應用6-7小時 – focode

+0

我非常感謝您的反饋,我已經將interactive_timeout和wait_timeout的持續時間從8小時增加到了11天,並在我的hibernate配置文件中提供了處理它的條款,我的這個特定的Web服務由兩個服務組成1.從客戶端和數據庫中獲取數據2.基於來自1個服務的數據,我的第二個服務爲同一個客戶端產生一些信息。當我在我的第二個服務中看到錯誤時(與上面一樣)我能夠執行我的第一個服務而沒有任何問題。 – focode

相關問題