Search code examples
grailsgrails-ormhibernate-criteria

Grails list() - handling Out of memory and restricting list results


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.

Solution

  • 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.