Amir tagjvaii
Amir tagjvaii

Reputation: 33

Can not run some time SQL query in Hibernate

This code is used to display the number of days sold by the month

public long getCountsOfQuery(String condition, Class outputclass) throws Exception {
  Session session = BuilderSession.getSession();
  return Long.parseLong(session.createSQLQuery(condition).uniqueResult().toString());
}

My JSP file is :

<td><%= ho.getCountsOfQuery("select COUNT(*) from store_sales where date like '"+ fd.getPartDate(FormatDate.PART_YEAR)+fd.getPartDate(FormatDate.PART_MONTH)+"01"+"' ", object.getClass())  %></td>
<td><%= ho.getCountsOfQuery("select COUNT(*) from store_sales where date like '"+ fd.getPartDate(FormatDate.PART_YEAR)+fd.getPartDate(FormatDate.PART_MONTH)+"02"+"' ", object.getClass())  %></td>
....
<td><%= ho.getCountsOfQuery("select COUNT(*) from store_sales where date like '"+ fd.getPartDate(FormatDate.PART_YEAR)+fd.getPartDate(FormatDate.PART_MONTH)+"31"+"' ", object.getClass())  %></td>

After two runs cannot response query in hibernate.
Also, no errors occur at runtime. Please help me.

Upvotes: 0

Views: 65

Answers (1)

Antoniossss
Antoniossss

Reputation: 32535

Its because you are not releasing connection.

  Session session = BuilderSession.getSession();
  Long result=Long.parseLong(session.createSQLQuery(condition).uniqueResult().toString());
 session.close();
return result;

Obviously you should wrap it in try-catch and close in finally block, but in general it shows you what you are missing.

Upvotes: 2

Related Questions