• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

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

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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




 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic