Spring3+Hibernate3+Atomikos集成构建JTA的分布式事务(二)

参考文章1:https://www.cnblogs.com/zhongshiqiang/p/7086137.html

参考文章2:http://blog.csdn.net/zmx729618/article/details/51074875

参考文章3:http://blog.csdn.net/zmx729618/article/details/54344296

配置Spring集成Atomikos分布式事务

spring 2.5以后,spring 删除了JotmFactoryBean ,spring不再提供对jotm提供支持

关于分布式事务的一个系列讲解:https://my.oschina.net/pingpangkuangmo/blog/413518

引入Atomikos相关jar包

atomikos-util-4.0.4.EVAL.jar
cglib-nodep-2.2.jar
transactions-4.0.4.EVAL.jar
transactions-api-4.0.4.EVAL.jar
transactions-jdbc-4.0.4.EVAL.jar
transactions-jta-4.0.4.EVAL.jar

数据库连接属性文件

jdbc.properties

########################################################################
#jdbc settings
#jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.driver=com.mysql.jdbc.Driver
########################################################################
#app
#app.jdbc.url=jdbc:oracle:thin:@117.132.8.93:1521:orcl
#sys
sys.jdbc.url=jdbc\:mysql\://60.205.151.25:3306/ray_sys?useOldAliasMetadataBehavior=true

#app
app.jdbc.url=jdbc\:mysql\://60.205.151.25:3306/ray_app?useOldAliasMetadataBehavior=true

#mysql db
#sys.jdbc.username=root
#sys.jdbc.password=hx123
sys.jdbc.username=test
sys.jdbc.password=15705427359

sys.max-size=10
sys.min-size=2
sys.life-time =10
sys.max-life-time=50
sys.sleep-time = 30
sys.dead-lock-max-wait = 50


#app.jdbc.username=root
#app.jdbc.password=hx123
app.jdbc.username=test
app.jdbc.password=15705427359

app.max-size=10
app.min-size=2
app.life-time =10
app.max-life-time=50
app.sleep-time = 30
app.dead-lock-max-wait = 50

配置数据源

datasource.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "../dtd/spring-beans.dtd">
<beans> 
    <!-- 配置Sys数据源,数据库参数在集成hibernate的时候导入配置属性文件,见下文。 -->
    <bean id="dataSource_sys" class="com.atomikos.jdbc.AtomikosDataSourceBean"
        init-method="init" destroy-method="close">
        <property name="uniqueResourceName" value="sys_resource" />
        <!-- 这里使用阿里巴巴分布式事务的多数据源 -->
        <property name="xaDataSourceClassName" value="com.alibaba.druid.pool.xa.DruidXADataSource" >
        </property>
        <property name="xaProperties">
            <props>
                <prop key="url">${sys.jdbc.url}</prop>
                <prop key="username">${sys.jdbc.username}</prop>
                <prop key="password">${sys.jdbc.password}</prop>
            </props>
        </property>
        <property name="poolSize" value="3"/>  
        <property name="reapTimeout"><value>20000</value></property> 
        <property name="maxLifetime" value="2000"/>   
        <property name="borrowConnectionTimeout" value="30" />  
        <property name="testQuery" value="select 1" />  
        <property name="maintenanceInterval" value="60" />  
    </bean> 

    <!-- 使用数据源。先集成hibernate,在配置事务。见上文的配置 --> 
    <bean id="mainSessionFactory" 
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource_sys" /></property>

        <!-- 这里的hibernateProperties引用第一篇中的hibernate属性配置。 -->
        <property name="hibernateProperties" ref="hibernateProperties" />
        <!-- 自动扫描注解方式配置的hibernate类文件 -->
        <property name="packagesToScan">
            <list>
                <value>com.juttec.**.entity</value>
            </list>
        </property>
    </bean>

    <!-- 配置app数据源,数据库参数在集成hibernate的时候导入配置属性文件,见下文。 -->
    <bean id="dataSource_app" class="com.atomikos.jdbc.AtomikosDataSourceBean"
        init-method="init" destroy-method="close">
        <property name="uniqueResourceName" value="app_resource" />
        <!-- 这里使用阿里巴巴分布式事务的多数据源 -->
        <property name="xaDataSourceClassName" value="com.alibaba.druid.pool.xa.DruidXADataSource" >
        </property>
        <property name="xaProperties">
            <props>
                <prop key="url">${app.jdbc.url}</prop>
                <prop key="username">${app.jdbc.username}</prop>
                <prop key="password">${app.jdbc.password}</prop>
            </props>
        </property>
        <property name="poolSize" value="3"/>  
        <property name="reapTimeout"><value>20000</value></property> 
        <property name="maxLifetime" value="2000"/>
        <property name="borrowConnectionTimeout" value="30" />  
        <property name="testQuery" value="select 1" />  
        <property name="maintenanceInterval" value="60" />    

    </bean> 

    <!-- 使用数据源。先集成hibernate,在配置事务。见上文的配置 -->
    <bean id="appSessionFactory" 
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource_app" /></property>

        <!-- 这里的hibernateProperties引用第一篇中的hibernate属性配置。 -->
        <property name="hibernateProperties" ref="hibernateProperties" />
        <!-- 自动扫描注解方式配置的hibernate类文件 -->
        <property name="packagesToScan">
            <list>
                <value>com.juttec.**.entity</value>
            </list>
        </property>
    </bean>
