I've data inside a DB which have hierarchical relationship (might be symbolized like a tree).
At this time, each time a request is available in the server, a recursive totally completed in the DB to produce an in-memory list (the industry branch from the tree, from leaf to root that really is available) and do processing by using their list.
This really is not so efficient, but a minimum of it really works for the time being, and it has the benefit when the DB is transformed, the alterations will get immediately observed.
Anyway, I wish to improve it and searching for a pattern to:
1)Not have to gain access to the DB constantly
2)If your change is completed in the DB, it will get immediatelly reflected
3)You can use it in most places from the server.
It is possible to standard design pattern generally employed for cases such as this? Or simply possess a data structure maintained with a after sales thread as well as on any update within the DB, reload my way through the information structure?


Rather than a thread you could utilize a caching solution. Caused by the query could be cached and came back on every request, if the tables within the query are modified you invalidate the cache, hold back until the following request to retrieve the brand new values and load them around the cache. This solution meets requirements 1 and a pair of. Exactly what do you mean by "You can use it in most places from the server"?

If you work with Hibernate, this is often easily completed with the second level cache, the caveat is you must guarantee that only the application changes the database (no exterior processes).

If you're not using Hibernate, you are able to roll your personal solution with Ehcache. You should alter the method that returns the dwelling to determine the cache before querying the DB. Techniques which make changes towards the tables involved, would need to modified to invalidate the cache.

Ehcache also offers integration with Spring, but I have never attempted it.

You do not give much particulars which ORM tool you utilize, however in Hibernate, this really is possible by proclaiming a listing of kids. Below is obtained from the development code:

   <class name="YourTreeClass" table="tree_table" dynamic-insert="true" dynamic-update="true">
   ...other properties...

      <list name="children" cascade="save-update" inverse="false" >
         <cache usage="nonstrict-read-write"/>
         <key column="parent_id"/>
         <one-to-many class="YourTreeClass" />

No requirement for hacky background threads - on node update, Hibernate takes care and invalidates relevant cached records.