简体   繁体   中英

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.

Each of these classes are annotated with @Transactional respectively with @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 ?

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.

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM