Reputation: 12548
I already figured that there are lots of questions regarding caching and hibernate on SO however I couldn't find a solution to my problem. Most questions where refering using hibernate directly without using JPA.
We are using Spring with Hibernate configured as a JPA provider. The application itself is a web application using JSF. The underlying database is a SQL Server 2008.
As per JPA-specification we added the following to the persistence.xml
.
<shared-cache-mode>NONE</shared-cache-mode>
Hibernate seems to more or less ignore it. The Hibernate documentation tells that this disables the second level cache. However our application requires the most recent data from the database as a 3rd application will change some of the data every now end then.
How can I disable the caching? We are getting data the JPA way with queries like:
@PersistenceContext(type=PersistenceContextType.EXTENDED)
private EntityManager em;
...
return em.createQuery("SELECT mt FROM " + MyTable.class.getSimpleName() + " mt",
MyTable.class).getResultList();
@axtavt I start up the application and load a page in the browser that is querying the table. I switch to my DBMS and do an update on the table. I open up a second browser and its still showing the old data and not the updated one.
Update
I ran some further tests. I was using the @PersistenceContext
with EXTENDED
. I added it here. If I remove that it works however I run into different problems then...
Upvotes: 1
Views: 2464
Reputation: 17874
In hibernate, they call it the second level cache, because the hibernate session itself serves as a first level cache. The session (persistence context) normally caches objects for the duration of one @Transactional method, just to make sure that object relations are maintained correctly.
In your use case, disabling the second level cache is what you need. During a request, you'd probably want consistent data, but between requests, there is no caching, so for the next request all data is fresh again.
Upvotes: 2
Reputation: 21194
It seems that your transaction has not committed and thus the other browser is still seeing old data.
I've never used the EXTENDED solution before but as far as I can tell, when using EXTENDED you have to commit the long running transaction in order for other transactions to be able to see the result. Look up how to commit a transaction when using the EXTENDED persistence context type.
Upvotes: 1
Reputation: 12548
I ran some further tests. I was using the @PersistenceContext
with EXTENDED
. I added it to the main post. If I remove the EXTENDED the refresh works. However I run into different problems then...
Upvotes: 0