Getting large lists of objects using Java EE

Is there a common way to return a large list of objects using Java EE?

For example, if you had a ResultSet database with millions of objects, how would you return these objects to a (remote) client application?

Another example - this is closer to what I'm actually doing - is to collect data from hundreds of sources, normalize them and gradually transfer them to the client system as one "list".

Since all data cannot fit in memory, I thought that combining the SessionBean state with the state and some kind of custom Iterator that accessed the server would do the trick.

So, in other words, if I have an API, for example Iterator<Data> getData(), then what is a good way to implement getData()and Iterator<Data>?

How did you successfully solve this problem in the past?

+3
source share
3 answers

Definitely do not duplicate the entire database in Java memory. This makes no sense and only makes things unnecessarily slow and clogs memory. Rather, enter pagination at the database level. You should request only the data that you really need to display on the current page, for example, how Google does it.

If it is really difficult for you to do this correctly and / or calculate the SQL query for a specific database, see this answer . For the JPA / Hibernate equivalent, check out this answer .


( ...), () :

List<Source> inputSources = createItSomehow();
Source outputSource = createItSomehow();

for (Source inputSource : inputSources) {
    while (inputSource.next()) {
        outputSource.write(inputSource.read());
    }
}

, Java , ():

List<Source> inputSources = createItSomehow();
List<Entry> entries = new ArrayList<Entry>();

for (Source inputSource : inputSources) {
    while (inputSource.next()) {
        entries.add(inputSource.read());
    }
}

Source outputSource = createItSomehow();

for (Entry entry : entries) {
    outputSource.write(entry);
}
+2

Pagination - - ui. , , . rmiio .

+1

, , . , , , OutOfMemoryException.

, , , , . , 1 , . , . (. BalusC)

, , , , OutOfMemory.

Also note: it’s normal to load millions of objects from a database as an administrative task: for example, to back up and export some “exceptional” case. But you should not use it as a request that any user can make. It will be slow and drain server resources.

0
source

Source: https://habr.com/ru/post/1783721/


All Articles