Skip to content

Commit 60581c6

Browse files
candrewsvpavic
authored andcommitted
Fix delta handling in JdbcOperationsSessionRepository
See gh-1070
1 parent 836ea12 commit 60581c6

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcOperationsSessionRepository.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,13 +733,23 @@ public Set<String> getAttributeNames() {
733733
@Override
734734
public void setAttribute(String attributeName, Object attributeValue) {
735735
if (attributeValue == null) {
736-
this.delta.put(attributeName, DeltaValue.REMOVED);
736+
if (this.delta.get(attributeName) == DeltaValue.ADDED) {
737+
this.delta.remove(attributeName);
738+
}
739+
else {
740+
this.delta.put(attributeName, DeltaValue.REMOVED);
741+
}
737742
}
738-
else if (this.delegate.getAttribute(attributeName) != null) {
743+
else if (this.delta.get(attributeName) != DeltaValue.ADDED && this.delegate.getAttribute(attributeName) != null) {
739744
this.delta.put(attributeName, DeltaValue.UPDATED);
740745
}
741746
else {
742-
this.delta.put(attributeName, DeltaValue.ADDED);
747+
if (this.delta.get(attributeName) == DeltaValue.REMOVED) {
748+
this.delta.put(attributeName, DeltaValue.UPDATED);
749+
}
750+
else {
751+
this.delta.put(attributeName, DeltaValue.ADDED);
752+
}
743753
}
744754
this.delegate.setAttribute(attributeName, attributeValue);
745755
if (PRINCIPAL_NAME_INDEX_NAME.equals(attributeName) ||

spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcOperationsSessionRepositoryTests.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@
4343
import static org.assertj.core.api.Assertions.assertThat;
4444
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4545
import static org.mockito.ArgumentMatchers.anyLong;
46+
import static org.mockito.ArgumentMatchers.anyString;
4647
import static org.mockito.ArgumentMatchers.eq;
4748
import static org.mockito.ArgumentMatchers.isA;
4849
import static org.mockito.ArgumentMatchers.startsWith;
4950
import static org.mockito.BDDMockito.given;
5051
import static org.mockito.Mockito.atLeastOnce;
5152
import static org.mockito.Mockito.mock;
53+
import static org.mockito.Mockito.never;
5254
import static org.mockito.Mockito.times;
5355
import static org.mockito.Mockito.verify;
5456
import static org.mockito.Mockito.verifyZeroInteractions;
@@ -338,6 +340,23 @@ public void saveUpdatedAddSingleAttribute() {
338340
verifyZeroInteractions(this.jdbcOperations);
339341
}
340342

343+
@Test
344+
public void saveUpdatedAddSingleAttributeSetTwice() {
345+
JdbcOperationsSessionRepository.JdbcSession session = this.repository.new JdbcSession("primaryKey",
346+
new MapSession());
347+
session.setAttribute("testName", "testValue");
348+
session.setAttribute("testName", "testValue");
349+
350+
this.repository.save(session);
351+
352+
assertThat(session.isNew()).isFalse();
353+
assertPropagationRequiresNew();
354+
verify(this.jdbcOperations, times(1)).update(
355+
startsWith("INSERT INTO SPRING_SESSION_ATTRIBUTES("),
356+
isA(PreparedStatementSetter.class));
357+
verifyZeroInteractions(this.jdbcOperations);
358+
}
359+
341360
@Test
342361
public void saveUpdatedAddMultipleAttributes() {
343362
JdbcOperationsSessionRepository.JdbcSession session = this.repository.new JdbcSession("primaryKey",
@@ -431,6 +450,61 @@ public void saveUpdatedRemoveMultipleAttributes() {
431450
verifyZeroInteractions(this.jdbcOperations);
432451
}
433452

453+
@Test
454+
public void saveUpdatedAddThenRemoveSingleAttribute() {
455+
JdbcOperationsSessionRepository.JdbcSession session = this.repository.new JdbcSession("primaryKey",
456+
new MapSession());
457+
session.setAttribute("testName", "testValue");
458+
session.removeAttribute("testName");
459+
460+
this.repository.save(session);
461+
462+
assertThat(session.isNew()).isFalse();
463+
assertPropagationRequiresNew();
464+
verify(this.jdbcOperations, never()).update(
465+
anyString(),
466+
isA(PreparedStatementSetter.class));
467+
verifyZeroInteractions(this.jdbcOperations);
468+
}
469+
470+
@Test
471+
public void saveUpdatedModifyThenRemoveSingleAttribute() {
472+
JdbcOperationsSessionRepository.JdbcSession session = this.repository.new JdbcSession("primaryKey",
473+
new MapSession());
474+
session.setAttribute("testName", "testValue");
475+
session.clearChangeFlags();
476+
session.setAttribute("testName", "testValueModifed");
477+
session.removeAttribute("testName");
478+
479+
this.repository.save(session);
480+
481+
assertThat(session.isNew()).isFalse();
482+
assertPropagationRequiresNew();
483+
verify(this.jdbcOperations, times(1)).update(
484+
startsWith("DELETE FROM SPRING_SESSION_ATTRIBUTES WHERE"),
485+
isA(PreparedStatementSetter.class));
486+
verifyZeroInteractions(this.jdbcOperations);
487+
}
488+
489+
@Test
490+
public void saveUpdatedRemoveThenModifySingleAttribute() {
491+
JdbcOperationsSessionRepository.JdbcSession session = this.repository.new JdbcSession("primaryKey",
492+
new MapSession());
493+
session.setAttribute("testName", "testValue");
494+
session.clearChangeFlags();
495+
session.removeAttribute("testName");
496+
session.setAttribute("testName", "testValueModifed");
497+
498+
this.repository.save(session);
499+
500+
assertThat(session.isNew()).isFalse();
501+
assertPropagationRequiresNew();
502+
verify(this.jdbcOperations, times(1)).update(
503+
startsWith("UPDATE SPRING_SESSION_ATTRIBUTES SET"),
504+
isA(PreparedStatementSetter.class));
505+
verifyZeroInteractions(this.jdbcOperations);
506+
}
507+
434508
@Test
435509
public void saveUpdatedLastAccessedTime() {
436510
JdbcOperationsSessionRepository.JdbcSession session = this.repository.new JdbcSession("primaryKey",

0 commit comments

Comments
 (0)