Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate does not reconcile insert/updates

 
Stan Sokolov
Ranch Hand
Posts: 120
Hibernate IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a deadlock issue in multi-threading environment if "insert and update of the same object" is performed within the same transaction. Point is that hibernate performs two operations (instead of one) when thread creates one object then updates the same object .
Second operation is deadlocking with another thread that is doing the same job. I expected hibernate reconciles update and insert because both of them are performed before then do a flush. Record that is inserted is not yet persisted when update is requested. It looks like that second update is absolutly undesired.

I tested this issue with HB 2.1 and 3.1 - both with the same results. Configuration is SQL Server, HILO generator is used for PK. Two processes are continiously performing the same operation in the separate JVMs.

As a result I see
insert object x
update object x
Why does not hibernate reconcile insert and update ?

[ October 27, 2005: Message edited by: Stas Sakalou ]
[ October 27, 2005: Message edited by: Stas Sakalou ]
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The update after an insert could be due a missing

inverse="true"

on a bi-directional association. Take a look at

this post.
 
Stan Sokolov
Ranch Hand
Posts: 120
Hibernate IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are no relationships mapped.
Can you just for curiosity sake, make one simple test. You have to create a class with only one method main. In the main method you have to create a session then initialize a new object, and then save it. Update one of the fields of the object and flush the session. Check hibernate SQL trace. Does it perform 2 operations?
?
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it. Hibernate performed 2 operations.

Hibernate is mirroring in sql the operations I performed on the POJOs.

That's really the safe approach. It could cause problems if Hibernate combined statements (or eliminated statements in the case of an insert + a delete.)

For example, triggers might not fire or operations might not cascade correctly.

On my current project, I have an inventory value that my application should insert but never update. If Hibernate combined statements, I could end up with an incorrect value.
[ October 30, 2005: Message edited by: Scott Johnson ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic