`
sharron5
  • 浏览: 18557 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

spring2.5+atomikos分布式事务配置

阅读更多
首先为src目录下的app_jta.xml(数据源及事务配置):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<aop:config proxy-target-class="true">
		<aop:pointcut id="serviceOperation" expression="execution(* com.service.impl.*.*(..))" />
		<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" />
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable"/>
			<tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable"/>
			<tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable"/>
			<tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable"/>
			<tx:method name="create*" propagation="REQUIRED" rollback-for="Throwable"/>
			<tx:method name="get*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="*" />
		</tx:attributes>
	</tx:advice>

	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath*:jdbc.properties</value>
			</list>
		</property>
	</bean>

	<bean id="dataSourceA" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="${mysqlA.uniqueResourceName}" />
		<property name="xaDataSourceClassName" value="${mysql.xaDataSourceClassName}" />
		<property name="xaProperties">
			<props>
				<prop key="user">${mysql.user}</prop>
				<prop key="password">${mysql.password}</prop>
				<prop key="URL">${mysqlA.url}</prop>
			</props>
		</property>
		<property name="poolSize" value="${mysql.poolSize}" />
	</bean>

	<bean id="dataSourceB" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="${mysqlB.uniqueResourceName}" />
		<property name="xaDataSourceClassName" value="${mysql.xaDataSourceClassName}" />
		<property name="xaProperties">
			<props>
				<prop key="user">${mysql.user}</prop>
				<prop key="password">${mysql.password}</prop>
				<prop key="URL">${mysqlB.url}</prop>
			</props>
		</property>
		<property name="poolSize" value="${mysql.poolSize}" />
	</bean>

	<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
		init-method="init" destroy-method="close">
		<property name="forceShutdown" value="${transactionManager.forceShutdown}" />
	</bean>

	<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
		<property name="transactionTimeout" value="${transactionManager.transactionTimeout}" />
	</bean>

	<bean id="transactionManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="transactionManager">
			<ref bean="atomikosTransactionManager" />
		</property>
		<property name="userTransaction">
			<ref bean="atomikosUserTransaction" />
		</property>
	</bean>
	
	<!--
		<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />
	-->

	<!-- dao -->
	<bean id="userDaoA" class="com.dao.jdbc.UserJdbcDaoA">
		<property name="dataSource" ref="dataSourceA" />
	</bean>

	<bean id="userDaoB" class="com.dao.jdbc.UserJdbcDaoB">
		<property name="dataSource" ref="dataSourceB" />
	</bean>

	<bean id="userService" class="com.service.impl.UserServiceImpl">
		<property name="userDaoA" ref="userDaoA" />
		<property name="userDaoB" ref="userDaoB" />
	</bean>
	
	<bean id="userCtl" class="com.controller.UserController">
		<property name="userService" ref="userService" />
	</bean>

</beans>


接下来为WEB-INF下的spring-servlet.xml(SPRING MVC配置):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


	<!-- 方法名解析器 -->
	<bean id="InternalPathMethodNameResolver"
		class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" />


	<!--  对模型视图名称的解析,在请求时模型视图名称添加前后缀  -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

	<bean id="urlMapping"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<!-- partner -->
				<prop key="/_new.do">userCtl</prop>
				<prop key="/create.do">userCtl</prop>
			</props>
		</property>
	</bean>
</beans>

以上为主要配置,另附完整实例,见附件。 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics