Hibernate version: 2.1.1
Name and version of the database you are using: Db2 v8.1.7
I have an issue with attempting to selectively bypass the Hibernate first level cache using LockMode.READ withing the same Hibernate session. I have a simple test method as follows: -
public void testAdapter() throws Exception
{
long orgId = 1;
// Start up hibernate session.
hibernateAccess = new DataAccessBase();
hibernateAccess.startUpHibernate();
hibernateAccess.setHibernateOrganisationGroup(orgId);
dbsession = DbSession.getInstance();
try
{
long id = 1;
Vendor s1 = (Vendor) dbsession.load(Vendor.class, id);
Vendor s2 = (Vendor) dbsession.load(Vendor.class, id, LockMode.UPGRADE);
}
catch (HibernateException e)
{
e.printStackTrace();
}
dbsession.closeSession();
}
DbSession forms a wrapper around a Hibernate session and provides the same functionality in terms of method availability.
When I run the above code the first session.load accesses the database ( I can see this with show_sql - true ). When the second session.load executes it does NOT access the database even though I have specified a LockMode of READ. Instead it retrieves the object from the first level Hibernate cache.
As I have not specified a LockMode for the first session.load I expected it to execute with a LockMode.NONE. It does this up to a point but when it stores the retrieved object in the cache, Hibernate associates it with a Lockmode of READ. Then when the second session.load executes it has a LockMode that equals the LockMode of the first session.load.
The Hibernate code that performs this association seems to be in method instanceNotYetLoaded in class net.sf.hibernate.loader.Loader.java
My question is - Is this a Hibernate bug or a lack of understanding on my part? The Hibernate in Action book states that using LockMode.READ will "Bypass both levels of the cache, and perform a version
check to verify that the object in memory is the same version that currently
exists in the database". The main goal of all of this is to use managed versioning. We implement managed versioning and I want to be able to retrieve the latest available object directly from the database for use in version number comparison.
Ps I have read the Hibernate manual and Hibernate In Action until my eyes have bled. However, I feel the documentation in the area of the first level cache and how to interact with it / bypass it is a bit misleading.
|