Skip to content

Start JDBC transactions only when there is an update #3330

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

Merged
Merged
Show file tree
Hide file tree
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 @@ -907,8 +907,9 @@ private void save() {
});
}
else {
JdbcIndexedSessionRepository.this.transactionOperations.executeWithoutResult((status) -> {
if (JdbcSession.this.changed) {
List<Runnable> deltaActions = JdbcSession.this.changed ? new ArrayList<>(4) : new ArrayList<>();
if (JdbcSession.this.changed) {
deltaActions.add(() -> {
Map<String, String> indexes = JdbcIndexedSessionRepository.this.indexResolver
.resolveIndexesFor(JdbcSession.this);
JdbcIndexedSessionRepository.this.jdbcOperations
Expand All @@ -920,32 +921,43 @@ private void save() {
ps.setString(5, indexes.get(PRINCIPAL_NAME_INDEX_NAME));
ps.setString(6, JdbcSession.this.primaryKey);
});
}
List<String> addedAttributeNames = JdbcSession.this.delta.entrySet()
.stream()
.filter((entry) -> entry.getValue() == DeltaValue.ADDED)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
if (!addedAttributeNames.isEmpty()) {
insertSessionAttributes(JdbcSession.this, addedAttributeNames);
}
List<String> updatedAttributeNames = JdbcSession.this.delta.entrySet()
.stream()
.filter((entry) -> entry.getValue() == DeltaValue.UPDATED)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
if (!updatedAttributeNames.isEmpty()) {
updateSessionAttributes(JdbcSession.this, updatedAttributeNames);
}
List<String> removedAttributeNames = JdbcSession.this.delta.entrySet()
.stream()
.filter((entry) -> entry.getValue() == DeltaValue.REMOVED)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
if (!removedAttributeNames.isEmpty()) {
deleteSessionAttributes(JdbcSession.this, removedAttributeNames);
}
});
});
}

List<String> addedAttributeNames = JdbcSession.this.delta.entrySet()
.stream()
.filter((entry) -> entry.getValue() == DeltaValue.ADDED)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
if (!addedAttributeNames.isEmpty()) {
deltaActions.add(() -> insertSessionAttributes(JdbcSession.this, addedAttributeNames));
}

List<String> updatedAttributeNames = JdbcSession.this.delta.entrySet()
.stream()
.filter((entry) -> entry.getValue() == DeltaValue.UPDATED)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
if (!updatedAttributeNames.isEmpty()) {
deltaActions.add(() -> updateSessionAttributes(JdbcSession.this, updatedAttributeNames));
}

List<String> removedAttributeNames = JdbcSession.this.delta.entrySet()
.stream()
.filter((entry) -> entry.getValue() == DeltaValue.REMOVED)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
if (!removedAttributeNames.isEmpty()) {
deltaActions.add(() -> deleteSessionAttributes(JdbcSession.this, removedAttributeNames));
}

if (!deltaActions.isEmpty()) {
JdbcIndexedSessionRepository.this.transactionOperations.executeWithoutResult((status) -> {
for (Runnable action : deltaActions) {
action.run();
}
});
}
}
clearChangeFlags();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -48,17 +49,21 @@
import org.springframework.session.SaveMode;
import org.springframework.session.Session;
import org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionOperations;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.ArgumentMatchers.matches;
import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -78,12 +83,26 @@ class JdbcIndexedSessionRepositoryTests {
@Mock
private JdbcOperations jdbcOperations;

@Mock
private TransactionOperations transactionOperations;

private JdbcIndexedSessionRepository repository;

@BeforeEach
void setUp() {
this.repository = new JdbcIndexedSessionRepository(this.jdbcOperations,
TransactionOperations.withoutTransaction());
// Mock transaction callbacks to the real consumer
lenient().doAnswer((answer) -> {
answer.getArgument(0, Consumer.class).accept(mock(TransactionStatus.class));
return null;
}).when(this.transactionOperations).executeWithoutResult(any());

lenient()
.doAnswer((answer) -> answer.getArgument(0, TransactionCallback.class)
.doInTransaction(mock(TransactionStatus.class)))
.when(this.transactionOperations)
.execute(any());

this.repository = new JdbcIndexedSessionRepository(this.jdbcOperations, this.transactionOperations);
}

@Test
Expand Down Expand Up @@ -467,6 +486,7 @@ void saveUpdatedAddAndRemoveAttribute() {

assertThat(session.isNew()).isFalse();
verifyNoMoreInteractions(this.jdbcOperations);
verifyNoMoreInteractions(this.transactionOperations);
}

@Test // gh-1070
Expand Down
Loading