Exterminator13
Exterminator13

Reputation: 2182

EJB 3 & Swing: how to improve GUI responsiveness?

I'm making Swing database app based on EJB 3 technology. I'm using Netbeans 7.0.1. When program is starting up it's fetching all the data from database:

    private javax.persistence.Query spareQuery;
    private java.util.List<Spares> spareList;
    ...
    spareQuery = entityManager.createQuery("SELECT s FROM Spares s ORDER BY s.id");
    spareList = org.jdesktop.observablecollections.ObservableCollections.
                observableList(spareQuery.getResultList());

Fetching all the data from a database causes to significant pause in start-up process. For now, I need a wrapper for javax.persistence.Query interface which will do the following:

  1. Initialization:

    spareQuery = entityManager.createQuery("SELECT s FROM Spares s ORDER BY s.id");
    spareQuery = new MyQueryWrapper ( spareQuery );
    
  2. Main part! After that when this called:

    spareList = org.jdesktop.observablecollections.ObservableCollections.
                observableList(spareQuery.getResultList());
    

Instead of waiting all data received from the server, Query instance should split the query into chunks and after every chunk retrieved add data to the list (as list is observable, every portion of data will appear in associated JTable). As result, we'll have soft and fast start-up.

Thereby, logic of working should be like this:

  1. SELECT s FROM Spares s ORDER BY s.id WHERE s.id BETWEEN 1 and 20
  2. Add data to the list.
  3. ...
  4. SELECT s FROM Spares s ORDER BY s.id WHERE s.id BETWEEN 80 and 100
  5. Add data to the list.

QUESTION: Is there any library which can replace (wrap) EntityManager, Query or something else to achieve soft asynchronous data fetching from database using EJB3 technology?

Upvotes: 1

Views: 486

Answers (1)

Shivan Dragon
Shivan Dragon

Reputation: 15229

Why do you need a library for that. Just start your EntityManager instance and your Query execution in another thread and then bring the return values back to Swing's thread when they're available. You could use Swing Worker or ExecutorService to implement this but for such a simple task you might be better off just starting a thread with a callback.

Upvotes: 1

Related Questions