Ensure version update happens in transaction with insert.
Original pull request #687
This commit is contained in:
committed by
Jens Schauder
parent
dd846bd9e0
commit
ae7cb66e3f
@@ -17,6 +17,7 @@ package example.springdata.jdbc.howto.selectiveupdate;
|
||||
|
||||
import org.springframework.dao.OptimisticLockingFailureException;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -35,6 +36,7 @@ class PartyHatRepositoryImpl implements PartyHatRepository {
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public void addPartyHat(Minion minion) {
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@ package example.springdata.jdbc.howto.selectiveupdate;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
|
||||
@SpringBootApplication
|
||||
class SelectiveUpdateApplication {
|
||||
|
||||
@@ -58,7 +58,7 @@ class SelectiveUpdateApplicationTests {
|
||||
|
||||
Minion bob2 = minions.findById(bob.id).orElseThrow();
|
||||
|
||||
assertThat(bob2.toys).containsExactly(bob.toys.toArray(new Toy[] {}));
|
||||
assertThat(bob2.toys).containsExactlyElementsOf(bob.toys);
|
||||
assertThat(bob2.name).isEqualTo("Bob");
|
||||
assertThat(bob2.color).isEqualTo(Color.PURPLE);
|
||||
}
|
||||
@@ -81,4 +81,23 @@ class SelectiveUpdateApplicationTests {
|
||||
assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() -> minions.addPartyHat(bob));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cannotGrantPartyHatWhenOutOfSync() {
|
||||
|
||||
Minion bob = new Minion("Bob").addToy(new Toy("Tiger Duck")).addToy(new Toy("Security blanket"));
|
||||
minions.save(bob);
|
||||
minions.turnPurple(bob.id);
|
||||
|
||||
assertThat(bob.color).isEqualTo(Color.YELLOW);
|
||||
assertThat(bob.version).isOne();
|
||||
assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() -> minions.addPartyHat(bob));
|
||||
|
||||
Minion bob2 = minions.findById(bob.id).orElseThrow();
|
||||
|
||||
assertThat(bob2.name).isEqualTo("Bob");
|
||||
assertThat(bob2.color).isEqualTo(Color.PURPLE);
|
||||
assertThat(bob2.version).isEqualTo(bob.version + 1);
|
||||
assertThat(bob2.toys).extracting("name").containsExactlyInAnyOrder("Tiger Duck", "Security blanket");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user