1

I am trying to update a singleton object ( which is loaded by spring on application startup using the bean definition ) . This object is however loaded with entries from a table in a database ( MySQL ).

This table can be updated from time to time through the application.

Is it possible to update the singleton object using spring and hibernate when a row in the database is updated ?

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
anurupr
  • 2,294
  • 2
  • 20
  • 28
  • 1
    If the data it contains is small enough to be kept in memory, loading the state from the database every time it's needed shouldn't be a problem. You can always use the 2nd-level cache if really needed. – JB Nizet Sep 28 '14 at 12:03
  • I don't understand your meaning by saying "table can be updated through the application", is the application means other function from same vm? Or is it means another vm or host may write the table at same time? If the changes comes from another vm or host, your current object is already dirty. You'll have to load them from database everytime to keep it same to database, as @JBNizet suggested. – Doug Hou Sep 28 '14 at 14:22
  • I'm required to update the table through the application using other functions on the same vm. – anurupr Sep 28 '14 at 19:17

1 Answers1

1

You can use Hibernate event listeners:

  1. Check how to add event listeners to your Session Factory.
  2. You can listen on entity persist/merge/flush events. Because the event listener is a Spring object you can inject the reference to your singleton.
  3. Once you intercept the wanted event, you can update the singleton state with the latest updated data.
Community
  • 1
  • 1
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • Which will only work in a single application. If you have multiple application modifying the same data you won't get those events. – M. Deinum Sep 29 '14 at 09:53
  • The question says "This table can be updated from time to time through the application." – Vlad Mihalcea Sep 29 '14 at 09:58
  • With PostgreSQL you can get [database notifications](http://www.postgresql.org/docs/9.3/static/sql-notify.html) but he's using MySQL instead. Another workaround is to use JMS to link events and the singleton update. – Vlad Mihalcea Sep 29 '14 at 09:59
  • A database notification is still not an event you can handle with hibernate or jpa events and if I'm not mistaken requires work on the database side (you will need some code to trigger the `NOTIFY`). If it is a single application one could still use the 2nd level cache as that could be either updated automatically or you could invalidate the element in the cache. No need to hack around with an event listener and try to figure out the correct object. – M. Deinum Sep 29 '14 at 10:11
  • We are on the seam wave-length. A distributed 2nd level cache will work if it supports custom callbacks. Oracle supports [advance queueing](http://docs.oracle.com/cd/B10500_01/appdev.920/a96587/qintro.htm) but for MySQL I'd probably go for a [CQRS architecture](http://martinfowler.com/bliki/CQRS.html). – Vlad Mihalcea Sep 29 '14 at 10:15