Skip to content

Commit bf77aaa

Browse files
author
mhyeon-lee
committed
[DATAJDBC-493] Add acquire lock before execute delete aggregate root
1 parent 7bb2beb commit bf77aaa

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
4040
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
4141
import org.springframework.data.relational.core.mapping.event.*;
42+
import org.springframework.data.relational.core.sql.LockMode;
4243
import org.springframework.lang.Nullable;
44+
import org.springframework.transaction.support.TransactionSynchronizationManager;
4345
import org.springframework.util.Assert;
4446

4547
/**
@@ -50,6 +52,7 @@
5052
* @author Thomas Lang
5153
* @author Christoph Strobl
5254
* @author Milan Milanov
55+
* @author Myeonghyeon Lee
5356
*/
5457
public class JdbcAggregateTemplate implements JdbcAggregateOperations {
5558

@@ -352,6 +355,9 @@ private <T> void deleteTree(Object id, @Nullable T entity, Class<T> domainType)
352355
entity = triggerBeforeDelete(entity, id, change);
353356
change.setEntity(entity);
354357

358+
// [DATAJDBC-493] Acquire Lock to avoid DeadLock
359+
this.acquireLockIfActualTransactionActive(id, domainType);
360+
355361
executor.execute(change);
356362

357363
triggerAfterDelete(entity, id, change);
@@ -441,4 +447,10 @@ private <T> T triggerBeforeDelete(@Nullable T aggregateRoot, Object id, MutableA
441447

442448
return null;
443449
}
450+
451+
private <T> void acquireLockIfActualTransactionActive(Object id, Class<T> domainType) {
452+
if (TransactionSynchronizationManager.isActualTransactionActive()) {
453+
this.accessStrategy.findByIdWithLock(id, LockMode.PESSIMISTIC_WRITE, domainType);
454+
}
455+
}
444456
}

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryConcurrencyIntegrationTests.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import lombok.Getter;
2121
import lombok.With;
2222
import org.junit.ClassRule;
23-
import org.junit.Ignore;
2423
import org.junit.Rule;
2524
import org.junit.Test;
2625
import org.springframework.beans.factory.annotation.Autowired;
@@ -36,7 +35,6 @@
3635
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
3736
import org.springframework.test.annotation.IfProfileValue;
3837
import org.springframework.test.annotation.ProfileValueSourceConfiguration;
39-
import org.springframework.test.context.ActiveProfiles;
4038
import org.springframework.test.context.ContextConfiguration;
4139
import org.springframework.test.context.junit4.rules.SpringClassRule;
4240
import org.springframework.test.context.junit4.rules.SpringMethodRule;
@@ -126,7 +124,6 @@ public void updateConcurrencyWithEmptyReferences() throws Exception {
126124
}
127125

128126
@Test // DATAJDBC-493
129-
@Ignore("failing test")
130127
public void updateConcurrencyWithDelete() throws Exception {
131128

132129
DummyEntity entity = createDummyEntity();

0 commit comments

Comments
 (0)