Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

desktop and jpa and domain driven development (DDD)

tbee-JavaNetMay 15 2007
Ok, I'm trying to do some DDD alike coding. That means I have a totally independant business model (BM) jar, containing entities, persistence and business logic.

In order to encapsulate and reuse as much code as possible, the BM also contains some static "find" methods. These methods require access to the EntityManager (EM) to execute a query. But in the future there maybe other business logic (use cases) as well, that will also require direct access to the EM.

The problem that I'm facing is how to provide access to the EM at any point in the BM.

Initially this seems quite simple: a singleton. However, the swing application in question may have multiple instances of the same frames open. Each frame can contain unpersisted changes to the BM. Upon a button click, these changes are either persisted or cleared (by means of a EM.clear call). Since a clear in one frame may not clear the changes in another, every frame must have its own EM. So a singleton is not an option.

Thread related then? Well, since all GUI actions are performed within the EDT, this will not work.

Focus based? Using KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow() one can determine the active frame and thus the associated EM.

However, as soon as the EDT spawns a thread for long running tasks, this will no longer work, since the focus may move to another frame, while the thread is running. It is possible to use a combination and associate the spawned thread with the at-the-moment-of-spawning valid EM.

Bit it gets even more complex when the spawned thread re-inserts code into de EDT by means of an invokeLater. Then it IMHO is no longer possible to find out which thread inserted the runnable.

All in all a complex problem. Any suggestions?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 12 2007
Added on May 15 2007
0 comments
130 views