Hibernate 2.1.1
EHCache enabled
I noticed that hibernate caching is disabled if you supply the connection to the openSession() method on the SessionFactory.
Code:
Connection con = <get from somewhere>;
SessionFactory sf = <get from somewhere>;
Session sess = sf.openSession(con);
Looking through the source code, I noticed the following
Code:
private Session openSession(Connection connection, boolean autoClose, long timestamp, Interceptor interceptor) {
return new SessionImpl( connection, this, autoClose, timestamp, interceptor );
}
public Session openSession(Connection connection, Interceptor interceptor) {
return openSession( connection, false, Long.MIN_VALUE, interceptor );
}
public Session openSession(Connection connection) {
return openSession(connection, interceptor); //prevents this session from adding things to cache
}
It looks like the timestamp is set to some silly negative number so that when the ReadWriteCache is used, the check for isGettable() from the cache will always return false. I noticed that the other openSession() methods just get the timestamp from the CacheProvider.
Code:
public Session openSession(Interceptor interceptor) throws HibernateException {
// note that this timestamp is not correct if the connection provider
// returns an older JDBC connection that was associated with a
// transaction that was already begun before openSession() was called
// (don't know any possible solution to this!)
long timestamp = settings.getCacheProvider().nextTimestamp();
return openSession( null, true, timestamp, interceptor );
}
public Session openSession() throws HibernateException {
return openSession(interceptor);
}
Due to a very long story with websphere advanced server, I have to pass in connections to the SessionFactory but want to use ehcache. So, I've modified the openSession() method to:
Code:
public Session openSession(Connection connection, Interceptor interceptor) {
long timestamp = settings.getCacheProvider().nextTimestamp();
//return openSession( connection, false, Long.MIN_VALUE, interceptor );
return openSession(connection, false, timestamp, interceptor);
}
My hunch is that the reason hibernate blocks caching stuff if you pass in connections is because hibernate has no way of knowing if every call passes in a connection to the same database.
I've rambled on for a bit there but I guess my questionis, Is there a better work around or any problems with what i am doing?
Thanks...