FIX: Cache exist query results

This commit is contained in:
Roland Praml authored and Jonas Pöhler committed 2021-08-27 15:27:54 +02:00
1 parent 56fed3ad87
commit 35bff7c3e7
2 files changed
+33

No files matched your search

@@ -339,6 +339,13 @@ public final class PersistRequestBean<T> extends PersistRequest implements BeanP
} }
} }
private void onFailedUpdateUndoGeneratedProperties() {
for (BeanProperty prop : beanDescriptor.propertiesGenUpdate()) {
Object oldVal = intercept.getOrigValue(prop.getPropertyIndex());
prop.setValue(entityBean, oldVal);
}
}
private void onInsertGeneratedProperties() { private void onInsertGeneratedProperties() {
for (BeanProperty prop : beanDescriptor.propertiesGenInsert()) { for (BeanProperty prop : beanDescriptor.propertiesGenInsert()) {
Object value = prop.getGeneratedProperty().getInsertValue(prop, entityBean, now()); Object value = prop.getGeneratedProperty().getInsertValue(prop, entityBean, now());
@@ -857,6 +864,7 @@ public final class PersistRequestBean<T> extends PersistRequest implements BeanP
public final void checkRowCount(int rowCount) { public final void checkRowCount(int rowCount) {
if (rowCount != 1 && rowCount != Statement.SUCCESS_NO_INFO) { if (rowCount != 1 && rowCount != Statement.SUCCESS_NO_INFO) {
if (ConcurrencyMode.VERSION == concurrencyMode) { if (ConcurrencyMode.VERSION == concurrencyMode) {
onFailedUpdateUndoGeneratedProperties();
throw new OptimisticLockException("Data has changed. updated row count " + rowCount, null, bean); throw new OptimisticLockException("Data has changed. updated row count " + rowCount, null, bean);
} else if (rowCount == 0 && type == Type.UPDATE) { } else if (rowCount == 0 && type == Type.UPDATE) {
throw new EntityNotFoundException("No rows updated"); throw new EntityNotFoundException("No rows updated");
@@ -10,6 +10,9 @@ import org.tests.model.basic.UTMaster;
import javax.persistence.OptimisticLockException; import javax.persistence.OptimisticLockException;
import java.sql.Timestamp; import java.sql.Timestamp;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class TestIUDVanilla extends BaseTestCase { public class TestIUDVanilla extends BaseTestCase {
@Test @Test
@@ -79,4 +82,26 @@ public class TestIUDVanilla extends BaseTestCase {
e0.setVersion(42); e0.setVersion(42);
e0.update(); e0.update();
} }
@Test
public void testOptimisticLockException() {
UTMaster e0 = new UTMaster("optLock");
DB.save(e0);
e0 = DB.find(UTMaster.class).where().eq("name", "optLock").findOne();
UTMaster e1 = DB.find(UTMaster.class).where().eq("name", "optLock").findOne();
int oldVersion = e0.getVersion();
e0.setDescription("foo");
e0.save();
assertThat(e0.getVersion()).isGreaterThan(oldVersion);
e1.setDescription("bar");
oldVersion = e1.getVersion();
assertThatThrownBy(e1::save).isInstanceOf(OptimisticLockException.class);
// after optimisticLockExecption, a restore of version is expected
assertThat(e1.getVersion()).isEqualTo(oldVersion);
// and subsequent saves must fail
assertThatThrownBy(e1::save).isInstanceOf(OptimisticLockException.class);
}
} }