I have configured two persistent units with the entity managers set up as show below:
Actually, there is a way to use named TransactionManager with Spring Data JPA. This works for me:
<bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEntityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="myTransactionManager"/>
<jpa:repositories base-package="com.xxx.yyy" entity-manager-factory-ref="myEntityManagerFactory" transaction-manager-ref="myTransactionManager">
</jpa:repositories>
I use java configuration and specifying the transactionManagerRef was solution for me:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "myCustomEntityManagerFactory",
basePackages = {"ua.demitt.other.path.to.repos"},
transactionManagerRef = "myCustomTransactionManager" )
The default value for the transaction-manager attribute is transaction-manager. In your case, you should specify which transaction manager you want to use per method or service like this:
@Service
@Transactional(value="LiveTransactionManager")
class someClass...
or
@Transactional(value="ArchiveTransactionManager")
public void someMethod
In your @Configuration file, if this is your dataSource:
@Bean(name = "dataSource")
public DataSource getDataSource() {
return DataSourceBuilder
.create()
.username(username)
.password(password)
.url(url)
.build();
}
this would be your transaction manager bean:
@Bean(name = "DataSourceTransactionManager")
public DataSourceTransactionManager getDataSourceTransactionManager() {
return new DataSourceTransactionManager(getDataSource());
}
Then annotate the service method you would like to be transactional:
@Transactional(transactionManager = "DataSourceTransactionManager", timeout = 60, rollbackFor = { Exception.class})