-->
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.  [ 7 posts ] 
Author Message
 Post subject: Is this a LockMode bug or a feature?
PostPosted: Fri Jul 30, 2004 10:25 am 
Beginner
Beginner

Joined: Wed Jul 21, 2004 12:28 pm
Posts: 27
Location: New York
Below is the test case that demonstrates odd behavior. Parent and Child are nothing special just regular parent (one-to-many) child relation and both are versioned. Both 2.1.4 and 2.1.3 exhibit the same behavior.

Code:
    public void testLockModeBug () throws Exception {

        Session s = HibernateSessionFactoryHolder.getSessionFactory ().openSession ();
       
        // create parent and children
        Parent p = new Parent ();
        p.setName ("test parent");
        Set children = new HashSet ();
        for (int i = 0; i < 2; i++) {
           Child c = new Child (); c.setName ("child " + String.valueOf (i+1)); c.setParent (p);
           children.add (c);
        }
        p.setChildren (children);
       
        s.saveOrUpdate (p);
        s.flush ();
        s.close ();
       
        // update children collection in one session and try to lock parent in another
        Session s0 = HibernateSessionFactoryHolder.getSessionFactory ().openSession ();
        Session s1 = HibernateSessionFactoryHolder.getSessionFactory ().openSession ();
       
        try {
            // retrieve the created parent
            Parent p0 = (Parent) s0.load (Parent.class, p.getId ());
            for (Iterator iter = p0.getChildren ().iterator (); iter.hasNext ();) {
                Child element = (Child) iter.next ();
                element.setName ("Updated name");
                break;
            }
            s0.flush ();
            s0.close ();
       
            try {
                s1.lock (p, LockMode.READ);
            } catch (StaleObjectStateException sose) {
                // this is correct
            }
       
            try {
                s1.lock (p, LockMode.READ);
                fail ("Object has to be stale still");
            } catch (StaleObjectStateException sose) {
                // this is correct
            }
        } finally  {
            try { s1.close (); } catch (Throwable t) {}
        }
       
    }

_________________
Thanks,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 10:26 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You should read about transactions, there isn't any in your code, just random data access.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 10:52 am 
Beginner
Beginner

Joined: Wed Jul 21, 2004 12:28 pm
Posts: 27
Location: New York
I did read about transactions, but what does it have to do with transactions?

In any case, I just dropped transactional code since it is noisy and not related to what I thought (and still think) is a bug.

Another reason I think it is a bug is the following test that passes:

Code:
    public void testLockModeNonBug () throws Exception {

        Session s = HibernateSessionFactoryHolder.getSessionFactory ().openSession ();
       
        // create parent and children
        Parent p = new Parent ();
        p.setName ("test parent");
        s.saveOrUpdate (p);
        s.flush ();
        s.close ();
       
        // update children collection in one session and try to lock parent in another
        Session s0 = HibernateSessionFactoryHolder.getSessionFactory ().openSession ();
        try {
            // retrieve the created parent
            Parent p0 = (Parent) s0.load (Parent.class, p.getId ());
            p0.setName ("name update");
            s0.flush ();
        } finally {
            try { s0.close (); } catch (Throwable t) {}
        }
       
        Session s1 = HibernateSessionFactoryHolder.getSessionFactory ().openSession ();
        try {
            try {
                s1.lock (p, LockMode.READ);
                fail ("Object has to be stale still");
            } catch (StaleObjectStateException sose) {
                // this is correct
            }
       
            try {
                s1.lock (p, LockMode.READ);
                fail ("Object has to be stale still");
            } catch (StaleObjectStateException sose) {
                // this is correct
            }
        } finally  {
            try { s1.close (); } catch (Throwable t) {}
        }
       
    }

_________________
Thanks,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 11:18 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
As long as I see close() in your code without transaction commit or rollback, I will not read on.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 11:36 am 
Beginner
Beginner

Joined: Wed Jul 21, 2004 12:28 pm
Posts: 27
Location: New York
oh well ...

Code:
    public void testLockModeBug () throws Exception {

        Session s = HibernateSessionFactoryHolder.getSessionFactory ().openSession ();
       
        Parent p = new Parent ();
        p.setName ("test parent");
        Set children = new HashSet ();
        for (int i = 0; i < 2; i++) {
           Child c = new Child (); c.setName ("child " + String.valueOf (i+1)); c.setParent (p);
           children.add (c);
        }
        p.setChildren (children);
       
        s.saveOrUpdate (p);
        s.flush ();
        s.close ();
       
        Session s0 = HibernateSessionFactoryHolder.getSessionFactory ().openSession ();
        try {
            Parent p0 = (Parent) s0.load (Parent.class, p.getId ());
            for (Iterator iter = p0.getChildren ().iterator (); iter.hasNext ();) {
                Child element = (Child) iter.next ();
                element.setName ("Updated name");
                break;
            }
            s0.flush ();
        } finally {
            s0.close ();
        }

        Session s1 = HibernateSessionFactoryHolder.getSessionFactory ().openSession ();
        Transaction t0 = s1.beginTransaction ();
        boolean bShallCommit = true;
        try {
            try {
                s1.lock (p, LockMode.READ);
            } catch (StaleObjectStateException sose) {
                bShallCommit = false;
                t0.rollback ();
            }
        } finally  {
            if (bShallCommit) { t0.commit (); }
        }

        Transaction t1 = s1.beginTransaction ();
        bShallCommit = true;
        try{
            try {
                s1.lock (p, LockMode.READ);
                fail ("Object has to be stale still");
            } catch (StaleObjectStateException sose) {
                bShallCommit = false;
                try { t1.rollback (); } catch (Throwable t) { t.printStackTrace (); }
            }
        } finally  {
            if (bShallCommit) {
                try { t1.commit (); } catch (Throwable t) { t.printStackTrace (); }
            }
            try { s1.close (); } catch (Throwable t) { t.printStackTrace (); }
        }
       
    }


by the way I had no doubt that you did not read the code in the first place. No way in hell you could answer it that fast ...

_________________
Thanks,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 11:38 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
And again, I stopped here:

[code]

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 11:39 am 
Beginner
Beginner

Joined: Wed Jul 21, 2004 12:28 pm
Posts: 27
Location: New York
you know what whatever ...

_________________
Thanks,
Alex


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.