Skip to content

Commit 8668164

Browse files
Fix bug in JDBC SaveMode.ON_GET_ATTRIBUTE
1 parent e8e4ee2 commit 8668164

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

spring-session-jdbc/src/integration-test/java/org/springframework/session/jdbc/AbstractJdbcIndexedSessionRepositoryITests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.security.core.context.SecurityContextHolder;
4646
import org.springframework.session.FindByIndexNameSessionRepository;
4747
import org.springframework.session.MapSession;
48+
import org.springframework.session.SaveMode;
4849
import org.springframework.session.config.SessionRepositoryCustomizer;
4950
import org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession;
5051
import org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession;
@@ -819,6 +820,33 @@ void saveNewSessionAttributeConcurrently() {
819820
}
820821
}
821822

823+
@Test
824+
void savesWhenSaveModeIsOnGetAttributeAndANewAttributeIsSetAndGet() {
825+
try {
826+
// Prerequisite: SaveMode.ON_GET_ATTRIBUTE is enabled
827+
this.repository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE);
828+
829+
// Step 1: Save session
830+
JdbcSession initialSession = this.repository.createSession();
831+
this.repository.save(initialSession);
832+
833+
// Step 2: Load session again, set an attribute and immediately get it again
834+
JdbcSession session = this.repository.findById(initialSession.getId());
835+
String attributeName = "attribute1";
836+
String attributeValue = "value1";
837+
session.setAttribute(attributeName, attributeValue);
838+
session.getAttribute(attributeName);
839+
this.repository.save(session);
840+
841+
// Step 3: Load session 3rd time, verify that attribute is set correctly
842+
assertThat((String) this.repository.findById(initialSession.getId()).getAttribute(attributeName))
843+
.isEqualTo(attributeValue);
844+
}
845+
finally {
846+
this.repository.setSaveMode(SaveMode.ON_SET_ATTRIBUTE);
847+
}
848+
}
849+
822850
private String getSecurityName() {
823851
return this.context.getAuthentication().getName();
824852
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,8 @@ public <T> T getAttribute(String attributeName) {
728728
T attributeValue = supplier.get();
729729
if (attributeValue != null
730730
&& JdbcIndexedSessionRepository.this.saveMode.equals(SaveMode.ON_GET_ATTRIBUTE)) {
731-
this.delta.put(attributeName, DeltaValue.UPDATED);
731+
this.delta.merge(attributeName, DeltaValue.UPDATED, (oldDeltaValue,
732+
deltaValue) -> (oldDeltaValue == DeltaValue.ADDED) ? oldDeltaValue : deltaValue);
732733
}
733734
return attributeValue;
734735
}

0 commit comments

Comments
 (0)