Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private void onUpdateGeneratedProperties() {
}
}

private void onFailedUpdateUndoGeneratedProperties() {
public void onFailedUpdateUndoGeneratedProperties() {
for (BeanProperty prop : beanDescriptor.propertiesGenUpdate()) {
Object oldVal = intercept.origValue(prop.propertyIndex());
prop.setValue(entityBean, oldVal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@ public interface BatchPostExecute {
* Add timing metrics for batch persist.
*/
void addTimingBatch(long startNanos, int batch);

/**
* Undos generated properties.
*/
default void onFailedUpdateUndoGeneratedProperties() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkme: Rename to "onExecuteUndo(ex)"?

Are there possible uses cases in CallableSql/OrmUpdate/SqlUpdate?


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ private void postExecute() {

private void executeAndCheckRowCounts() throws SQLException {
try {
results = pstmt.executeBatch();
try {
results = pstmt.executeBatch();
} catch (SQLException ex) {
list.forEach(BatchPostExecute::onFailedUpdateUndoGeneratedProperties);
throw ex;
}
if (transaction.isLogSql()) {
transaction.logSql(" -- executeBatch() size:{0} sql:{1}", results.length, sql);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private int execute(PersistRequestBean<?> request, PersistHandler handler) {
if (request.transaction().isLogSummary()) {
request.transaction().logSummary(msg);
}
request.onFailedUpdateUndoGeneratedProperties();
throw dbPlatform.translate(msg, e);
} finally {
if (!batched) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.ebean.DB;
import io.ebean.DuplicateKeyException;
import io.ebean.Transaction;
import io.ebean.annotation.Transactional;
import io.ebean.xtest.BaseTestCase;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -13,6 +14,7 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class TestInsertDuplicateKey extends BaseTestCase {
Expand Down Expand Up @@ -100,4 +102,61 @@ private void insertTheBatch_duplicateKey_catchAndContinue() {
doc0.setBody("insertTheBatch_duplicateKey_catchAndContinue-1");
doc0.save();
}


@Test
public void insert_duplicateKey_retry() {
Document doc1 = new Document();
doc1.setTitle("Key1ABC");
doc1.setBody("one");
doc1.save();

Document doc2 = new Document();
doc2.setTitle("Key1ABC");
doc2.setBody("clashes with doc1");
Long version = doc2.getVersion();
assertThrows(DuplicateKeyException.class, doc2::save);
assertEquals(version, doc2.getVersion());

doc2.setTitle("Key1ABCD");

doc2.save();

doc1.setTitle("Key1ABCD");
assertThrows(DuplicateKeyException.class, doc1::save);
doc1.setTitle("Key1ABCDE");
doc1.save();
}

@Test
public void insert_duplicateKey_retryWithBatch() {
Document doc1 = new Document();
doc1.setTitle("Key2ABC");
doc1.setBody("one");
doc1.save();

Document doc2 = new Document();
doc2.setTitle("Key2ABC");
doc2.setBody("clashes with doc1");
Long version = doc2.getVersion();
try (Transaction tx = DB.beginTransaction()) {
tx.setBatchMode(true);
doc2.save();
assertThrows(DuplicateKeyException.class, tx::commit);
}
assertEquals(version, doc2.getVersion());

doc2.setTitle("Key2ABCD");

doc2.save();

doc1.setTitle("Key2ABCD");
assertThrows(DuplicateKeyException.class, doc1::save);
doc1.setTitle("Key2ABCDE");
try (Transaction tx = DB.beginTransaction()) {
tx.setBatchMode(true);
doc1.save();
tx.commit();
}
}
}