Hopefully this is an interesting and relatively unexplored question. I'm trying to implement a "set based tree" using Hibernate. In my implementation of this structure a "delete" node requires three
update statements and a
delete and an "append" node requires two
updates statements and an
save For some updates and deletes, almost the entire table might be modified. (It's a structure that's optimized for tree-based queries; not for lots of updates and delete).
Code for delete looks like this:
Code:
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) {
CacheMode old = session.getCacheMode();
session.setCacheMode(CacheMode.IGNORE);
Query updateChildren = session.createQuery("update SetTreeEntity "
+"set leftExtent = leftExtent - 1, rightExtent = rightExtent - 1, depth = depth - 1 "
+"where leftExtent > :left and rightExtent < :right");
updateChildren.setParameter("left", deleteLeft);
updateChildren.setParameter("right", deleteRight);
updateChildren.executeUpdate();
Query updateRight = session.createQuery("update SetTreeEntity "
+"set leftExtent = leftExtent - 2, rightExtent = rightExtent - 2 "
+"where leftExtent > :right");
updateRight.setParameter("right", deleteRight);
updateRight.executeUpdate();
Query updateParents = session.createQuery("update SetTreeEntity "
+"set rightExtent = rightExtent - 2 "
+"where leftExtent < :left and rightExtent > :right");
updateParents.setParameter("left", deleteLeft);
updateParents.setParameter("right", deleteRight);
updateParents.executeUpdate();
Query deleteEntity = session.createQuery("delete from SetTreeEntity where id = :id");
deleteEntity.setParameter("id", e.getId());
deleteEntity.executeUpdate();
session.flush();
return true;
}
});
Anyway, to test I've sent up multiple threads to append and delete tree nodes and the structure of the tree gets messed up as updates from the "append" method seems to get intertwined with updates from the "delete" method.
I've tried locking at the Java level using "synchronized(lock)" as well as trying to establish transactions using
org.springframework.orm.hibernate3.HibernateTransactionManager but so far as I hit it with a bunch of threads I always end up with a corrupted tree. Since these update and delete actions are supposed to be rare, I could even try a full table lock if I could figure out how to get that to work...
Any help would be appreciated! Even just the right terminology for this type of problem would be great.
Thanks.
Hibernate version: 3.2.6.ga
Code between sessionFactory.openSession() and session.close():
See above. I'm using the SpringFramework Hibernate Template.
Full stack trace of any exception that occurs:
No exception.
Name and version of the database you are using:
MySQL 5.0.x (Using InnoDB tables)
Problems with Session and transaction handling?
Read this: http://hibernate.org/42.html
Read it. Doesn't seem to cover this situation, but I might not fully understand the terminology.