diff --git a/spring-session-jdbc/src/integration-test/java/org/springframework/session/jdbc/AbstractJdbcOperationsSessionRepositoryITests.java b/spring-session-jdbc/src/integration-test/java/org/springframework/session/jdbc/AbstractJdbcOperationsSessionRepositoryITests.java index 84150c63..3929d783 100644 --- a/spring-session-jdbc/src/integration-test/java/org/springframework/session/jdbc/AbstractJdbcOperationsSessionRepositoryITests.java +++ b/spring-session-jdbc/src/integration-test/java/org/springframework/session/jdbc/AbstractJdbcOperationsSessionRepositoryITests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2018 the original author or authors. + * Copyright 2014-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -797,18 +797,14 @@ public abstract class AbstractJdbcOperationsSessionRepositoryITests { MapSession delegate = (MapSession) ReflectionTestUtils.getField(session, "delegate"); + Supplier attribute1 = delegate.getAttribute("attribute1"); + assertThat(ReflectionTestUtils.getField(attribute1, "value")).isNull(); assertThat((String) session.getAttribute("attribute1")).isEqualTo("value1"); - assertThat(delegate).isNotNull(); - assertThat(ReflectionTestUtils - .getField((Supplier) delegate.getAttribute("attribute1"), "value")) - .isEqualTo("value1"); - assertThat(ReflectionTestUtils - .getField((Supplier) delegate.getAttribute("attribute2"), "value")) - .isNull(); + assertThat(ReflectionTestUtils.getField(attribute1, "value")).isEqualTo("value1"); + Supplier attribute2 = delegate.getAttribute("attribute2"); + assertThat(ReflectionTestUtils.getField(attribute2, "value")).isNull(); assertThat((String) session.getAttribute("attribute2")).isEqualTo("value2"); - assertThat(ReflectionTestUtils - .getField((Supplier) delegate.getAttribute("attribute2"), "value")) - .isEqualTo("value2"); + assertThat(ReflectionTestUtils.getField(attribute2, "value")).isEqualTo("value2"); } @Test // gh-1203 diff --git a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcOperationsSessionRepository.java b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcOperationsSessionRepository.java index 5eab81b9..e3196122 100644 --- a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcOperationsSessionRepository.java +++ b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcOperationsSessionRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2018 the original author or authors. + * Copyright 2014-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -532,7 +532,8 @@ public class JdbcOperationsSessionRepository implements public void setValues(PreparedStatement ps, int i) throws SQLException { String attributeName = attributeNames.get(i); ps.setString(1, attributeName); - setObjectAsBlob(ps, 2, session.getAttribute(attributeName)); + getLobHandler().getLobCreator().setBlobAsBytes(ps, 2, + serialize(session.getAttribute(attributeName))); ps.setString(3, session.getId()); } @@ -547,7 +548,8 @@ public class JdbcOperationsSessionRepository implements this.jdbcOperations.update(this.createSessionAttributeQuery, (ps) -> { String attributeName = attributeNames.get(0); ps.setString(1, attributeName); - setObjectAsBlob(ps, 2, session.getAttribute(attributeName)); + getLobHandler().getLobCreator().setBlobAsBytes(ps, 2, + serialize(session.getAttribute(attributeName))); ps.setString(3, session.getId()); }); } @@ -561,7 +563,8 @@ public class JdbcOperationsSessionRepository implements @Override public void setValues(PreparedStatement ps, int i) throws SQLException { String attributeName = attributeNames.get(i); - setObjectAsBlob(ps, 1, session.getAttribute(attributeName)); + getLobHandler().getLobCreator().setBlobAsBytes(ps, 1, + serialize(session.getAttribute(attributeName))); ps.setString(2, session.primaryKey); ps.setString(3, attributeName); } @@ -576,7 +579,8 @@ public class JdbcOperationsSessionRepository implements else { this.jdbcOperations.update(this.updateSessionAttributeQuery, (ps) -> { String attributeName = attributeNames.get(0); - setObjectAsBlob(ps, 1, session.getAttribute(attributeName)); + getLobHandler().getLobCreator().setBlobAsBytes(ps, 1, + serialize(session.getAttribute(attributeName))); ps.setString(2, session.primaryKey); ps.setString(3, attributeName); }); @@ -659,16 +663,17 @@ public class JdbcOperationsSessionRepository implements getQuery(DELETE_SESSIONS_BY_EXPIRY_TIME_QUERY); } - private void setObjectAsBlob(PreparedStatement ps, int paramIndex, Object object) - throws SQLException { - byte[] bytes = (byte[]) this.conversionService.convert(object, - TypeDescriptor.valueOf(Object.class), - TypeDescriptor.valueOf(byte[].class)); - this.lobHandler.getLobCreator().setBlobAsBytes(ps, paramIndex, bytes); + private LobHandler getLobHandler() { + return this.lobHandler; } - private Object getBlobAsObject(ResultSet rs, String columnName) throws SQLException { - byte[] bytes = this.lobHandler.getBlobAsBytes(rs, columnName); + private byte[] serialize(Object object) { + return (byte[]) this.conversionService.convert(object, + TypeDescriptor.valueOf(Object.class), + TypeDescriptor.valueOf(byte[].class)); + } + + private Object deserialize(byte[] bytes) { return this.conversionService.convert(bytes, TypeDescriptor.valueOf(byte[].class), TypeDescriptor.valueOf(Object.class)); } @@ -898,8 +903,9 @@ public class JdbcOperationsSessionRepository implements } String attributeName = rs.getString("ATTRIBUTE_NAME"); if (attributeName != null) { - Object attributeValue = getBlobAsObject(rs, "ATTRIBUTE_BYTES"); - session.delegate.setAttribute(attributeName, lazily(() -> attributeValue)); + byte[] bytes = getLobHandler().getBlobAsBytes(rs, "ATTRIBUTE_BYTES"); + session.delegate.setAttribute(attributeName, + lazily(() -> deserialize(bytes))); } sessions.add(session); }