Skip to content

JdbcOperationsSessionRepository: deserialize lazily #1133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -530,7 +531,7 @@ private void insertSessionAttributes(JdbcSession session, List<String> attribute
public void setValues(PreparedStatement ps, int i) throws SQLException {
String attributeName = attributeNames.get(i);
ps.setString(1, attributeName);
serialize(ps, 2, session.getAttribute(attributeName));
bytesToLob(ps, 2, serialize(session.getAttribute(attributeName)));
ps.setString(3, session.getId());
}

Expand All @@ -545,7 +546,7 @@ public int getBatchSize() {
this.jdbcOperations.update(this.createSessionAttributeQuery, (ps) -> {
String attributeName = attributeNames.get(0);
ps.setString(1, attributeName);
serialize(ps, 2, session.getAttribute(attributeName));
bytesToLob(ps, 2, serialize(session.getAttribute(attributeName)));
ps.setString(3, session.getId());
});
}
Expand All @@ -559,7 +560,7 @@ private void updateSessionAttributes(JdbcSession session, List<String> attribute
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
String attributeName = attributeNames.get(i);
serialize(ps, 1, session.getAttribute(attributeName));
bytesToLob(ps, 1, serialize(session.getAttribute(attributeName)));
ps.setString(2, session.primaryKey);
ps.setString(3, attributeName);
}
Expand All @@ -574,7 +575,7 @@ public int getBatchSize() {
else {
this.jdbcOperations.update(this.updateSessionAttributeQuery, (ps) -> {
String attributeName = attributeNames.get(0);
serialize(ps, 1, session.getAttribute(attributeName));
bytesToLob(ps, 1, serialize(session.getAttribute(attributeName)));
ps.setString(2, session.primaryKey);
ps.setString(3, attributeName);
});
Expand Down Expand Up @@ -657,18 +658,26 @@ private void prepareQueries() {
getQuery(DELETE_SESSIONS_BY_EXPIRY_TIME_QUERY);
}

private void serialize(PreparedStatement ps, int paramIndex, Object attributeValue)
private void bytesToLob(PreparedStatement ps, int paramIndex, byte[] bytes)
throws SQLException {
this.lobHandler.getLobCreator().setBlobAsBytes(ps, paramIndex,
(byte[]) this.conversionService.convert(attributeValue,
bytes);
}

private byte[] serialize(Object attributeValue) {
return (byte[]) this.conversionService.convert(attributeValue,
TypeDescriptor.valueOf(Object.class),
TypeDescriptor.valueOf(byte[].class)));
TypeDescriptor.valueOf(byte[].class));
}

private Object deserialize(ResultSet rs, String columnName)
private byte[] lobToBytes(ResultSet rs, String columnName)
throws SQLException {
return this.lobHandler.getBlobAsBytes(rs, columnName);
}

private Object deserialize(byte[] bytes) {
return this.conversionService.convert(
this.lobHandler.getBlobAsBytes(rs, columnName),
bytes,
TypeDescriptor.valueOf(byte[].class),
TypeDescriptor.valueOf(Object.class));
}
Expand All @@ -679,6 +688,34 @@ private enum DeltaValue {

}

static final <Z> Supplier<Z> constantSupplier(Z value) {
if (value == null) {
return null;
}
else {
return () -> value;
}
}

static final <Z> Supplier<Z> lazily(Supplier<Z> supplier) {
if (supplier == null) {
return null;
}
else {
return new Supplier<Z>() {
private Z value;

@Override
public Z get() {
if (this.value == null) {
this.value = supplier.get();
}
return this.value;
}
};
}
}

/**
* The {@link Session} to use for {@link JdbcOperationsSessionRepository}.
*
Expand Down Expand Up @@ -748,7 +785,13 @@ public String changeSessionId() {

@Override
public <T> T getAttribute(String attributeName) {
return this.delegate.getAttribute(attributeName);
Supplier<T> supplier = this.delegate.getAttribute(attributeName);
if (supplier == null) {
return null;
}
else {
return supplier.get();
}
}

@Override
Expand Down Expand Up @@ -783,7 +826,7 @@ public void setAttribute(String attributeName, Object attributeValue) {
? oldDeltaValue
: DeltaValue.UPDATED));
}
this.delegate.setAttribute(attributeName, attributeValue);
this.delegate.setAttribute(attributeName, constantSupplier(attributeValue));
if (PRINCIPAL_NAME_INDEX_NAME.equals(attributeName) ||
SPRING_SECURITY_CONTEXT.equals(attributeName)) {
this.changed = true;
Expand Down Expand Up @@ -875,7 +918,8 @@ public List<JdbcSession> extractData(ResultSet rs) throws SQLException, DataAcce
}
String attributeName = rs.getString("ATTRIBUTE_NAME");
if (attributeName != null) {
session.delegate.setAttribute(attributeName, deserialize(rs, "ATTRIBUTE_BYTES"));
byte[] bytes = lobToBytes(rs, "ATTRIBUTE_BYTES");
session.delegate.setAttribute(attributeName, lazily(() -> deserialize(bytes)));
}
sessions.add(session);
}
Expand Down