I need help in the following design. I expose a web method that does an SQL select from a database. The problem is that the number of records can be huge and I don’t want to return all of the records in a single call.
So I can think of these options (to return the result in pages):
1) Provide a method with parameters so that the client requests recordStart and recordEnd each time.
2) Modify the method to accept a resultset of size X and somehow understand that each request is not a new one but return the next X records. To figure this out somekind of a token could be associated per client but the problem is I am not sure how long should this token be kept and then disposed of so as to treat an incoming request either as a first request or a continuation of a previous one.
So which design should I go for and how would I solve any relevant problems I mention?
Are there better ways to deal with these problems?
From my point of view:
Your interface to perform the selection on database will be similar to:
This approach turns easy the extension of your paging method, imagine in a few months you need to implement a sort to that method, you will need to change each invocation to it. With this approach you change the
PageRequestobject and gives a default sort, nothing will be broken and you can customize sort just in the invocation that really needs it.Within this method you will need two different DataBase selections:
topfor sybase,limitfor mysql and PG,rownumwith Oracle, this will vary from one database to other);A good reference for your problem would be Spring Data, they have Page and PageRequest that is more or less what you need. Maybe you could use their API to implement your solution.
Practically your request object could looks like:
Of course you could play a bit with Generics too in order to have response holding types you already requested, facilitating use of the response object like:
having the objects for Request and Response like: