Note that there are some explanatory texts on larger screens.

plurals
  1. POSession management using Hibernate in a *multi-threaded* Swing application
    text
    copied!<p>I'm currently working on a (rather large) pet project of mine , a Swing application that by it's very nature needs to be multi-threaded. Almost all user interactions might fetch data from some remote servers over the internet , since I neither control these servers nor the internet itself, long response times are thus inevitable. A Swing UI obviously cannot repaint itself while the EDT is busy so all remote server calls need to be executed by background thread(s).</p> <p>My problem:</p> <p>Data fetched by the background threads gets 'enriched' with data from a local (in-memory) database (remote server returns IDs/references to data in the local database). This data later eventually gets passed to the EDT where it becomes part of the view model. Some entities are not completely initialized at this point (lazy-fetching enabled) so the user might trigger lazy-fetching by e.g. scrolling in a JTable. Since the hibernate session is already closed this will trigger a LazyInitializationException. I can't know when lazy-fetching might be triggered by the user so creating a session on demand/attaching the detached object will not work here.</p> <p>I 'solved' this problem by:</p> <ul> <li>using a single (synchronized , since Session instances are not thread-safe) Session for the whole application </li> <li>disabling lazy-fetching completely</li> </ul> <p>While this works, the application's performance has suffered greatly (sometimes being close to unusable). The slowdown is mainly caused by the large number of objects that are now fetched by each query.</p> <p>I'm currently thinking about changing the application's design to 'Session-per-thread' and migrating all entities fetched by non-EDT threads to the EDT thread's Session (similar to <a href="https://forum.hibernate.org/viewtopic.php?f=1&amp;t=942695&amp;start=0" rel="noreferrer">this posting on the Hibernate forums</a>).</p> <p>Side-note: Any problems related to database updates do not apply since all database entities are read-only (reference data).</p> <p>Any other ideas on how to use Hibernate <em>with</em> lazy-loading in this scenario ? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload