Spring 数据 Neo4j 6 中的@Transactional
@Transactional in Spring Data Neo4j 6
我有一个使用 Neo4j SDN (v6.1.1) 的简单 Spring 引导项目。问题是,当我用@Transactional 标记一个方法时,它似乎没有考虑到这一点(与我使用 Neo4j-OGM 和以前版本的 SDN 时相反)。当我在“template.save”之后检查数据库时,更新在数据库中可见,甚至在整个方法 运行 之前。我该怎么办?
方法:
@Transactional
void method() {
template.deleteAll(Person.class);
template.deleteAll(Club.class);
// save by Neo4jTemplate
Person person_1 = template.save(new Person("Reza", "Mahdavi", 22, new Member(new Club("Iran"), "2021")));
// Save by Repository
Person person_2 = repository.save(new Person("Ali" + new Random(System.currentTimeMillis()).nextInt(), "Alavi", 20, new Member(new Club("Iran"), "2020")));
// Query by Repository
Person ali = repository.findByName("Ali");
// Query by Neo4jTemplate
Optional<Person> reza = template.findById(person_1.getId(), Person.class);
// Custom query
Result result = driver.session().run("Match(n) return count(n) as count", TransactionConfig.builder().build());
System.out.println("Entity Count: " + result.single().values().get(0));
}
另外,我在日志中有这样的句子:
No need to create transaction for [org.springframework.data.neo4j.repository.support.SimpleNeo4jRepository.toString]: This method is not transactional.
代码here
该方法需要具有 public 可见性。否则 Spring 无法围绕此创建所需的基础结构(代理相关)。
文档中有更多关于此的信息 https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#transaction-declarative-annotations
链接部分下的几行有一个框
When you use proxies, you should apply the @Transactional annotation
only to methods with public visibility. If you do annotate protected,
private or package-visible methods with the @Transactional annotation,
no error is raised, but the annotated method does not exhibit the
configured transactional settings. If you need to annotate non-public
methods, consider using AspectJ (described later).
我有一个使用 Neo4j SDN (v6.1.1) 的简单 Spring 引导项目。问题是,当我用@Transactional 标记一个方法时,它似乎没有考虑到这一点(与我使用 Neo4j-OGM 和以前版本的 SDN 时相反)。当我在“template.save”之后检查数据库时,更新在数据库中可见,甚至在整个方法 运行 之前。我该怎么办?
方法:
@Transactional
void method() {
template.deleteAll(Person.class);
template.deleteAll(Club.class);
// save by Neo4jTemplate
Person person_1 = template.save(new Person("Reza", "Mahdavi", 22, new Member(new Club("Iran"), "2021")));
// Save by Repository
Person person_2 = repository.save(new Person("Ali" + new Random(System.currentTimeMillis()).nextInt(), "Alavi", 20, new Member(new Club("Iran"), "2020")));
// Query by Repository
Person ali = repository.findByName("Ali");
// Query by Neo4jTemplate
Optional<Person> reza = template.findById(person_1.getId(), Person.class);
// Custom query
Result result = driver.session().run("Match(n) return count(n) as count", TransactionConfig.builder().build());
System.out.println("Entity Count: " + result.single().values().get(0));
}
另外,我在日志中有这样的句子:
No need to create transaction for [org.springframework.data.neo4j.repository.support.SimpleNeo4jRepository.toString]: This method is not transactional.
代码here
该方法需要具有 public 可见性。否则 Spring 无法围绕此创建所需的基础结构(代理相关)。
文档中有更多关于此的信息 https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#transaction-declarative-annotations 链接部分下的几行有一个框
When you use proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. If you need to annotate non-public methods, consider using AspectJ (described later).