1
votes

Have 2 configuration files in a project. Both having Transaction management for different datasources using aspectj as follows-

context1.xml

<!-- Creating TransactionManager Bean, since JDBC we are creating of type 
    DataSourceTransactionManager -->
<bean id="transactionManager1"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource1" />
</bean>

<!-- MySQL DB DataSource -->
<bean id="dataSource1"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/TestDB" />
    <property name="username" value="t" />
    <property name="password" value="t123" />
</bean>

context2. xml

<!-- Creating TransactionManager Bean, since JDBC we are creating of type 
    DataSourceTransactionManager -->
<bean id="transactionManager2"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource2" />
</bean>

<!-- MySQL DB DataSource -->
<bean id="dataSource2"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/TestDB2" />
    <property name="username" value="t2" />
    <property name="password" value="t2123" />
</bean>

This causes setting of transactionManager property of Abstract class - 'org.springframework.transaction.interceptor.TransactionAspectSupport to the last annotation transaction aspect bean created during server startup.

Thus if the last bean instantiated is of dataSource1, the transactions are created on dataSource1, else for dataSource2. How can this issue be resolved?

in a DAO class for TransactionManagement use transaction as follows-

@Transactional(propagation = Propagation.REQUIRED, rollbackFor =   customException.class)
@Override
public void addDoc(param doc) throws customException{

    addOrUpdate(testQuery, doc, "add new doc");
}

Instead of @transactional("tansactionManager2"), @transactional for transactionManager2 is used only at 2 places. Can we implement non AOP implement of transaction for these methods so that there are not many changes?

1

1 Answers

0
votes

You can use qualifiers to indicate the transaction manager to be used:

 <tx:annotation-driven/>
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource1" />
    <qualifier value="transactionManager1"/>
</bean>
<bean id="transactionManager2"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource2" />
    <qualifier value="transactionManager2"/>
</bean>

and annotate your service methods with @Transactional("transactionManager1") or @Transactional("transactionManager2").

For more details on how to define multiple transaction managers see Spring DOC