Skip to content
Advertisement

Spring boot “no transaction is in progress” with 2 datasources

I have two databases and i’m trying to save some records to both of them inside a service method.

This gives me the error: org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress.

Here is entities:

JavaScript

And here is one of the config files for multiple db connection:

JavaScript

And here is the other one:

JavaScript

The problem is about the hist entity. If i save just the other one it saves.

But if i try to save the hist entity like this:

JavaScript

it saves the first one but it doesn’t save the hist and when i look at the logs it just calls a select query and not insert.

And if i try to save the hist entity with saveAndFlush method it gives the error.

What is the reason what can i do about it. Is it about config files?

Advertisement

Answer

Both answers posted by @Airy and @GJohannes is pointing out the parts i was missing but there is also one thing i needed to add: @Qualifier("histEntityManager")

Here is the final config file that works for me:

JavaScript

And ofcourse i’m adding @Transactional(transactionManager = "histTransactionManager")

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement