简体   繁体   English

Hibernate query.list()需要很长时间才能响应

[英]Hibernate query.list() taking long time to respond

I am working on a REST server which accepts and returns JSON data. 我正在使用REST服务器,该服务器接受并返回JSON数据。 For a particular input values(from input JSON) Hibernate (integrated with Spring) query the database and send JSON back to Rest Controller. 对于特定的输入值(来自输入JSON),Hibernate(与Spring集成)查询数据库并将JSON发送回Rest Controller。 Client application is designed to have timeout of 5 sec, for reading response, which cannot be altered. 客户端应用程序的读取响应超时时间为5秒,无法更改。

Now the problem is sometimes (not always but mostly) the Hibernate is unable to process data within given time limit thus time out error is received on client side. 现在的问题是(有时(并非总是但主要是)休眠)无法在给定的时限内处理数据,因此在客户端接收到超时错误。

I checked with other post on net to use full constructor in bean classes and to have lazy loading. 我与网上的其他帖子一起检查过,以在bean类中使用完整的构造函数并进行延迟加载。 Both are true in my case. 就我而言,两者都是正确的。 Below is one of the DAOImpl method causing this problem. 下面是导致此问题的DAOImpl方法之一。 Here i have to query 2 tables (which doesn't have much data, approx 20 entries in each table), add all data to a json array and send back. 在这里,我必须查询2个表(没有太多数据,每个表中大约有20个条目),将所有数据添加到json数组中并发送回去。

@Override
   public String getOfferOnRuleNameBalance(String inputJsonString) {
    JSONObject saveJsonObject = new JSONObject(inputJsonString);
    String ruleName    =   saveJsonObject.getString("rulename");
    int currentAccountBalance    =   saveJsonObject.getInt("currentAccountBalance");
    Session session1 = getSession();
    Criteria criteria =session1.createCriteria(PaymentPlanOffers.class);
    criteria.add(Restrictions.eq("rulename", ruleName));
    @SuppressWarnings("unchecked")
    List<PaymentPlanOffers> offerList=criteria.list();
    JSONArray jsonArray = new JSONArray();
    for(PaymentPlanOffers object:offerList)
    {
        JSONObject jsonObject1 =new JSONObject();
        jsonObject1.put("instAmount",object.getAmountPercent());
        jsonObject1.put("instNumber", object.getNumInstallment());
        jsonObject1.put("frequency", object.getFrequency());
        jsonObject1.put("offerId", object.getId());
        jsonObject1.put("offerName", object.getOfferName());
        jsonObject1.put("active", object.isActive());
        jsonObject1.put("accepted", object.isAccepted());
        jsonObject1.put("totalAmount", currentAccountBalance);
        jsonObject1.put("startDate", object.getStartDate());
        jsonObject1.put("endDate", object.getEndDate());
        jsonArray.put(jsonObject1);
    }

    Criteria criteria2 =session1.createCriteria(CustomPlanOffer.class);
    criteria2.add(Restrictions.eq("rulename", ruleName));
    @SuppressWarnings("unchecked")
    List<CustomPlanOffer> customOfferList=criteria2.list();
    for(CustomPlanOffer object:customOfferList)
    {
        JSONObject jsonObject1 =new JSONObject();
        jsonObject1.put("instAmount", object.getAvgInstallment());
        jsonObject1.put("instNumber", object.getNumOfInstallment());
        jsonObject1.put("frequency", object.getFrequency());
        jsonObject1.put("offerId", object.getId());
        jsonObject1.put("offerName", object.getName());
        jsonObject1.put("active", object.isActive());
        jsonObject1.put("accepted", object.isAccepted());
        jsonObject1.put("totalAmount", object.getTotalPaymentAmount());
        jsonObject1.put("startDate", object.getStartDate());
        jsonObject1.put("endDate", object.getEndDate());
        jsonArray.put(jsonObject1);
    }

    JSONObject mainObj = new JSONObject();
    mainObj.put("allOffers", jsonArray);
    session1.close();
    return mainObj.toString();
}

Please let me know if i have implemented it in correct way. 请让我知道我是否以正确的方式实施了它。

EDIT : Posting another method causing similar issue with all modifications done 编辑:发布另一种方法,完成所有修改后会导致类似问题

@Override
public String getAllOffers(String inputJsonString) {
    Session session = getSession();
    Transaction t = session.beginTransaction();
    String hql = "FROM PaymentPlanOffers";
    Query query = session.createQuery(hql);
    @SuppressWarnings("unchecked")

    List<PaymentPlanOffers> results = query.list(); //this is where it goes on hang


    JSONArray jsonArray = new JSONArray();
    for(PaymentPlanOffers object:results)
    {
        JSONObject jsonObject1 =new JSONObject();
        jsonObject1.put("offername", object.getOfferName());
        jsonObject1.put("rulename", object.getRulename());
        jsonObject1.put("id", object.getId());
        jsonObject1.put("offerMessage", object.getOfferMessage());
        jsonArray.put(jsonObject1);
    }
    JSONObject mainObj = new JSONObject();
    mainObj.put("allOffers", jsonArray);
    t.commit();
    session.close();
    return mainObj.toString();
}

The first thing I would do is to log the SQL queries being generated ( see here ). 我要做的第一件事是记录正在生成的SQL查询( 请参阅此处 )。 Once you have them you can do some more digging: 拥有它们后,您可以进行更多挖掘:

  • Are you sure the database itself isn't the problem? 您确定数据库本身不是问题吗? Bad table and/or join indexes are a common cause. 错误的表和/或联接索引是常见的原因。
    • Log the start and end time of getOfferOnRuleNameBalance 记录getOfferOnRuleNameBalance的开始和结束时间
    • Log the start and end time of when you do each query 记录每次查询的开始和结束时间
    • Check to see if generated SQL queries perform poorly when manually run against the database 检查手动对数据库运行时生成的SQL查询是否性能不佳
  • Are your hibernate mappings and code causing an N+1 select? 您的休眠映射和代码是否导致N + 1选择? You will know if there are multiple SELECT statements generated for each item in a lazy loaded collection. 您将知道在延迟加载的集合中是否为每个项目生成了多个SELECT语句。 This could also be caused by a loop somewhere, or caused by an equals() , toString() , or hashCode() method accessing the lazy loaded collection and checking each element in it. 这也可能是由于某个地方的循环引起的,或者是由访问惰性加载的集合并检查其中的每个元素的equals()toString()hashCode()方法引起的。

Logging each query will also tell you if it's only one that's causing the problem or if it's both queries. 记录每个查询还会告诉您是仅一个原因引起的问题还是两个查询。

For more troubleshooting I would have to see the mappings for the hibernate objects themselves but this may at least give you a starting point. 有关更多故障排除,我将必须查看休眠对象本身的映射,但这至少可以为您提供一个起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM