File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Why My view doesn't render the last changes in the DB? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Why My view doesn Watch "Why My view doesn New topic
Author

Why My view doesn't render the last changes in the DB?

Enrique Villamizar
Ranch Hand

Joined: Jul 30, 2005
Posts: 93
Hi to everybody:

The last few days I have been trying to solve the following problem but still I don't know what is hapenning.

I have a ManagedBean (RequestScoped) which has a method returning a String (the name of a navigation page). This bean has a method that opens a Hibernate Session, create a simply org.hibernate.Query "from Customer" and save the result in a List<Customer> customers and finally close the Session.

The first time the application begins it retrieves the data from a MySQL table and show it in the view and everything is OK. Nervertheless when I change data directly in the database and execute the method my view doesn't reflect the changes and I don't see why, because my method every time Open the Hibernate Session, clean the List and execute the Query once again, save the result en the List and close the Session.

In the view I use a datatable.

If I redeploy my application it refreshes the data the first time but it never does it again.

Please, could you tell me what could be the problem? The browser cache (Firefox 3.69)? The Scope of the Bean? Hibernate Configuration? o ...?

Tools: Netbeans 6.9.1, Tomcat 6.0, Hibernate3.5, primefaces 2.2.1.

Thanks in advanced for your help.


Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
hi Enrique,

have you enabled second level cache for your customer class.

best way for you to check is by looking at the hashcode of returned list.
if the same hashcode is returned every time you refresh the page, then you can be certain that the problem is with your hibernate configuration(assuming the bean is request scope)
Enrique Villamizar
Ranch Hand

Joined: Jul 30, 2005
Posts: 93
I tried to flush the Session, and it didn’t work. I understand that we use flush in DML operation to synchronize the in-memory Session state with the database. Since I’m running a query I think I don’t need to flush the Session.

Finally I used a Transaction and now my query works well, I mean, it renders the last changes I made in the DB.

Nevertheless I don´t understand why I need a Transaction for a query (read-only). Why, when no using Transaction, the first time I run the query I get the correct data but when the data change in the database my page doesn’t show the last changes. Is it a kind of cache? If a transaction is also needed for queries why the first time it works?

The following code doesn't work.


Where my old data is saved since every time the method is called I begin a new Session, clean the result list (list = null) and I also clean the Query (query = null)?

A Transaction is needed for queries???

The following solved my problem:
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
hi Enrique,

yes you are right.
Hibernate cache query. but by default it doesn't.

here is the small article i am sure will clear your doubt.

Regards,
Hemant




Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hemant Thard wrote:hi Enrique,

have you enabled second level cache for your customer class.

best way for you to check is by looking at the hashcode of returned list.
if the same hashcode is returned every time you refresh the page, then you can be certain that the problem is with your hibernate configuration(assuming the bean is request scope)


A better way is to look at your configuration. Enabling the second level cache and the query cache is a fairly involved affair, you are not going to do it by accident. At the very least you would need to have a query marked as cachable, which you don't, so it is not Hibernate's Query cache that is causing this.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why My view doesn't render the last changes in the DB?