Skip to content

Commit 83e0f4f

Browse files
committed
Fix JdbcOperationsSessionRepository lazy deserialization
Resolves: #1411
1 parent 397d4e5 commit 83e0f4f

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2018 the original author or authors.
2+
* Copyright 2014-2019 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.
@@ -797,18 +797,14 @@ public void sessionFromStoreResolvesAttributesLazily() {
797797
MapSession delegate = (MapSession) ReflectionTestUtils.getField(session,
798798
"delegate");
799799

800+
Supplier attribute1 = delegate.getAttribute("attribute1");
801+
assertThat(ReflectionTestUtils.getField(attribute1, "value")).isNull();
800802
assertThat((String) session.getAttribute("attribute1")).isEqualTo("value1");
801-
assertThat(delegate).isNotNull();
802-
assertThat(ReflectionTestUtils
803-
.getField((Supplier) delegate.getAttribute("attribute1"), "value"))
804-
.isEqualTo("value1");
805-
assertThat(ReflectionTestUtils
806-
.getField((Supplier) delegate.getAttribute("attribute2"), "value"))
807-
.isNull();
803+
assertThat(ReflectionTestUtils.getField(attribute1, "value")).isEqualTo("value1");
804+
Supplier attribute2 = delegate.getAttribute("attribute2");
805+
assertThat(ReflectionTestUtils.getField(attribute2, "value")).isNull();
808806
assertThat((String) session.getAttribute("attribute2")).isEqualTo("value2");
809-
assertThat(ReflectionTestUtils
810-
.getField((Supplier) delegate.getAttribute("attribute2"), "value"))
811-
.isEqualTo("value2");
807+
assertThat(ReflectionTestUtils.getField(attribute2, "value")).isEqualTo("value2");
812808
}
813809

814810
@Test // gh-1203

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2018 the original author or authors.
2+
* Copyright 2014-2019 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.
@@ -532,7 +532,8 @@ private void insertSessionAttributes(JdbcSession session, List<String> attribute
532532
public void setValues(PreparedStatement ps, int i) throws SQLException {
533533
String attributeName = attributeNames.get(i);
534534
ps.setString(1, attributeName);
535-
setObjectAsBlob(ps, 2, session.getAttribute(attributeName));
535+
getLobHandler().getLobCreator().setBlobAsBytes(ps, 2,
536+
serialize(session.getAttribute(attributeName)));
536537
ps.setString(3, session.getId());
537538
}
538539

@@ -547,7 +548,8 @@ public int getBatchSize() {
547548
this.jdbcOperations.update(this.createSessionAttributeQuery, (ps) -> {
548549
String attributeName = attributeNames.get(0);
549550
ps.setString(1, attributeName);
550-
setObjectAsBlob(ps, 2, session.getAttribute(attributeName));
551+
getLobHandler().getLobCreator().setBlobAsBytes(ps, 2,
552+
serialize(session.getAttribute(attributeName)));
551553
ps.setString(3, session.getId());
552554
});
553555
}
@@ -561,7 +563,8 @@ private void updateSessionAttributes(JdbcSession session, List<String> attribute
561563
@Override
562564
public void setValues(PreparedStatement ps, int i) throws SQLException {
563565
String attributeName = attributeNames.get(i);
564-
setObjectAsBlob(ps, 1, session.getAttribute(attributeName));
566+
getLobHandler().getLobCreator().setBlobAsBytes(ps, 1,
567+
serialize(session.getAttribute(attributeName)));
565568
ps.setString(2, session.primaryKey);
566569
ps.setString(3, attributeName);
567570
}
@@ -576,7 +579,8 @@ public int getBatchSize() {
576579
else {
577580
this.jdbcOperations.update(this.updateSessionAttributeQuery, (ps) -> {
578581
String attributeName = attributeNames.get(0);
579-
setObjectAsBlob(ps, 1, session.getAttribute(attributeName));
582+
getLobHandler().getLobCreator().setBlobAsBytes(ps, 1,
583+
serialize(session.getAttribute(attributeName)));
580584
ps.setString(2, session.primaryKey);
581585
ps.setString(3, attributeName);
582586
});
@@ -659,16 +663,17 @@ private void prepareQueries() {
659663
getQuery(DELETE_SESSIONS_BY_EXPIRY_TIME_QUERY);
660664
}
661665

662-
private void setObjectAsBlob(PreparedStatement ps, int paramIndex, Object object)
663-
throws SQLException {
664-
byte[] bytes = (byte[]) this.conversionService.convert(object,
666+
private LobHandler getLobHandler() {
667+
return this.lobHandler;
668+
}
669+
670+
private byte[] serialize(Object object) {
671+
return (byte[]) this.conversionService.convert(object,
665672
TypeDescriptor.valueOf(Object.class),
666673
TypeDescriptor.valueOf(byte[].class));
667-
this.lobHandler.getLobCreator().setBlobAsBytes(ps, paramIndex, bytes);
668674
}
669675

670-
private Object getBlobAsObject(ResultSet rs, String columnName) throws SQLException {
671-
byte[] bytes = this.lobHandler.getBlobAsBytes(rs, columnName);
676+
private Object deserialize(byte[] bytes) {
672677
return this.conversionService.convert(bytes, TypeDescriptor.valueOf(byte[].class),
673678
TypeDescriptor.valueOf(Object.class));
674679
}
@@ -898,8 +903,9 @@ public List<JdbcSession> extractData(ResultSet rs) throws SQLException, DataAcce
898903
}
899904
String attributeName = rs.getString("ATTRIBUTE_NAME");
900905
if (attributeName != null) {
901-
Object attributeValue = getBlobAsObject(rs, "ATTRIBUTE_BYTES");
902-
session.delegate.setAttribute(attributeName, lazily(() -> attributeValue));
906+
byte[] bytes = getLobHandler().getBlobAsBytes(rs, "ATTRIBUTE_BYTES");
907+
session.delegate.setAttribute(attributeName,
908+
lazily(() -> deserialize(bytes)));
903909
}
904910
sessions.add(session);
905911
}

0 commit comments

Comments
 (0)