I have 2 databases (MySql and HSQLDB). I configured 2 data sources and 2 EntityManagerFactory beans. I can also configure 2 correspondent JpaTransactionManager beans.
But I don't know how to specify which of them should be used to manage transactions for concrete service-class. I want to use @Transactional
annotation for that purpose, but I actually can specify only one of txManagers:
<tx:annotation-driven transaction-manager="manager"/>
What is the way out from this situation?
Since its after a longtime since the correct answers.
Skaffman may be correct in terms of usability of JpaTransactionManager for multiple databases.
But there is working solution for using 2 different databases with 2 different JpaTransactionManager.
@Primary
should be used to specify for the ones where you don't specify qualifier name in@Transactional
The javadoc for JpaTransactionManager has some advice on this:
In other words, if you find yourself with multiple entity managers, with corresponding tx managers, then you should consider using a single
JtaTransactionManager
instead. The entity managers should be able to participate in JTA transactions, and this will give you full transactionality across both entity managers, without hacving to worry about which entity manager you're in at any one time.Of course,
JtaTransactionManager
does require a full JTA-supporting application server, rather than a vanilla servlet engine like Tomcat.Declare your
<tx:annotation-driven>
without transaction-manager attribute, declare qualifiers for transaction managers like this:Use this qualifier in @Transactional as a value to select one of transaction managers:
or, with more properties:
You have to specify two transaction managers for that in application-context.xml as below:
@Transactional attribute will now use its relevant transaction manager.