From 91145b08fc512f6d4148c0e1c0b13821ee7d6a23 Mon Sep 17 00:00:00 2001 From: Kohei Tamura Date: Mon, 29 Jun 2020 23:25:16 +0900 Subject: [PATCH 1/2] Add try-with-resources to methods to insert BLOB --- .../jdbc/JdbcIndexedSessionRepository.java | 95 ++++++++++--------- 1 file changed, 49 insertions(+), 46 deletions(-) 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 6f1a0bd09..cd849fced 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.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 Map findByIndexNameAndIndexValue(String indexName, f 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); + ps.setString(1, attributeName); + lobCreator.setBlobAsBytes(ps, 2, + serialize(session.getAttribute(attributeName))); + ps.setString(3, session.getId()); + } - @Override - public void setValues(PreparedStatement ps, int i) throws SQLException { - String attributeName = attributeNames.get(i); + @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); + lobCreator.setBlobAsBytes(ps, 1, + serialize(session.getAttribute(attributeName))); + ps.setString(2, session.primaryKey); + ps.setString(3, attributeName); + } - @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 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); - }); + }); + } } } From cd250dd4cf5df282f64cb32d5a45adbba4f1a097 Mon Sep 17 00:00:00 2001 From: k-tamura Date: Wed, 22 Jul 2020 23:00:56 +0900 Subject: [PATCH 2/2] Add a test method --- .../JdbcIndexedSessionRepositoryTests.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) 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 aed37e535..ef3828c9e 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.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.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 @@ void flushModeSetLastAccessedTime() { 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(); + } + }