</beans>

配置jta事务管理器

applicationContext.xml

<!-- 配置分布式事务管理(多数据源) --> 
<!--    <bean id="jotm" class="com.juttec.core.spring.JotmFactoryBean">
    <property name="defaultTimeout" value="600" />  
</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="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <!-- <property name="userTransaction" ref="jotm" /> -->
    <property name="transactionManager">
        <ref bean="atomikosTransactionManager" />
    </property>
    <property name="userTransaction">
        <ref bean="atomikosUserTransaction" />
    </property>
</bean>

<!-- 使用spring的声明式事务配置 -->
<tx:annotation-driven proxy-target-class="true"/> 

==注意==:本来要配置transaction-manager属性,如:。这里没有配置是因为它的默认值是transactionManager。

添加Atomikos的配置文件

在src下添加transactions.properties

# SAMPLE PROPERTIES FILE FOR THE TRANSACTION SERVICE
# THIS FILE ILLUSTRATES THE DIFFERENT SETTINGS FOR THE TRANSACTION MANAGER
# UNCOMMENT THE ASSIGNMENTS TO OVERRIDE DEFAULT VALUES;

# Required: factory implementation class of the transaction core.
# NOTE: there is no default for this, so it MUST be specified! 
# 
com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory


# Set base name of file where messages are output 
# (also known as the 'console file').
#
 com.atomikos.icatch.console_file_name = tm.out

# Size limit (in bytes) for the console file;
# negative means unlimited.
#
# com.atomikos.icatch.console_file_limit=-1

# For size-limited console files, this option
# specifies a number of rotating files to 
# maintain.
#
# com.atomikos.icatch.console_file_count=1

# Set the number of log writes between checkpoints
#
# com.atomikos.icatch.checkpoint_interval=500

# Set output directory where console file and other files are to be put
# make sure this directory exists!
#
# com.atomikos.icatch.output_dir = ./

# Set directory of log files; make sure this directory exists!
#
# com.atomikos.icatch.log_base_dir = ./

# Set base name of log file
# this name will be  used as the first part of 
# the system-generated log file name
#
# com.atomikos.icatch.log_base_name = tmlog

# Set the max number of active local transactions 
# or -1 for unlimited.
#
# com.atomikos.icatch.max_actives = 50

# Set the default timeout (in milliseconds) for local transactions
#
 com.atomikos.icatch.default_jta_timeout = 10000

# Set the max timeout (in milliseconds) for local transactions
#
 com.atomikos.icatch.max_timeout = 300000

# The globally unique name of this transaction manager process
# override this value with a globally unique name
#
 com.atomikos.icatch.tm_unique_name = tm

# Do we want to use parallel subtransactions? JTA's default
# is NO for J2EE compatibility
#
# com.atomikos.icatch.serial_jta_transactions=true

# If you want to do explicit resource registration then
# you need to set this value to false.
#
# com.atomikos.icatch.automatic_resource_registration=true  

# Set this to WARN, INFO or DEBUG to control the granularity
# of output to the console file.
#
# com.atomikos.icatch.console_log_level=WARN

# Do you want transaction logging to be enabled or not?
# If set to false, then no logging overhead will be done
# at the risk of losing data after restart or crash.
#
# com.atomikos.icatch.enable_logging=true

# Should two-phase commit be done in (multi-)threaded mode or not?
# Set this to false if you want commits to be ordered according
# to the order in which resources are added to the transaction.
#
# NOTE: threads are reused on JDK 1.5 or higher. 
# For JDK 1.4, thread reuse is enabled as soon as the 
# concurrent backport is in the classpath - see 
# http://mirrors.ibiblio.org/pub/mirrors/maven2/backport-util-concurrent/backport-util-concurrent/
#
# com.atomikos.icatch.threaded_2pc=false

# Should shutdown of the VM trigger shutdown of the transaction core too?
#
# com.atomikos.icatch.force_shutdown_on_vm_exit=false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值