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