[英]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.