aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate or browser caching DB results? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate or browser caching DB results?" Watch "Hibernate or browser caching DB results?" New topic
Author

Hibernate or browser caching DB results?

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
I have a situation where I check to see if the user's login is still valid. This check is a simple database check of a date field.

What I'm noticing is that if I change the value in the database, the hibernate code doesn't always "see" the new value and says that the user's account has expired when, in fact, I just updated it and saved the results (separately, in MYSQL).

Once I re-start Tomcat, all is OK again.

I've tried clearing the browser cache and using session.flush() on the hibernate logic, but neither of these things seems to fix the fact that the hibernate code is not getting the current DB value.

Is there another way to make sure I'm getting the most current database data?

Thanks for any suggestions.

Mike
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

There are a few places this could happen. your session handling could be suspect - can you show use the code you use to get the latest data from the db? Usually, opeing a new session and calling a get method will read the database. If you are reusing a session you may get cached data. Are you using a second level cache or query cache?


What I'm noticing is that if I change the value in the database, the hibernate code doesn't always "see" the new value and says that the user's account has expired when, in fact, I just updated it and saved the results (separately, in MYSQL).

I don't know MySQL very well, but does your SQL client auto-commit? I.e. have you really updated the data or does it just appear you have?


Once I re-start Tomcat, all is OK again.

This is authentication code, right? Tomcat (like most containers) has an authentication cache (because reauthenticating for every call is drag on performance). Depending on what you are doing and how you are doing it you may be hitting this.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Hi Paul,

Here is the basic code I'm using to read from the database:


[ Edited to use code tags - Paul Sturrock ]
[ June 17, 2008: Message edited by: Paul Sturrock ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


How does this get a session? Is is an existing session or a new one?


Why are you performing a select query withing a transaction? Is there more going on than a simple get?


What does findByUsrLogin do? Does it run a query? Do you use a query cache?


Are any exceptions being thrown? Do you log the exception message, or ignore it (as above)?
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Hi Paul,

A couple of responses to your questions.

1. The code that I'm using references boilerplate code that MyEclipse creates for you automatically. I didn't hand-craft the underlying session code or anything like that. And,....

2. This code works perfectly locally.

Both local and remote (server) the code is using Tomcat 5.5, MySQL 5.0.x (though different actual release numbers). Remote uses Apache and Linux where development is Windows and just Tomcat for both Web server and servlet container. Both local and remote use the same JDBC driver version (though I also tried the 3.x and 5.0 versions on the server to see if that would help. It didn't).

I also tried adding autoReconnect="true" as shown below (from hibernate config file), but that didn't seem to help either...

<property name="connection.url">
jdbc:mysql://localhost:3306/TestProject?autoReconnect="true"
</property>

-------------

I'm not sure how to actually track down this problem.

There are LOTS of postings about this error, but no fixes that I've seen.

Thanks again for any thing you can suggest.

Mike
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Added these lines too to hibernate config (autoreconnect=true added the way below instead of the way mentioned in the posting directly above):

<property name="connection.autoReconnect">true</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>

Don't know if this will help, but I'll let you know.

Thanks

Mike
Madan Prabhu Durai
Greenhorn

Joined: Nov 11, 2008
Posts: 3
By using session.evict and sessionFactory.evict methods. in addition to that call twice the retrieve code for your data.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate or browser caching DB results?