0

I am facing following problems in Grails.

Here is my code in which I am trying to get list of all possible data and display it.

def c = Abc.createCriteria()
def results = c.list(){
  eq("A", "a")
  eq("B", "b")    
}

As the results is huge it goes on fetching them and dies. I want to restrict the list fetch or put max size for it.

I tried using maxResults() but it act as a late filter.

I want to put a

  1. timeout
  2. restrict the fetch
  3. if possible, handle all data without hitting Out of memory error.
tim_yates
  • 167,322
  • 27
  • 342
  • 338
Amy PrIce
  • 47
  • 1
  • 7

2 Answers2

0

I'm not sure what you mean by a "late filter", but as far as I know the following in a criteria query

maxResults(10)

is identical to

LIMIT 10

in an SQL query. You can verify this by turning on SQL logging. In other words maxResults(X) will limit the number of records returned by the query itself.

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • Hi Donal, by late filter I meant may be it getting all list of data and then picking max of it which is specified by maxResults(x). I even tried the same way but it ends up with out of memory issue. – Amy PrIce Jun 12 '14 at 12:22
  • I want to restrict the huge data before it gives an outofmemory or timeout error. Or in any way I can handle these data in list() as shown in example. – Amy PrIce Jun 12 '14 at 12:27
  • @AmyPrIce as I said in my answer, `maxResults` does not perform what you call a "late filter". It restricts the size of the results that are returned by the query itself – Dónal Jun 12 '14 at 14:59
  • I understand that, but still not sure why the query is getting timed out when we have to search for huge files even thought I have given maxResults(10) as 10 only. – Amy PrIce Jun 12 '14 at 17:13
0

You can use pagination then

params.max = Math.min(params.int('max') ?: 10, 100)
def c = Abc.createCriteria()
def results = c.list(params){
    eq("A", "a")
    eq("B", "b")    
}

view:

<g:paginate controller="myController" action="myAction" total="${results.totalCount}" />
MKB
  • 7,587
  • 9
  • 45
  • 71