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 Caching Issues with Hibnernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Caching Issues with Hibnernate" Watch "Caching Issues with Hibnernate" New topic
Author

Caching Issues with Hibnernate

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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?


GenRocket - Experts at Building Test Data
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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).

Can you describe the behavior you're seeing?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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

hibernate.cfg.xml


HibernateFilter



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.

User.hbm.xml


User.java
public class User implements Serializable
{
private Integer userId;
private String username;
private String password;
private String firstName;
private String lastName;
private String emailAddress;
private String role;
private boolean active;

public User() { }

public Integer getUserId()
{
return userId;
}

public void setUserId(Integer userId)
{
this.userId = userId;
}

public String getUsername()
{
return username;
}

public void setUsername(String username)
{
this.username = username;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}

public String getFirstName()
{
return firstName;
}

public void setFirstName(String firstName)
{
this.firstName = firstName;
}

public String getLastName()
{
return lastName;
}

public void setLastName(String lastName)
{
this.lastName = lastName;
}

public String getEmailAddress()
{
return emailAddress;
}

public void setEmailAddress(String emailAddress)
{
this.emailAddress = emailAddress;
}

public String getRole()
{
return role;
}

public void setRole(String role)
{
this.role = role;
}

public boolean isActive(){
return this.active;
}
public void setActive(boolean active) {
this.active = active;
}

public boolean isAdmin()
{
if (this.role.equals(Constants.ROLE_ADMIN)){
return true;
}else{
return false;
}
}
}
[/code]

Login code snippet


HibernateUserDAO.java
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

I left out a couple of files:

HibernateUtil.java


BaseDAO.java
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.

Thanks again.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Originally posted by David Harkness:
You should also not put the Session into the HttpSession. Each request should create a new Session and release it at the end.


How is it being placed in the HttpSession?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Gregg Bolinger:
How is it being placed in the HttpSession?
HibernateFilter pulls it from the HttpSession and puts the Session returned from HibernateUtil.disconnectSession() back into it.That looks dangerous to me, even though the Session is disconnected.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Caching Issues with Hibnernate