diff --git a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java index 6f1a0bd0..cd849fce 100644 --- a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java +++ b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java @@ -44,6 +44,7 @@ import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.ResultSetExtractor; import org.springframework.jdbc.support.lob.DefaultLobHandler; +import org.springframework.jdbc.support.lob.LobCreator; import org.springframework.jdbc.support.lob.LobHandler; import org.springframework.session.DelegatingIndexResolver; import org.springframework.session.FindByIndexNameSessionRepository; @@ -460,63 +461,65 @@ public class JdbcIndexedSessionRepository private void insertSessionAttributes(JdbcSession session, List attributeNames) { Assert.notEmpty(attributeNames, "attributeNames must not be null or empty"); - if (attributeNames.size() > 1) { - this.jdbcOperations.batchUpdate(this.createSessionAttributeQuery, new BatchPreparedStatementSetter() { + try (LobCreator lobCreator = lobHandler.getLobCreator()) { + if (attributeNames.size() > 1) { + this.jdbcOperations.batchUpdate(this.createSessionAttributeQuery, new BatchPreparedStatementSetter() { - @Override - public void setValues(PreparedStatement ps, int i) throws SQLException { - String attributeName = attributeNames.get(i); + @Override + public void setValues(PreparedStatement ps, int i) throws SQLException { + String attributeName = attributeNames.get(i); + ps.setString(1, attributeName); + lobCreator.setBlobAsBytes(ps, 2, + serialize(session.getAttribute(attributeName))); + ps.setString(3, session.getId()); + } + + @Override + public int getBatchSize() { + return attributeNames.size(); + } + + }); + } else { + this.jdbcOperations.update(this.createSessionAttributeQuery, (ps) -> { + String attributeName = attributeNames.get(0); ps.setString(1, attributeName); - getLobHandler().getLobCreator().setBlobAsBytes(ps, 2, - serialize(session.getAttribute(attributeName))); + lobCreator.setBlobAsBytes(ps, 2, serialize(session.getAttribute(attributeName))); ps.setString(3, session.getId()); - } - - @Override - public int getBatchSize() { - return attributeNames.size(); - } - - }); - } - else { - this.jdbcOperations.update(this.createSessionAttributeQuery, (ps) -> { - String attributeName = attributeNames.get(0); - ps.setString(1, attributeName); - getLobHandler().getLobCreator().setBlobAsBytes(ps, 2, serialize(session.getAttribute(attributeName))); - ps.setString(3, session.getId()); - }); + }); + } } } private void updateSessionAttributes(JdbcSession session, List attributeNames) { Assert.notEmpty(attributeNames, "attributeNames must not be null or empty"); - if (attributeNames.size() > 1) { - this.jdbcOperations.batchUpdate(this.updateSessionAttributeQuery, new BatchPreparedStatementSetter() { + try (LobCreator lobCreator = lobHandler.getLobCreator()) { + if (attributeNames.size() > 1) { + this.jdbcOperations.batchUpdate(this.updateSessionAttributeQuery, new BatchPreparedStatementSetter() { - @Override - public void setValues(PreparedStatement ps, int i) throws SQLException { - String attributeName = attributeNames.get(i); - getLobHandler().getLobCreator().setBlobAsBytes(ps, 1, - serialize(session.getAttribute(attributeName))); + @Override + public void setValues(PreparedStatement ps, int i) throws SQLException { + String attributeName = attributeNames.get(i); + lobCreator.setBlobAsBytes(ps, 1, + serialize(session.getAttribute(attributeName))); + ps.setString(2, session.primaryKey); + ps.setString(3, attributeName); + } + + @Override + public int getBatchSize() { + return attributeNames.size(); + } + + }); + } else { + this.jdbcOperations.update(this.updateSessionAttributeQuery, (ps) -> { + String attributeName = attributeNames.get(0); + lobCreator.setBlobAsBytes(ps, 1, serialize(session.getAttribute(attributeName))); ps.setString(2, session.primaryKey); ps.setString(3, attributeName); - } - - @Override - public int getBatchSize() { - return attributeNames.size(); - } - - }); - } - else { - this.jdbcOperations.update(this.updateSessionAttributeQuery, (ps) -> { - String attributeName = attributeNames.get(0); - getLobHandler().getLobCreator().setBlobAsBytes(ps, 1, serialize(session.getAttribute(attributeName))); - ps.setString(2, session.primaryKey); - ps.setString(3, attributeName); - }); + }); + } } } diff --git a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java index aed37e53..ef3828c9 100644 --- a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java +++ b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java @@ -35,6 +35,8 @@ import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.PreparedStatementSetter; import org.springframework.jdbc.core.ResultSetExtractor; +import org.springframework.jdbc.support.lob.DefaultLobHandler; +import org.springframework.jdbc.support.lob.TemporaryLobCreator; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.authority.AuthorityUtils; @@ -53,9 +55,12 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.ArgumentMatchers.startsWith; import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; /** * Tests for {@link JdbcIndexedSessionRepository}. @@ -710,4 +715,19 @@ class JdbcIndexedSessionRepositoryTests { verifyNoMoreInteractions(this.jdbcOperations); } + @Test + void saveAndFreeTemporaryLob() { + DefaultLobHandler lobHandler = mock(DefaultLobHandler.class); + TemporaryLobCreator lobCreator = mock(TemporaryLobCreator.class); + when(lobHandler.getLobCreator()).thenReturn(lobCreator); + lobHandler.setCreateTemporaryLob(true); + this.repository.setLobHandler(lobHandler); + + JdbcSession session = this.repository.new JdbcSession(new MapSession(), "primaryKey", false); + session.setAttribute("testName", "testValue"); + this.repository.save(session); + + verify(lobCreator, atLeastOnce()).close(); + } + }