Skip to content

Commit cde256e

Browse files
felixscheinosteleftherias
authored andcommitted
Fix bug in JDBC SaveMode.ON_GET_ATTRIBUTE
Closes gh-2040
1 parent 63f7f7b commit cde256e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

+2-1
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
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,20 @@ void saveWithSaveModeOnGetAttribute() {
655655
verifyNoMoreInteractions(this.jdbcOperations);
656656
}
657657

658+
@Test
659+
void saveWithSaveModeOnGetAttributeAndNewAttributeSetAndGet() {
660+
this.repository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE);
661+
MapSession delegate = new MapSession();
662+
delegate.setAttribute("attribute1", (Supplier<String>) () -> "value1");
663+
JdbcSession session = this.repository.new JdbcSession(delegate, UUID.randomUUID().toString(), false);
664+
session.setAttribute("attribute2", "value2");
665+
session.getAttribute("attribute2");
666+
this.repository.save(session);
667+
verify(this.jdbcOperations, times(1)).update(startsWith("INSERT INTO SPRING_SESSION_ATTRIBUTES ("),
668+
isA(PreparedStatementSetter.class));
669+
verifyNoMoreInteractions(this.jdbcOperations);
670+
}
671+
658672
@Test
659673
void saveWithSaveModeAlways() {
660674
this.repository.setSaveMode(SaveMode.ALWAYS);

0 commit comments

Comments
 (0)