spring JTA多数据源事务管理详细教程

<context:annotation-config />
<!-- 使用注解的包路径 -->
<context:component-scan
    base-package="com.rongli.service,com.rongli.dao,com.rongli.controller" />
<!-- 支持 @Transactional 标记 -->
<tx:annotation-driven transaction-manager="springJTATransactionManager"
    proxy-target-class="true" />
<!-- 导入cxf配置文件 -->
<import resource="ws.cxf.xml" />

<!-- 加载properties配置文件 -->
<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jta.jdbc.properties</value>
        </list>
    </property>
</bean>

<!--公有数据库连接池 -->
<bean id="abstractXADataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
    init-method="init" destroy-method="close" abstract="true">
    <property name="xaDataSourceClassName"
        value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
    <property name="poolSize" value="10" />
    <property name="minPoolSize" value="10" />
    <property name="maxPoolSize" value="30" />
    <!--获取连接失败重新获等待最大时间,在这个时间内如果有可用连接,将返回 -->
    <property name="borrowConnectionTimeout" value="60" />
    <!--最大获取数据时间,如果不设置这个值,Atomikos使用默认的5分钟, 那么在处理大批量数据读取的时候,一旦超过5分钟,就会抛出类似 Resultset 
        is close 的错误. -->
    <property name="reapTimeout" value="20000" />
    <!-- 最大空闲时间 -->
    <property name="maxIdleTime" value="60" />
    <!--连接回收时间 -->
    <property name="maintenanceInterval" value="60" />
    <!--java数据库连接池,最大可等待获取datasouce的时间 -->
    <property name="loginTimeout" value="60" />
    <property name="testQuery">
        <value>select 1</value>
    </property>
</bean>

<!-- 客户数据库 -->
<bean id="rlc_cus" parent="abstractXADataSource">
    <property name="uniqueResourceName" value="mysql/rlc_cus" />
    <property name="xaProperties">
        <props>
            <prop key="URL">${jdbc.rlc_cus.properties}</prop>
            <prop key="user">${jdbc.rlc.user}</prop>
            <prop key="password">${jdbc.rlc.password}</prop>
            <prop key="pinGlobalTxToPhysicalConnection">true</prop>
        </props>
    </property>
</bean>

<!-- 系统数据库 -->
<bean id="rlc_sys" parent="abstractXADataSource">
    <property name="uniqueResourceName" value="mysql/rlc_sys" />
    <property name="xaProperties">
        <props>
            <prop key="URL">${jdbc.rlc_sys.properties}</prop>
            <prop key="user">${jdbc.rlc.user}</prop>
            <prop key="password">${jdbc.rlc.password}</prop>
            <prop key="pinGlobalTxToPhysicalConnection">true</prop>
        </props>
    </property>
</bean>

<!-- 公有SessionFactory配置 -->
<bean id="baseSessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
    abstract="true">
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.format_sql}</prop>
            <prop key="hibernate.format_sql">${hibernate.show_sql}</prop>
            <prop key="javax.persistence.validation.mode">none</prop>
        </props>
    </property>
    <!-- 自动扫描注解方式配置的hibernate类文件 -->
    <property name="packagesToScan" value="com.rongli.bean"></property>
</bean>

<!-- sessionFactory配置 -->
<!-- customer sessionFactory -->
<bean id="cusSessionFactory" parent="baseSessionFactory">
    <property name="dataSource" ref="rlc_cus" />
</bean>
<!-- system sessionFactory -->
<bean id="sysSessionFactory" parent="baseSessionFactory">
    <property name="dataSource" ref="rlc_sys" />
</bean>

<!-- atomikos事务管理器 -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
    init-method="init" destroy-method="close">
    <!-- 调用终止时,强制关闭 -->
    <property name="forceShutdown">
        <value>true</value>
    </property>
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
    <property name="transactionTimeout">
        <value>2000</value>
    </property>
</bean>

<!-- spring 事务管理器 -->
<bean id="springJTATransactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager">
        <ref bean="atomikosTransactionManager" />
    </property>
    <property name="userTransaction">
        <ref bean="atomikosUserTransaction" />
    </property>
</bean>

<!-- 用于测试,发布到服务器上时删除 -->
<bean id="systemServiceImpl" class="com.rongli.service.impl.SystemServiceImpl">
</bean>

 

posted @ 2016-06-27 17:36  业精于勤而荒于嬉  阅读(1056)  评论(0编辑  收藏  举报