• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Querying with JPA, returning all...Good or Bad?

 
Enrique Castro
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a question for you, somebody told me that is not a good practice
when you have an EJB to use the method findAll() cause it makes the server
gets down when it returns a big amount of entities however i want to make sure
that this might be a possible issue for this to happen.
I think that if netbeans automatically generates that method in a Facade it is because
it supports that functionality and must work indistinctly the amount to be returned....
and i would think that if the server gets down when returning a high amount of data
through this method it should happen because of a server missconfiguration.

However i want to really be aware if it is NOT a good practice to return ALL of the entities
or if there is NO issue to return all of the entities even when it is supposed to return a high amount of data.

Thanks in advance.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have ten rows in your database, it's not an issue. If you have ten million rows in your database..er...you might run into a problem.

I had even a small database with a few thousand records in it, and when I did a test with a findAll, I could hear my hard drive just moaning, and that was just a simple sample database.

Personally, I like to hand off DAOs to people, and when I do, the findAll method requires a start index and the number of rows to return:



That way, the developer is forced to think about it. When they're forced to think about it, even when they don't care, they never put a number in greater than 100, because they know that they're not likely going to be doing much with more than 100 records. And if they need more, they can just do another query for 100 records, using a startIndex of 100.

Make sense? It's about balance.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enrique,
It also depends on what you are doing with the data. If you don't need it all, then returning it is unnecessary overhead. If you do need it and you have thousands/millions of records, you want to use paging as Cameron indicated rather than return it in one go so you don't have memory issues.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic