先贴出配置文件:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd <bean id="dataSource" class="assp.evoucher.common.datasource.AsspDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="XADataSource" /> <property name="poolSize" value="10" /> <property name="maxPoolSize" value="100" /> <property name="reapTimeout" value="0"/> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close" > <property name="forceShutdown" value="false"/> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp" > <property name="transactionTimeout" value="300"/> </bean> <bean id="com.atomikos.spring.jms.tm" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="atomikosTransactionManager"/> <property name="userTransaction" ref="atomikosUserTransaction"/> </bean> <bean id="baseDAO" class="grp.pt.util.BaseDAO"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="com.atomikos.spring.jms.tm" /> <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <prop key="newTransExecute">PROPAGATION_REQUIRES_NEW,-Exception</prop> <prop key="*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Service</value> </list> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean> <!-- 小事务公共类,以传播属性PROPAGATION_REQUIRES_NEW执行 --> <bean id="smallTranService" class="grp.pt.util.transation.SmallTransService"></bean> <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" lazy-init="true"/> <bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true"> <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/> </bean> </beans>
1.数据源
<bean id="dataSource" class="assp.evoucher.common.datasource.AsspDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="XADataSource" /> <property name="poolSize" value="10" /> <property name="maxPoolSize" value="100" /> <property name="reapTimeout" value="0"/> </bean>
对于init-method="init"配置,可以发现affterPropertiesSet放在执行在init方法执行之前。
2.将数据源绑定到jdbcTemplate,因为公司用的持久层是与JDBC相连接的
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close" > <property name="forceShutdown" value="false"/> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp" > <property name="transactionTimeout" value="300"/> </bean> <bean id="com.atomikos.spring.jms.tm" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="atomikosTransactionManager"/> <property name="userTransaction" ref="atomikosUserTransaction"/> </bean>这两个类:com.atomikos.icatch.jta.UserTransactionManager、com.atomikos.icatch.jta.UserTransactionImp是atomikos平台下的两个类,大家应该先了解一下atomikos。这两个事务管理器配置好之后,需要通过Spring来支持:org.springframework.transaction.jta.JtaTransactionManager,我们用到的框架Spring,所以需要将事务管理器通过Spring事务管理器封装一下。
4.将绑定数据源后的jdbcTemplate作为DAO层的支持。
<bean id="baseDAO" class="grp.pt.util.BaseDAO"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="com.atomikos.spring.jms.tm" /> <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <prop key="newTransExecute">PROPAGATION_REQUIRES_NEW,-Exception</prop> <prop key="*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean>
6.配置事务创建的代理
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Service</value> </list> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean>
里面配置拦截器和我们需要制定事务控制的类*Service
补充:而外创建一个小事务
<!-- 小事务公共类,以传播属性PROPAGATION_REQUIRES_NEW执行 --> <bean id="smallTranService" class="grp.pt.util.transation.SmallTransService"></bean> <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" lazy-init="true"/> <bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true"> <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/> </bean>