-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Deleting an object from a collection leads to race condition
PostPosted: Fri Jan 02, 2004 11:04 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
At a high level, I have a Bag collection which has been read in by Hibernate. I want to delete one of the child objects. The code I use for that looks schematically like this:

Code:
BeginTransaction()
Update(child_item)
Delete(child_item)
EndTransaction()


At the same time, in another thread, and a different Session that same collection is being looked at, schematically like this:

Code:
BeginTransaction()
Update(parent_item)
if (!parent_item.getCollection().contains(child_item) {
   parent_item.getCollection().add(new_item)
}
EndTrantransaction()


Which produces a synch error "Record Not Found." Clearly it's a race condition, but I am not sure the 'correct' way to code this with Hibernate. Does my scheme look correct?

I've tried various variations, like removing the child from the parent collection, with or without the explicit delete. This one seems the closest to being correct, but it is still giving that error. Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 03, 2004 7:13 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I can't see any race condition, unless equals of child is broken, or your tx level is improper.
The second assert will happen once the child object is deleted (and tx completed)

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Explain how broken EQ could cause a problem
PostPosted: Sat Jan 03, 2004 8:55 am 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
Thanks for the answer. Can you elaborate on how a broken EQ could cause the race condition?

Also what do you mean about an improper TX level?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 03, 2004 9:33 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
bag.contains(object) use object(Equals), so if equals is broken
Code:
parent_item.getCollection().add(new_item)
will be done even if you don't want to.
For tx level explaination see http://otn.oracle.com/oramag/oracle/02-jul/o42special_jdbc.html

Actually, I suspect a race condition under broken equals, since you read a collection, delete an element (in DB) and then update the collection Hibernate speaking which find inconsistency in collection to update.
BTW Hibernate should prevent such race condition by using version optimistic locking.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Maybe the trace on the error will suggest something...
PostPosted: Sat Jan 03, 2004 4:51 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
I checked the .equals function, and it looks good to me:
Code:
  public boolean equals(Object obj) {
    if (!(obj instanceof ItemIF)) {
      return false;
    }
    ItemIF cmp = (ItemIF) obj;

    boolean te;
    if (title != null) {
      te = title.equals(cmp.getTitle());
    } else {
      te = (cmp.getTitle() == null);
    }
    boolean de;
    if (description != null) {
      de = description.equals(cmp.getDescription());
    } else {
      de = (cmp.getDescription() == null);
    }
    boolean le;
    if (link != null) {
      le = link.equals(cmp.getLink());
    } else {
      le = (cmp.getLink() == null);
    }
    return (te && de && le);
}


Also here's the trace
Code:
net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)
   at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
   at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:672)
   at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:625)
   at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2308)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2262)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2187)
   at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
   at de.nava.informa.impl.hibernate.ChannelBuilder.endTransaction(ChannelBuilder.java:131)
   at de.nava.informa.utils.PersistChanGrpMgrTask.handleChannelItems(PersistChanGrpMgrTask.java:178)
   at de.nava.informa.utils.PersistChanGrpMgrTask.handleChannel(PersistChanGrpMgrTask.java:105)
   at de.nava.informa.utils.PersistChanGrpMgrTask.run(PersistChanGrpMgrTask.java:72)
   at java.util.TimerThread.mainLoop(Timer.java:432)
   at java.util.TimerThread.run(Timer.java:382)


Does that suggest anything?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 1:41 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Does parent_item and child_item all ItemIF classes. Isn't there is a child also parent issue.

What is link, an object ? Does link.equals OK ?

Does Item has surrogate key ? if yes, why not using it in the equals function ? If no, Is title/description/link is the PK of itemIF (should be because of the equals implem) ?

Does parent_item and children items has id properly initialized (wrong id means Hibernate cannot find row) ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 5:29 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
epbernard wrote:
Does parent_item and child_item all ItemIF classes. Isn't there is a child also parent issue.


Sorry, I don't understand... Please restate?


Top
 Profile  
 
 Post subject: More questions
PostPosted: Sun Jan 04, 2004 5:35 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
THANKS for helping. To answer your question:

Link is just a URL object, and Description is a String. I am relying on the standard Java serialization and the default .equals.

Did the Stack Trace tell you anything useful?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 6:10 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Quote:
Does parent_item and child_item all ItemIF classes. Isn't there is a child also parent issue.

If parent_item is also one of it's own child (or something like that).

The stack trace is just telling me that Hibernate expects n rows updates and the JDBC driver return something different.

Here are the questions :
1. How do you persist an URL object ?
2. Do you have some issues described above about parent/child
3. Does Item has surrogate key ?
3.1. if yes, why not using it in the equals function ?
3.2 If no, Is title/description/link is the PK of itemIF (should be because of the equals implem) ?
4. which flush launch the exception ? The collection update or the child deletion ?
5. Enable Hibernate logs, it helps a lot (http://www.hibernate.org/ForumMailingli ... AskForHelp)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 10:06 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
Thanks for sticking with me on this!! Here are my answers.

1. How do you persist an URL object ?
>> it's just a field in the ItemIF object. I rely on serialization into a STRING field of the db

2. Do you have some issues described above about parent/child
>> No

3. Does Item has surrogate key ?
>> Not really.

3.1. if yes, why not using it in the equals function ?

3.2 If no, Is title/description/link is the PK of itemIF (should be because of the equals implem) ?
>> Yes

4. which flush launch the exception ? The collection update or the child deletion ?
>> The Update

5. Enable Hibernate logs, it helps a lot (http://www.hibernate.org/ForumMailingli ... AskForHelp)
For all of hibernate? What level, debug?

THANKS!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2004 12:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
DEBUG mode for all hibernate.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: I emailed you the log file
PostPosted: Tue Jan 06, 2004 12:01 am 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
Sorry, I didn't know what part is useful to you and it seemed much to big to post as text

To see the error search for:

SEVERE: Could not synchronize database state with sessio

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 11:58 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hum, It's just too hard too debug off-site and out of the project knowledge. Try to use p6spy to find the real executed statement and the missing row asked, it will help.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Thanks for all your help
PostPosted: Tue Jan 06, 2004 12:17 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
I will look into that.


Top
 Profile  
 
 Post subject: Emmanuel, hope you are still here: Problem running p6Spy
PostPosted: Thu Jan 08, 2004 1:13 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
I finally got back to this. Is there a special hibernate db dialect that I should or should not use with p6spy?

(Note I am not under an app server.)

Here's the trace:
Code:
java.sql.SQLException: No suitable driver
   at java.sql.DriverManager.getConnection(DriverManager.java:532)
   at java.sql.DriverManager.getConnection(DriverManager.java:140)
   at net.sf.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:95)
   at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
   at net.sf.hibernate.cfg.Configuration.buildSettings(Configuration.java:1091)

Any info?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.