50,526
社区成员
发帖
与我相关
我的任务
分享
<!-- ================================== 配置数据源 ================================== -->
<!-- 数据源A -->
<bean id="dataSourceA" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="mysql/test"/>
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
<property name="xaProperties">
<props>
<prop key="user">${jdbc.username}</prop>
<prop key="password">${jdbc.password}</prop>
<prop key="URL">${jdbc.url_test1}</prop>
</props>
</property>
<property name="poolSize" value="3" />
</bean>
<!-- 数据源B -->
<bean id="dataSourceB" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="mysql/test2"/>
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
<property name="xaProperties">
<props>
<prop key="user">${jdbc.username}</prop>
<prop key="password">${jdbc.password}</prop>
<prop key="URL">${jdbc.url_test2}</prop>
</props>
</property>
<property name="poolSize" value="3" />
</bean>
<!-- ================================== 配置ibatis ============================== -->
<bean id="sqlMapClientA" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref local="dataSourceA" />
</property>
<property name="configLocation">
<value>classpath:sqlmap/SqlMapConfig-a.xml</value>
</property>
</bean>
<bean id="sqlMapClientB" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref local="dataSourceB" />
</property>
<property name="configLocation">
<value>classpath:sqlmap/SqlMapConfig-b.xml</value>
</property>
</bean>
<bean id="daoTargetA" abstract="true">
<property name="sqlMapClient" ref="sqlMapClientA" />
</bean>
<bean id="daoTargetB" abstract="true">
<property name="sqlMapClient" ref="sqlMapClientB" />
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="true" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean>
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
<property name="allowCustomIsolationLevels" value="true"/>
</bean>
<!-- aop配置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="load*" propagation="REQUIRED" read-only="true"/>
<tx:method name="list*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 事务切面配置 -->
<aop:config>
<aop:pointcut id="serviceOperation" expression="execution(* com.dtb.test.service.impl..*.*(..))" />
<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" />
</aop:config>
@Controller("tabAService")
public class TabAServiceImpl implements TabAService {
@Autowired
private TabADao tabADao;
@Autowired
private TabBDao tabBDao;
@Override
public int add(TabA tabA) {
return tabADao.add(tabA);
}
@Override
public TabA get(int id) {
tabBDao.add(new TabB());
tabADao.add(new TabA());
return tabADao.get(1);
}
@Override
public TabA getTabA(int Id) {
tabBDao.add(new TabB());//添加入库方法,如果是只读事务,这里会回滚
tabADao.add(new TabA());//添加入库方法,如果是只读事务,这里会回滚
return tabADao.get(1);
}
}
<bean id="tabAService" class="com.dtb.test.service.impl.TabAServiceImpl ">
<property name="daoTargetA" ref="daoTargetA"></property>
<property name="daoTargetB" ref="daoTargetB"></property>
</bean>
<bean id="tabAService" class="com.dtb.test.service.impl.TabAServiceImpl "> <property name="daoTargetA" ref="daoTargetA"></property>
<property name="daoTargetB" ref="daoTargetB"></property> </bean>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>