【发布时间】:2017-03-16 05:35:30
【问题描述】:
我正在开发一个接受并返回 JSON 数据的 REST 服务器。对于特定的输入值(来自输入 JSON),Hibernate(与 Spring 集成)查询数据库并将 JSON 发送回 Rest Controller。客户端应用程序被设计为具有 5 秒的超时时间,用于读取响应,该响应不可更改。
现在的问题是有时(并非总是但大多数情况下)Hibernate 无法在给定的时间限制内处理数据,因此在客户端收到超时错误。
我检查了网络上的其他帖子,以在 bean 类中使用完整的构造函数并进行延迟加载。就我而言,两者都是正确的。下面是导致此问题的 DAOImpl 方法之一。在这里我要查询 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();
}
如果我以正确的方式实施它,请告诉我。
编辑:发布另一种导致所有修改完成的类似问题的方法
@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();
}
【问题讨论】:
标签: java mysql json spring hibernate