I am using an EhCache for my second level cache and I have run into an issue where everytime I execute session.CreateSQLQuery(), the cache discards all objects. I use the createSQLQuery to execute a table level lock on a table (I am using a database which requires a LOCK FOR ACCESS in order to allow concurrent reads and writes to be executed on a table), and within the same transaction is modify objects from the table or the cache.
So say that I have the following code:
Code:
public List<TestObject> readAll() throws Exception {
Session sess = HibernateSessionFactory.getSessionFactory().getCurrentSession();
sess.beginTransaction();
sess.createSQLQuery("LOCK TABLE TEST_OBJECT FOR ACCESS").executeUpdate();
sess.setCacheMode(CacheMode.NORMAL);
List<TestObject> objs = sess.createQuery("from TestObject as tst").setLockMode("tst", LockMode.FORCE).setCacheable(true).list();
sess.getTransaction().commit();
return objs;
}
Once this executes all the objects from the TEST_OBJECT table are loaded into memory. Once the transaction is committed, the following debug message appears:
Invalidating space [TEST_OBJECT], timestamp: 5161828781969408
Then once I attempt to retrieve objects from the cache:
Code:
public TestObject read(String uuid) {
Session sess = HibernateSessionFactory.getSessionFactory().getCurrentSession();
sess.beginTransaction();
sess.setCacheMode(CacheMode.NORMAL);
TestObject obj = (TestObject) sess.get(TestObject.class, uuid);
sess.getTransaction().commit();
return obj;
}
I keep getting the invalidate space message and queries are executed against the database.
So I have 2 questions:
1. Is there a way to have hibernate prepend that Lock table statement prior to each query that it makes to the database?
Or
2. Is there a way to execute the native query without invalidating the cache?
Thanks