-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: hibernate caching disabled if you supply a connection
PostPosted: Wed Jan 07, 2004 10:29 pm 
Newbie

Joined: Wed Jan 07, 2004 10:15 pm
Posts: 3
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...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 3:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
uughhh.

The trouble is that Hibernate needs a before-start-of-transaction timestamp for use of read-write cache concurrency strategy.

If you pass your own connection, it has NO WAY to figure that out.

Try nonstrict-read-write perhaps.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 10:58 am 
Newbie

Joined: Wed Jan 07, 2004 10:15 pm
Posts: 3
Will try nonstrict read write. I do have cases where simulatenous updates to the same object happen, not sure how that will work out with nonstrict.

Is passing in the LockMode.UPDATE in the session.load sufficient?

If I can guarantee that the connection I am passing in is fresh and not involved in any other transaction or work, it is alright to override the openSession() method to use the timestamper?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Is passing in the LockMode.UPDATE in the session.load sufficient?



Ummmm hmmm. Never thought about this.

Actually, no, it probably doesn't make a difference, unless you use an UPGRADE lock even for *reading* the data.

It more depends upon the transaction isolation mode.

I think that for a database that uses locking (ie. NOT Oracle or postgres!!), nonstrict-read-write is safe if the db is in REPEATABLE READ or SERIALIZABLE, not if the db is in READ COMMITTED.

What you need is long read locks (read locks that are held until transaction completion). Long write locks are not sufficient.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:34 pm 
Newbie

Joined: Wed Jan 07, 2004 10:15 pm
Posts: 3
Doh. Unfortunately environment is Oracle 9i.

Hmmmm. I'll poke around some more and see if I can come up with a workaround.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.