This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Is there any way to just turn all caching completely off in Hibernate? I am having some strange results and I'd just assume not deal with them right now, but I need the data I look up to be accurate. I have told the hibernate config to disable second level caching and I have not provided any caching statments in any of my mapping files. Is there anything else I can do?
I don't know that you can disable the Session cache. For one thing, any objects you've modified cannot be dropped from the Session cache or their changes would be lost. But the Session cache is also used to ensure that within a single application transaction, each unique object has only one instance (avoid two instances of User with the same ID).
Is there any way to just turn all caching completely off in Hibernate
You could force the Session to flush after every operation (by calling flush()). But I would be very surprised if it were the first level cache itself which is causing issues - so I too would be interested to hear what the funny behaviour is.
Later today I will post exactly what is happening along with code. I am a bit too busy at the moment, but I didn't want everyone to forget about this thread because I really need to get this figured out. Thanks.
I'll begin by saying that I have not done any customization of any of the caching mechanisms within hibernate. I have up to now just let the default ride. I'll show my hibernate.cgf.xml file, my HibernateFilter, and relavent mapping files as the situation calls for it. I am using Hibernate 3 RC4
Ok, let's start with caching issue #1.
Issue #1 ---------------------- I have a column in my database that stores a tinyint. 0 means the account is not active, 1 means the account is active. In my login action, if the username and password pass, I check this value and either return an error message or pass into the webapp. What is happening is after the webapp starts and I login, if I go into the database and change the tinyint value and I try and login again, it is still pulling the old value. I have to restart the webapp to get it to pull the new value. So I am assuming that it is not reading the database again, it is just pulling the value from cache somewhere, but I don't know enough about how it works to figure it out.
Originally posted by Lasse Koskela: It looks like you never close the Hibernate Session object (unless something bad happens) -- try having the Filter close the session after each request and see if that helps.
Perfect. Works great now. Thanks Lasse. Now the question I guess is, I got the HibernateUtil and the HibernateFilter from the CeveatEmptor application from HIA. Why would they not have closed the session after each request? Unless they are closing the session in the DAO's instead of the Filter. I'll have to look at that.
Joined: Aug 07, 2003
You indeed were experiencing a caching issue because the Session is the first level cache. You should also not put the Session into the HttpSession. Each request should create a new Session and release it at the end.
That's strange that HiA had that code. I did get the sense sometimes while skimming HiA and porting my application that it was more a coalescing of the online documentation. Perhaps it was not thoroughly reviewed before publication? Bummer, as that's a very important issue.