简体   繁体   English

使用aspectj进行Spring事务管理

[英]Spring Transaction Management using aspectj

Have 2 configuration files in a project. 一个项目中有2个配置文件。 Both having Transaction management for different datasources using aspectj as follows- 都使用aspectj对不同数据源进行事务管理,如下所示:

context1.xml 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. 上下文2。 xml 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. 这导致将Abstract类的'transorg.springframework.transaction.interceptor.TransactionAspectSupport的transactionManager属性设置为服务器启动期间创建的最后一个注释事务方面Bean。

Thus if the last bean instantiated is of dataSource1, the transactions are created on dataSource1, else for dataSource2. 因此,如果最后实例化的bean是dataSource1,则在dataSource1上创建事务,否则在dataSource2上创建事务。 How can this issue be resolved? 如何解决这个问题?

in a DAO class for TransactionManagement use transaction as follows- 在用于TransactionManagement的DAO类中,使用事务如下:

@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. @transactional代替@transactional(“ tansactionManager2”),仅在2个地方使用@transactional。 Can we implement non AOP implement of transaction for these methods so that there are not many changes? 我们可以为这些方法实现非AOP的事务实现方式,以使更改不多吗?

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") . 并使用@Transactional("transactionManager1")@Transactional("transactionManager2")注释您的服务方法。

For more details on how to define multiple transaction managers see Spring DOC 有关如何定义多个事务管理器的更多详细信息,请参见Spring DOC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM