1

Good day everyone,I have Java application that use JPA (EclipseLink) for database access-some CRUD operation.How i can make synhronization in that case ?
I mean if two users User1 and User2 start application on they machine and User1 change some records how to make User2 see it ? Is there some opportunity make User2 know that User1 change record and update only that record ?.
The same problems has discussed here
How to synchronize multiple clients with a shared database (JPA)?.
Updated data (in Database) is not visible via JPA/Eclipselink
But the only what there suggest is to update by timer. Is it common way to do such things ?
Thank you for your help
[EDIT]
Monitor MySQL inserts from different application
How to make a database listener with java?
change notification on domain objects (Hibernate/Java)
Show me direction in resolving my problem .Hope can help somebody.

Community
  • 1
  • 1
user1722669
  • 488
  • 2
  • 6
  • 22

1 Answers1

1

You should create a new entity Manager instance for each transaction. I suggest to use spring with a JTA Transaction manager and let the container manage the entity manager scope.

See http://spring.io/blog/2011/08/15/configuring-spring-and-jta-without-full-java-ee/

[edit]

Note that if there is a refreh(someEntity) method on the EntityManager, there is no refreshAll() method. This is because the EM is not designed to last a long time and be refreshed.

If you let the container (Spring is advised for a standalone app) manage the persistence context (container managed entityManager), it will instantiate a new EM for each transaction. In other words, each time you invoke a method annotated with @transactional annotation, a new EM will be instantiated for the lifecycle of the method.

In this case you don't need to take care about data synchronization, each time you want the grid to be refreshed you recall the transactional getMyEntityList() method which will retrieve a new fresh set of entities to display in the grid. You can of course use a timer to trigger the refresh.

The trick is to never let unpersisted modification in memory. Each time a user update the grid, open a new transaction and persist the modification, each time you refresh, retrieve a new up-to-date persistence context and let the GC dispose the old unreferenced entities.

If you don't want user1 to be able to override user2 data, configure optimistic locking.

Otherwise if you absolutely want to maintain an application scoped EM for performance reason (avoiding to regularly retrieve data for DB), you can set up a messaging topic for the different application instance to notify each others in case of data update, but this gonna lead to additional work and constraints.

Gab
  • 7,869
  • 4
  • 37
  • 68
  • See http://stackoverflow.com/questions/22772980/struggling-to-understand-entitymanager-proper-use/22773758#22773758 for some generality about EM – Gab Apr 11 '14 at 19:18
  • Thank you for your reply and links. As i understand all this things make EM synhronization only when User made CRUD operation, but what if User2 see some records in Swing/JTable for example, and in User1 in that moment update that records.How to make User2 EM know that records was updated.The timer updater the only what i think about, is there any other tool/pattern/idea/something else how i can make it ? – user1722669 Apr 12 '14 at 09:09