I've just started doing some Java EE projects right now I'm trying to update my existing auto complete field. I'm using Primeface for JSF, and I'm using JPA.
My auto complete is working fine. My problem now is that the records have grown to a million and with that my current code is now producing an out of memory/heap space problem due to the large List generated.
I generate the List on ejb with @Startup since the data does not change. It just usually grow when we add more data directly to the database. Also when I try loading this to a Managed Bean with a @Postconstruct I get the out of memory faster.
I'm using this to populate my list in the ejb
private List<TranslationAutoComplete> translations;
this.translations = em.createQuery("SELECT NEW com.sample.model.TranslationAutoComplete(t.id, t.entry) FROM Translation t ORDER BY t.entry ASC", TranslationAutoComplete.class).getResultList();
With that what can be the better structure for me to efficiently handle this without producing some memory/ heap space problem? I've read about Memcache? and other non java core Colletions but haven't tried it yet. Are these the better solution? Or is there a more efficient way in doing this in Java EE.
You have to limit the query results. You can achieve this with the setMaxResults(int maxResults)
for your Query. If you do this:
this.translations = em
.createQuery("SELECT NEW com.sample.model.TranslationAutoComplete(t.id, t.entry)
FROM Translation t ORDER BY t.entry ASC", TranslationAutoComplete.class)
.setMaxResults(10).getResultList();
You will get only 10 results. If the user keeps inputting data on your autocomplete, then more results will appear, making it a lot more efficient. Cheers.