Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure version update happens in transaction with insert. #687

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -17,6 +17,7 @@

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;
Expand All @@ -35,6 +36,7 @@ public PartyHatRepositoryImpl(NamedParameterJdbcOperations template) {
this.template = template;
}

@Transactional
@Override
public void addPartyHat(Minion minion) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
class SelectiveUpdateApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void turnPurpleByDirectUpdate() {

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);
}
Expand All @@ -81,4 +81,23 @@ void grantPartyHat() {
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");
}

}
Loading