Skip to content

Commit 29ff2e4

Browse files
authored
Add try-with-resources to methods to insert BLOB
1 parent dc9da1d commit 29ff2e4

File tree

2 files changed

+69
-46
lines changed

2 files changed

+69
-46
lines changed

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

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.jdbc.core.JdbcOperations;
4545
import org.springframework.jdbc.core.ResultSetExtractor;
4646
import org.springframework.jdbc.support.lob.DefaultLobHandler;
47+
import org.springframework.jdbc.support.lob.LobCreator;
4748
import org.springframework.jdbc.support.lob.LobHandler;
4849
import org.springframework.session.DelegatingIndexResolver;
4950
import org.springframework.session.FindByIndexNameSessionRepository;
@@ -460,63 +461,65 @@ public Map<String, JdbcSession> findByIndexNameAndIndexValue(String indexName, f
460461

461462
private void insertSessionAttributes(JdbcSession session, List<String> attributeNames) {
462463
Assert.notEmpty(attributeNames, "attributeNames must not be null or empty");
463-
if (attributeNames.size() > 1) {
464-
this.jdbcOperations.batchUpdate(this.createSessionAttributeQuery, new BatchPreparedStatementSetter() {
464+
try (LobCreator lobCreator = lobHandler.getLobCreator()) {
465+
if (attributeNames.size() > 1) {
466+
this.jdbcOperations.batchUpdate(this.createSessionAttributeQuery, new BatchPreparedStatementSetter() {
467+
468+
@Override
469+
public void setValues(PreparedStatement ps, int i) throws SQLException {
470+
String attributeName = attributeNames.get(i);
471+
ps.setString(1, attributeName);
472+
lobCreator.setBlobAsBytes(ps, 2,
473+
serialize(session.getAttribute(attributeName)));
474+
ps.setString(3, session.getId());
475+
}
465476

466-
@Override
467-
public void setValues(PreparedStatement ps, int i) throws SQLException {
468-
String attributeName = attributeNames.get(i);
477+
@Override
478+
public int getBatchSize() {
479+
return attributeNames.size();
480+
}
481+
482+
});
483+
} else {
484+
this.jdbcOperations.update(this.createSessionAttributeQuery, (ps) -> {
485+
String attributeName = attributeNames.get(0);
469486
ps.setString(1, attributeName);
470-
getLobHandler().getLobCreator().setBlobAsBytes(ps, 2,
471-
serialize(session.getAttribute(attributeName)));
487+
lobCreator.setBlobAsBytes(ps, 2, serialize(session.getAttribute(attributeName)));
472488
ps.setString(3, session.getId());
473-
}
474-
475-
@Override
476-
public int getBatchSize() {
477-
return attributeNames.size();
478-
}
479-
480-
});
481-
}
482-
else {
483-
this.jdbcOperations.update(this.createSessionAttributeQuery, (ps) -> {
484-
String attributeName = attributeNames.get(0);
485-
ps.setString(1, attributeName);
486-
getLobHandler().getLobCreator().setBlobAsBytes(ps, 2, serialize(session.getAttribute(attributeName)));
487-
ps.setString(3, session.getId());
488-
});
489+
});
490+
}
489491
}
490492
}
491493

492494
private void updateSessionAttributes(JdbcSession session, List<String> attributeNames) {
493495
Assert.notEmpty(attributeNames, "attributeNames must not be null or empty");
494-
if (attributeNames.size() > 1) {
495-
this.jdbcOperations.batchUpdate(this.updateSessionAttributeQuery, new BatchPreparedStatementSetter() {
496+
try (LobCreator lobCreator = lobHandler.getLobCreator()) {
497+
if (attributeNames.size() > 1) {
498+
this.jdbcOperations.batchUpdate(this.updateSessionAttributeQuery, new BatchPreparedStatementSetter() {
499+
500+
@Override
501+
public void setValues(PreparedStatement ps, int i) throws SQLException {
502+
String attributeName = attributeNames.get(i);
503+
lobCreator.setBlobAsBytes(ps, 1,
504+
serialize(session.getAttribute(attributeName)));
505+
ps.setString(2, session.primaryKey);
506+
ps.setString(3, attributeName);
507+
}
496508

497-
@Override
498-
public void setValues(PreparedStatement ps, int i) throws SQLException {
499-
String attributeName = attributeNames.get(i);
500-
getLobHandler().getLobCreator().setBlobAsBytes(ps, 1,
501-
serialize(session.getAttribute(attributeName)));
509+
@Override
510+
public int getBatchSize() {
511+
return attributeNames.size();
512+
}
513+
514+
});
515+
} else {
516+
this.jdbcOperations.update(this.updateSessionAttributeQuery, (ps) -> {
517+
String attributeName = attributeNames.get(0);
518+
lobCreator.setBlobAsBytes(ps, 1, serialize(session.getAttribute(attributeName)));
502519
ps.setString(2, session.primaryKey);
503520
ps.setString(3, attributeName);
504-
}
505-
506-
@Override
507-
public int getBatchSize() {
508-
return attributeNames.size();
509-
}
510-
511-
});
512-
}
513-
else {
514-
this.jdbcOperations.update(this.updateSessionAttributeQuery, (ps) -> {
515-
String attributeName = attributeNames.get(0);
516-
getLobHandler().getLobCreator().setBlobAsBytes(ps, 1, serialize(session.getAttribute(attributeName)));
517-
ps.setString(2, session.primaryKey);
518-
ps.setString(3, attributeName);
519-
});
521+
});
522+
}
520523
}
521524
}
522525

spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.springframework.jdbc.core.JdbcOperations;
3636
import org.springframework.jdbc.core.PreparedStatementSetter;
3737
import org.springframework.jdbc.core.ResultSetExtractor;
38+
import org.springframework.jdbc.support.lob.DefaultLobHandler;
39+
import org.springframework.jdbc.support.lob.TemporaryLobCreator;
3840
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
3941
import org.springframework.security.core.Authentication;
4042
import org.springframework.security.core.authority.AuthorityUtils;
@@ -53,9 +55,12 @@
5355
import static org.mockito.ArgumentMatchers.isA;
5456
import static org.mockito.ArgumentMatchers.startsWith;
5557
import static org.mockito.BDDMockito.given;
58+
import static org.mockito.Mockito.atLeastOnce;
59+
import static org.mockito.Mockito.mock;
5660
import static org.mockito.Mockito.times;
5761
import static org.mockito.Mockito.verify;
5862
import static org.mockito.Mockito.verifyNoMoreInteractions;
63+
import static org.mockito.Mockito.when;
5964

6065
/**
6166
* Tests for {@link JdbcIndexedSessionRepository}.
@@ -710,4 +715,19 @@ void flushModeSetLastAccessedTime() {
710715
verifyNoMoreInteractions(this.jdbcOperations);
711716
}
712717

718+
@Test
719+
void saveAndFreeTemporaryLob() {
720+
DefaultLobHandler lobHandler = mock(DefaultLobHandler.class);
721+
TemporaryLobCreator lobCreator = mock(TemporaryLobCreator.class);
722+
when(lobHandler.getLobCreator()).thenReturn(lobCreator);
723+
lobHandler.setCreateTemporaryLob(true);
724+
this.repository.setLobHandler(lobHandler);
725+
726+
JdbcSession session = this.repository.new JdbcSession(new MapSession(), "primaryKey", false);
727+
session.setAttribute("testName", "testValue");
728+
this.repository.save(session);
729+
730+
verify(lobCreator, atLeastOnce()).close();
731+
}
732+
713733
}

0 commit comments

Comments
 (0)