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.  [ 1 post ] 
Author Message
 Post subject: Interesting! Mulitple updates in a transaction
PostPosted: Mon Aug 11, 2008 12:34 pm 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
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.


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

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.