Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Caching Issues with Hibnernate

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I left out a couple of files:

HibernateUtil.java


BaseDAO.java
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic