Skip to content

Create explicit transactional boundaries #2859

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

Closed
wants to merge 2 commits into from
Closed
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 @@ -75,10 +75,12 @@ public Configuration cypherDslConfiguration() {
public Neo4jOperations neo4jOperations(
@Any Instance<Neo4jClient> neo4jClient,
@Any Instance<Neo4jMappingContext> mappingContext,
@Any Instance<Configuration> cypherDslConfiguration
@Any Instance<Configuration> cypherDslConfiguration,
@Any Instance<PlatformTransactionManager> transactionManager
) {
Neo4jTemplate neo4jTemplate = new Neo4jTemplate(resolve(neo4jClient), resolve(mappingContext));
neo4jTemplate.setCypherRenderer(Renderer.getRenderer(resolve(cypherDslConfiguration)));
neo4jTemplate.setTransactionManager(resolve(transactionManager));
return neo4jTemplate;
}

Expand Down
473 changes: 275 additions & 198 deletions src/main/java/org/springframework/data/neo4j/core/Neo4jTemplate.java

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws

// Otherwise we open a session and synchronize it.
Session session = driver.session(Neo4jTransactionUtils.defaultSessionConfig(targetDatabase, asUser));
Transaction transaction = session.beginTransaction(TransactionConfig.empty());
Transaction transaction = session.beginTransaction(Neo4jTransactionUtils.createTransactionConfigFrom(TransactionDefinition.withDefaults(), -1));
// Manually create a new synchronization
connectionHolder = new Neo4jTransactionHolder(new Neo4jTransactionContext(targetDatabase, asUser), session, transaction);
connectionHolder.setSynchronizedWithTransaction(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public static Mono<ReactiveTransaction> retrieveReactiveTransaction(
return Mono.defer(() -> {

ReactiveSession session = driver.session(ReactiveSession.class, Neo4jTransactionUtils.defaultSessionConfig(targetDatabase, asUser));
return Mono.fromDirect(session.beginTransaction(TransactionConfig.empty())).map(tx -> {
return Mono.fromDirect(session.beginTransaction(Neo4jTransactionUtils.createTransactionConfigFrom(TransactionDefinition.withDefaults(), -1))).map(tx -> {

ReactiveNeo4jTransactionHolder newConnectionHolder = new ReactiveNeo4jTransactionHolder(
new Neo4jTransactionContext(targetDatabase, asUser), session, tx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -880,20 +881,22 @@ void saveWeirdHierarchy() {
}

@Test
void updatingFindShouldWork() {
void updatingFindShouldWork(@Autowired PlatformTransactionManager transactionManager) {
Map<String, Object> params = new HashMap<>();
params.put("wrongName", "Siemons");
params.put("correctName", "Simons");
Optional<Person> optionalResult = neo4jTemplate
.findOne("MERGE (p:Person {lastName: $wrongName}) ON MATCH set p.lastName = $correctName RETURN p",
params, Person.class);

assertThat(optionalResult).hasValueSatisfying(
updatedPerson -> {
assertThat(updatedPerson.getLastName()).isEqualTo("Simons");
assertThat(updatedPerson.getAddress()).isNull(); // We didn't fetch it
}
);
new TransactionTemplate(transactionManager).executeWithoutResult(tx -> {
Optional<Person> optionalResult = neo4jTemplate
.findOne("MERGE (p:Person {lastName: $wrongName}) ON MATCH set p.lastName = $correctName RETURN p",
params, Person.class);

assertThat(optionalResult).hasValueSatisfying(
updatedPerson -> {
assertThat(updatedPerson.getLastName()).isEqualTo("Simons");
assertThat(updatedPerson.getAddress()).isNull(); // We didn't fetch it
}
);
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,12 @@ void aggregateThroughQueryIntoCustomObjectDTOShouldWork(@Autowired PersonReposit
}

@Test // DATAGRAPH-1429
void queryAggregatesShouldWorkWithTheTemplate(@Autowired Neo4jTemplate template) {
void queryAggregatesShouldWorkWithTheTemplate(@Autowired Neo4jTemplate template, @Autowired PlatformTransactionManager transactionManager) {
new TransactionTemplate(transactionManager).executeWithoutResult(tx -> {

List<Person> people = template.findAll("unwind range(1,5) as i with i create (p:Person {firstName: toString(i)}) return p", Person.class);
assertThat(people).extracting(Person::getFirstName).containsExactly("1", "2", "3", "4", "5");
List<Person> people = template.findAll("unwind range(1,5) as i with i create (p:Person {firstName: toString(i)}) return p", Person.class);
assertThat(people).extracting(Person::getFirstName).containsExactly("1", "2", "3", "4", "5");
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.springframework.data.neo4j.test.Neo4jReactiveTestConfiguration;
import org.springframework.transaction.ReactiveTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.reactive.TransactionalOperator;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

Expand Down Expand Up @@ -533,9 +534,10 @@ void saveAllProjectionShouldWork(@Autowired ReactiveNeo4jTemplate template) {
}

@Test
void saveAllAsWithOpenProjectionShouldWork(@Autowired ReactiveNeo4jTemplate template) {
void saveAllAsWithOpenProjectionShouldWork(@Autowired ReactiveNeo4jTemplate template, @Autowired ReactiveTransactionManager transactionManager) {

// Using a query on purpose so that the address is null
TransactionalOperator.create(transactionManager).transactional(
template.findOne("MATCH (p:Person {lastName: $lastName}) RETURN p",
Collections.singletonMap("lastName", "Siemons"), Person.class)
.zipWith(template.findOne("MATCH (p:Person {lastName: $lastName}) RETURN p",
Expand All @@ -550,7 +552,7 @@ void saveAllAsWithOpenProjectionShouldWork(@Autowired ReactiveNeo4jTemplate temp
p2.setFirstName("Helga");
p2.setLastName("Schneider");
return template.saveAllAs(Arrays.asList(p1, p2), OpenProjection.class);
})
}))
.map(OpenProjection::getFullName)
.sort()
.as(StepVerifier::create)
Expand Down Expand Up @@ -832,13 +834,15 @@ void saveAllAsWithClosedProjectionShouldWork(@Autowired ReactiveNeo4jTemplate te
}

@Test
void updatingFindShouldWork() {
void updatingFindShouldWork(@Autowired ReactiveTransactionManager transactionManager) {
Map<String, Object> params = new HashMap<>();
params.put("wrongName", "Siemons");
params.put("correctName", "Simons");
neo4jTemplate
.findOne("MERGE (p:Person {lastName: $wrongName}) ON MATCH set p.lastName = $correctName RETURN p",
params, Person.class)
TransactionalOperator.create(transactionManager)
.transactional(
neo4jTemplate
.findOne("MERGE (p:Person {lastName: $wrongName}) ON MATCH set p.lastName = $correctName RETURN p",
params, Person.class))
.as(StepVerifier::create)
.consumeNextWith(updatedPerson -> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,9 @@ void aggregateThroughQueryIntoListShouldWork(@Autowired ReactivePersonRepository
}

@Test // DATAGRAPH-1429
void queryAggregatesShouldWorkWithTheTemplate(@Autowired ReactiveNeo4jTemplate template) {
void queryAggregatesShouldWorkWithTheTemplate(@Autowired ReactiveNeo4jTemplate template, @Autowired ReactiveTransactionManager reactiveTransactionManager) {

Flux<Person> people = template.findAll("unwind range(1,5) as i with i create (p:Person {firstName: toString(i)}) return p", Person.class);
Flux<Person> people = TransactionalOperator.create(reactiveTransactionManager).transactional(template.findAll("unwind range(1,5) as i with i create (p:Person {firstName: toString(i)}) return p", Person.class));

StepVerifier.create(people.map(Person::getFirstName))
.expectNext("1", "2", "3", "4", "5")
Expand Down