0

I'm trying to read from 1 datasource (A) and write the data from that datasource (A) to another datasource (B). I set up all my Java Spring 5 beans via annotation as I need to create placeholder datasources and provide the login details at runtime.

I've set up 2 LocalContainerEntityManagerFactoryBeans (using datasource A and B) and a JpaTransactionManager (B), call this transaction manager 'tm' - all named.

In my DAO I have

@Transactional('tm')
@Repository
public interface SomeDao extends JpaRepository<SomeModel, Long> {
    @Query("select soemthing something...")
    List<SomeModel> getter(Somefields foobar);

    @Modifying
    @Query("insert something something...")
    void insert(Somefields morestuff);

So back where I defined my JPA config/beans I have a

@Configuration
@EnableJpaRepositories(
    entityManagerFactoryRef = "A",
    transactionManagerRef = "B"
)

Which loads fine apparently as I can run the getter and get the correct data back from datasource A. As I try to use the insert method - ideally I want to write to datasource B, it just throws up a javax.persistence.TransactionRequiredException. Even without the insert and using the CrudRepository .save(Object S) inherited by my DAO, it won't insert into the database, and calling my DAO.flush() will also throw the no Transaction in progress.

Does calling @Transactional('tm') not automatically create some transaction? I don't understand why I am getting this error regardless of my attempts. Can I even do what I'm doing with these two entitymanagers and jpatransaction managers with the various datasources?

  • Does the following help? https://stackoverflow.com/questions/75700/jpa-multiple-transaction-managers/78479#78479 – toolkit Aug 09 '18 at 23:40
  • You simply misconfigured your transactionManagers – akuma8 Aug 09 '18 at 23:42
  • I didn't misconfigure, it was because I was setting my datasources at run time, which meant that the JpaTransactionManager, despite giving it an EntityManagerFactory, gets an EntityManager from it at bean creation and continues to use that. – anonymous frog Aug 11 '18 at 08:44

0 Answers0