<p>Our design has one jvm that is a jboss/webapp (read/write) that is used to maintain the data via hibernate (using jpa) to the db. The model has 10-15 persistent classes with 3-5 levels of depth in the relationships.</p>

<p>We then have a separate jvm that is the server using this data.  As it is running continuously we just have one long db session (read only).  </p>

<p>There is currently no intra-jvm cache involved - so we manually signal one jvm from the other.</p>

<p>Now when the webapp changes some data, it signals the server to reload the changed data.  What we have found is that we need to tell hibernate to purge the data and then reload it.  Just doing a fetch/merge with the db does not do the job - mainly in respect of the objects several layers down the hierarchy.  </p>

<p>Any thoughts on whether there is anything fundamentally wrong with this design or if anyone is doing this and has had better luck with working with hibernate on the reloads.</p>

<p>Thanks,
Chris</p>

[{'answer_id': 48795, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 3, 'selected': False, 'text': "<p>Chris, I'm a little confused about your circumstances. If I understand correctly, you have a both a web app (read/write) a standalone application (read-only?) using Hibernate to access a shared database. The changes you make with the web app aren't visible to the standalone app. Is that right?</p>\n\n<p>If so, have you considered using a different second-level cache implementation? I'm wondering if you might be able to use a clustered cache that is shared by both the web application and the standalone application. I believe that SwarmCache, which is integrated with Hibernate, will allow this, but I haven't tried it myself.</p>\n\n<p>In general, though, you should know that the contents of a given cache will never be aware of activity by another application (that's why I suggest having both apps share a cache). Good luck!</p>\n"}, {'answer_id': 50509, 'author': 'Binil Thomas', 'author_id': 3973, 'author_profile': 'https://Stackoverflow.com/users/3973', 'pm_score': 5, 'selected': True, 'text': '<p>A Hibernate session loads all data it reads from the DB into what they call the <i>first-level cache</i>. Once a row is loaded from the DB, any subsequent fetches for a row with the same PK will return the data from this cache. Furthermore, Hibernate gaurentees reference equality for objects with the same PK in a single Session. </p>\n\n<p>From what I understand, your read-only server application never closes its Hibernate session. So when the DB gets updated by the read-write application, the Session on read-only server is unaware of the change. Effectively, your read-only application is loading an in-memory copy of the database and using that copy, which gets stale in due course.</p>\n\n<p>The simplest and best course of action I can suggest is to close and open Sessions as needed. This sidesteps the whole problem. Hibernate Sessions are intended to be a window for a short-lived interaction with the DB. I agree that there is a performance gain by not reloading the object-graph again and again; but you need to measure it and convince yourself that it is worth the pains.</p>\n\n<p>Another option is to close and reopen the Session periodically. This ensures that the read-only application works with data not older than a given time interval. But there definitely is a window where the read-only application works with stale data (although the design guarantees that it gets the up-to-date data eventually). This might be permissible in many applications - you need to evaluate your situation. </p>\n\n<p>The third option is to use a <i>second level cache</i> implementation, and use short-lived Sessions. There are various caching packages that work with Hibernate with relative merits and demerits.</p>\n'}, {'answer_id': 574534, 'author': 'Andrey Vityuk', 'author_id': 60935, 'author_profile': 'https://Stackoverflow.com/users/60935', 'pm_score': 2, 'selected': False, 'text': '<p>From my point of view, you should change your underline Hibernate cache to that one, which supports clustered mode. It could be a <a href="http://www.jboss.org/jbosscache/" rel="nofollow noreferrer">JBoss Cache</a> or a <a href="http://swarmcache.sourceforge.net/" rel="nofollow noreferrer">Swarm Cache</a>. The first one has a better support of data synchronization (replication and invalidation) and also supports JTA.</p>\n\n<p>Then you will able to configure cache synchronization between webapp and server. Also look at isolation level if you will use JBoss Cache. I believe you should use <em>READ_COMMITTED</em> mode if you want to get new data on a server from the same session.</p>\n'}, {'answer_id': 13579352, 'author': 'Nicolas Zozol', 'author_id': 968988, 'author_profile': 'https://Stackoverflow.com/users/968988', 'pm_score': 1, 'selected': False, 'text': '<p>The most used practice is to have a <a href="http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html_single/#d0e129" rel="nofollow">Container-Managed Entity Manager</a> so that two or more applications in the same container (ie Glassfish, Tomcat, Websphere) can share the same caches.\nBut if you don\'t use an Application container, because you use Play! for instance, then I would build some webservices in the <em>primary Application</em> to read/write consistently in the cache.</p>\n\n<p>I think using stale data is an open door for disaster. Just like Singletons become Multitons, read-only applications are often a <em>write sometimes</em>.</p>\n\n<p><em>Belt and braces</em> :)</p>\n'}]
['https://Stackoverflow.com/questions/48733', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/48310/']