简体   繁体   English

在单个事务中运行spring @Transactional注释方法

[英]Run spring @Transactional annotated methods in a single transaction

We have several classes for writing to and reading from our database layer in a Command-Query-Separation fashion. 我们有几个类,用于以Command-Query-Separation方式写入和读取数据库层。

Each of these classes are annotated with @Transactional respectively with @Transactional(readOnly = true). 这些类中的每一个都分别用@Transactional注释@Transactional(readOnly = true)。

Now we've implemented a data import which is creating many objects and writing them to the database using our command-layer. 现在我们已经实现了一个数据导入,它创建了许多对象并使用我们的命令层将它们写入数据库。

For now, each write of a single object is done within its own transaction , and I am pretty sure that's a major performance killer. 目前,每个单个对象的写入都是在自己的事务中完成的 ,我很确定这是一个主要的性能杀手。

Is there a simple way to annotate the data import class in a way, that even though it calls many @Transactional annotated methods in the command layer many times, all would be done in a single transaction ? 是否有一种简单的方法以某种方式注释数据导入类,即使它多次在命令层中调用许多@Transactional注释方法,所有这些都将在单个事务中完成

Is this even a valid approach for improving insertion-performance, or would you consider this a bad idea? 这甚至是提高插入性能的有效方法,还是您认为这是一个坏主意?

Mistake One: I used an ExecutorService, as I read in this post How to use spring transaction in multithread spring does not support multi-threading in combination with transactions. 错误一:我使用了ExecutorService,正如我在这篇文章中所读到的。 如何在多线程弹簧中使用spring事务不支持多线程与事务相结合。

Mistake Two: I replaced the ExecutorService, called the spring managed class method with the annotation @Transactional directly, but this time it didn't work since the class was nested within the calling class, thus it was not regarded as a call "from outside". 错误二:我替换了ExecutorService,直接使用注释@Transactional调用了spring托管类方法,但是这次它没有工作,因为类嵌套在调用类中,因此它不被视为“来自外部的调用” ”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @Transactional 注释方法是否等待 Spring 中的成功提交? - Do @Transactional annotated methods wait for a successful commit in Spring? 在事务方法 spring 中运行没有事务的代码 - Run code without transaction in transactional mehod spring Spring Boot 使用@Transactional 注解方法,调用其他事务性方法并抛出异常 - Spring Boot With @Transactional annotated method, calls other transactional methods and throws an Exception 覆盖具有 Spring 的 @transactional 的方法的事务传播级别 - Overriding transaction propagation levels for methods having Spring's @transactional 使用 Java Spring 和 Hibernate 和 EntityManager 的 @Transactional 方法中的事务提交问题 - Problem with transaction commit in @Transactional methods using Java Spring and Hibernate and EntityManager Spring 为在 @Transactional 注释方法中调用的每个 JpaRepository 方法打开一个新事务 - Spring opens a new transaction for each JpaRepository method that is called within an @Transactional annotated method 在Spring的@Transactional注释的方法中引发并捕获的未经检查的异常是否还会导致事务回滚? - Will an unchecked exception thrown and caught inside a method annotated with spring's @Transactional still cause the transaction to rollback? 带有代理的@transactional注释类,但不创建事务 - @transactional annotated class wrapped with proxy, but transaction is not created 为什么必须覆盖使用@Transactional注释的方法 - Why must methods annotated with @Transactional be overrideable 一个Spring @Transaction中的两个dao方法 - Two dao methods in a single Spring @Transaction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM