-->
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.  [ 2 posts ] 
Author Message
 Post subject: Recovering from update failure with optimistic locking
PostPosted: Thu Sep 01, 2005 1:34 am 
Newbie

Joined: Mon Jun 27, 2005 8:46 pm
Posts: 3
Location: Brisbane, Australia
Hibernate version: 3.1

Hibernate annotations version: 3.1 beta 4

Hibernate entity manager version: 3.1 beta 2

Here's the test code I'm using:

Code:
    MyTest t = manager.find(MyTest.class, 1);
    boolean success = false;
    while (!success)  {
      try  {
        t.setValue(new Date().toString());
       // --First time through,  another db connection updates the record here --
        manager.persist(t);
        manager.flush();
        success = true;
      }
      catch (RuntimeException ex)  {
        manager.refresh(t);
      }
    }


"MyTest" is a simple entity class (in condensed form):

Code:
@Entity
@Table(name = "TEST")
public class MyTest implements Serializable {
  private Integer testId;
  private String value;
  private int version;

  @Id(generate = GeneratorType.NONE)
  @Column(name = "TEST_ID") public Integer getTestId()  {  return testId;  }

  @Column(name = "VALUE") public String getValue() { return value;  }

  @Version
  @Column(name = "VERSION") public int getVersion() {   return version;  }

  public void setTestId(Integer testId) {  this.testId = testId; }
  public void setValue(String value) { this.value = value;  }
  public void setVersion(int version) { this.version = version; }
}


I'm seeing an issue when the success loop attempts to update the MyTest object on the second attempt, after having failed the first flush() due to the record being updated by another db connection (to simulate this I set a breakpoint on the manager.persist() line, and updated the row, including its version, in a separate window). The subsequent attempt to flush() the update fails, as the update action from the first update attempt still exists in the action queue (org.hibernate.engine.ActionQueue). This code will continue to loop around ad-infinitum, adding an additional EntityUpdateAction to the end of the action queue each iteration.

My first question is, is this a bug? If it is then I'm happy to create an entry for it in Jira. If it's not a bug, then does anyone know of a work-around? I need to come up with some kind of pattern for re-attempting an update if it fails due to the optimistic locking version being out of date.

On a side note, does anyone know if the EJB persistence spec is likely to introduce some kind of lock mode control equivalent to what is already provided in Hibernate? The lack of this feature is what prompted me to use optimistic locking to achieve a similar result.

Thanks!

Shane Bryzak


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 11:56 am 
Newbie

Joined: Tue Jun 13, 2006 11:53 am
Posts: 2
I have the exact same problem with Hibernate 3.2 CR2 and Entity Manager 3.2 CR1. Has anyone found some workaround / solution ?


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