Skip to content

Commit 5c3b138

Browse files
committed
Optimize the usage of JacksonMongoSessionConverter to prevent duplicate MongoSession Document saves when a custom ObjectMapper is provided.spring-projects#3185
1 parent 2068812 commit 5c3b138

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616

1717
package org.springframework.session.data.mongo;
1818

19-
import java.io.IOException;
20-
import java.util.Collections;
21-
import java.util.Date;
22-
import java.util.HashMap;
23-
2419
import com.fasterxml.jackson.annotation.JsonAutoDetect;
2520
import com.fasterxml.jackson.annotation.JsonCreator;
2621
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -37,24 +32,28 @@
3732
import org.bson.Document;
3833
import org.bson.json.JsonMode;
3934
import org.bson.json.JsonWriterSettings;
40-
4135
import org.springframework.data.mongodb.core.query.Criteria;
4236
import org.springframework.data.mongodb.core.query.Query;
4337
import org.springframework.lang.Nullable;
4438
import org.springframework.security.jackson2.SecurityJackson2Modules;
4539
import org.springframework.session.FindByIndexNameSessionRepository;
4640
import org.springframework.util.Assert;
4741

42+
import java.io.IOException;
43+
import java.util.Collections;
44+
import java.util.Date;
45+
import java.util.HashMap;
46+
4847
/**
4948
* {@code AbstractMongoSessionConverter} implementation using Jackson.
5049
*
5150
* @author Jakub Kubrynski
5251
* @author Greg Turnquist
5352
* @author Michael Ruf
53+
* @author TiQuan Hu
5454
* @since 1.2
5555
*/
5656
public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter {
57-
5857
private static final Log LOG = LogFactory.getLog(JacksonMongoSessionConverter.class);
5958

6059
private static final String ATTRS_FIELD_NAME = "attrs.";
@@ -76,11 +75,22 @@ public JacksonMongoSessionConverter(Iterable<Module> modules) {
7675
}
7776

7877
public JacksonMongoSessionConverter(ObjectMapper objectMapper) {
79-
80-
Assert.notNull(objectMapper, "ObjectMapper can NOT be null!");
78+
Assert.notNull(objectMapper, "ObjectMapper can not be null!");
8179
this.objectMapper = objectMapper;
8280
}
8381

82+
public JacksonMongoSessionConverter(ObjectMapper objectMapper, boolean copyToUse) {
83+
Assert.notNull(objectMapper, "ObjectMapper can not be null!");
84+
if (!copyToUse) {
85+
configureObjectMapper(objectMapper);
86+
this.objectMapper = objectMapper;
87+
return;
88+
}
89+
var objectMapperCopy = objectMapper.copy();
90+
configureObjectMapper(objectMapperCopy);
91+
this.objectMapper = objectMapperCopy;
92+
}
93+
8494
@Nullable
8595
protected Query getQueryForIndex(String indexName, Object indexValue) {
8696

@@ -93,9 +103,12 @@ protected Query getQueryForIndex(String indexName, Object indexValue) {
93103
}
94104

95105
private ObjectMapper buildObjectMapper() {
96-
97106
ObjectMapper objectMapper = new ObjectMapper();
107+
this.configureObjectMapper(objectMapper);
108+
return objectMapper;
109+
}
98110

111+
private void configureObjectMapper(ObjectMapper objectMapper) {
99112
// serialize fields instead of properties
100113
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
101114
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
@@ -108,8 +121,6 @@ private ObjectMapper buildObjectMapper() {
108121
objectMapper.registerModules(SecurityJackson2Modules.getModules(getClass().getClassLoader()));
109122
objectMapper.addMixIn(MongoSession.class, MongoSessionMixin.class);
110123
objectMapper.addMixIn(HashMap.class, HashMapMixin.class);
111-
112-
return objectMapper;
113124
}
114125

115126
@Override
@@ -154,7 +165,7 @@ private static class MongoSessionMixin {
154165

155166
@JsonCreator
156167
MongoSessionMixin(@JsonProperty("_id") String id,
157-
@JsonProperty("intervalSeconds") long maxInactiveIntervalInSeconds) {
168+
@JsonProperty("intervalSeconds") long maxInactiveIntervalInSeconds) {
158169
}
159170

160171
}

0 commit comments

Comments
 (0)