Skip to content

Commit dde88e4

Browse files
Adapt tests that expected a DuplicateKeyException
Closes spring-projectsgh-2108
1 parent 65b994c commit dde88e4

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.session.jdbc;
1818

19+
import java.io.IOException;
1920
import java.time.Duration;
2021
import java.time.Instant;
2122
import java.time.temporal.ChronoUnit;
@@ -31,7 +32,7 @@
3132
import org.springframework.beans.factory.annotation.Autowired;
3233
import org.springframework.context.ApplicationContext;
3334
import org.springframework.context.annotation.Bean;
34-
import org.springframework.dao.DuplicateKeyException;
35+
import org.springframework.core.serializer.DefaultSerializer;
3536
import org.springframework.jdbc.core.JdbcOperations;
3637
import org.springframework.jdbc.core.JdbcTemplate;
3738
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@@ -54,7 +55,6 @@
5455

5556
import static org.assertj.core.api.Assertions.assertThat;
5657
import static org.assertj.core.api.Assertions.assertThatCode;
57-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5858

5959
/**
6060
* Base class for {@link JdbcIndexedSessionRepository} integration tests.
@@ -803,13 +803,17 @@ void saveNewSessionAttributeConcurrently() {
803803
this.jdbcOperations.update("INSERT INTO SPRING_SESSION_ATTRIBUTES VALUES (?, ?, ?)", (ps) -> {
804804
ps.setString(1, (String) ReflectionTestUtils.getField(session, "primaryKey"));
805805
ps.setString(2, attributeName);
806-
lobCreator.setBlobAsBytes(ps, 3, "value2".getBytes());
806+
lobCreator.setBlobAsBytes(ps, 3, serialize("value2"));
807807
});
808808
}
809809
session.setAttribute(attributeName, attributeValue);
810810
if (this.applicationContext.getBeansOfType(SessionRepositoryCustomizer.class).isEmpty()) {
811811
// without DB specific upsert configured we're seeing duplicate key error
812-
assertThatExceptionOfType(DuplicateKeyException.class).isThrownBy(() -> this.repository.save(session));
812+
// which is suppressed.
813+
// The attribute value won't be updated
814+
this.repository.save(session);
815+
assertThat((String) this.repository.findById(session.getId()).getAttribute(attributeName))
816+
.isEqualTo("value2");
813817
}
814818
else {
815819
// with DB specific upsert configured we're fine
@@ -819,6 +823,15 @@ void saveNewSessionAttributeConcurrently() {
819823
}
820824
}
821825

826+
private static byte[] serialize(String value) {
827+
try {
828+
return new DefaultSerializer().serializeToByteArray(value);
829+
}
830+
catch (IOException ex) {
831+
throw new RuntimeException(ex);
832+
}
833+
}
834+
822835
private String getSecurityName() {
823836
return this.context.getAuthentication().getName();
824837
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ public int getBatchSize() {
498498
throw ex;
499499
}
500500
catch (DataIntegrityViolationException ex) {
501+
if (logger.isTraceEnabled()) {
502+
logger.trace("Not able to create session attributes", ex);
503+
}
501504
// parent record not found - we are ignoring this error because we
502505
// assume that a concurrent request has removed the session
503506
}
@@ -515,6 +518,9 @@ public int getBatchSize() {
515518
throw ex;
516519
}
517520
catch (DataIntegrityViolationException ex) {
521+
if (logger.isTraceEnabled()) {
522+
logger.trace("Not able to create session attributes", ex);
523+
}
518524
// parent record not found - we are ignoring this error because we
519525
// assume that a concurrent request has removed the session
520526
}

0 commit comments

Comments
 (0)