使用spring+mybatis+atomikos+tomcat构建分布式事务

10 篇文章 0 订阅
1 篇文章 0 订阅

完整工程下载路径 http://download.csdn.net/detail/npf_java/8786825

 

拿一个MySQL + oracle 数据源配置为例吧

 

database.properties

 

[html] view plain copy
  1. #mysql DB  
  2. mysql.qa.db.url=jdbc:mysql://localhost:3306/ssm_qa?useUnicode=true&characterEncoding=UTF-8  
  3. mysql.qa.db.user=root  
  4. mysql.qa.db.password=ROOT  
  5.   
  6. mysql.dev.db.url=jdbc:mysql://localhost:3306/ssm_dev?useUnicode=true&characterEncoding=UTF-8  
  7. mysql.dev.db.user=root  
  8. mysql.dev.db.password=ROOT  
  9.   
  10. #oracle DB  
  11. oracle.qa.db.url=jdbc\:oracle\:thin\:@1.1.1.32\:1521\:nyj  
  12. oracle.qa.db.driverClassName=oracle.jdbc.OracleDriver  
  13. oracle.qa.db.testQuery=select 1 from dual  
  14. oracle.qa.db.user=oms  
  15. oracle.qa.db.password=oms  
  16.   
  17. oracle.dev.db.url=jdbc\:oracle\:thin\:@1.1.1.32\:1521\:nyj  
  18. oracle.dev.db.driverClassName=oracle.jdbc.OracleDriver  
  19. oracle.dev.db.testQuery=select 1 from dual  
  20. oracle.dev.db.user=com  
  21. oracle.dev.db.password=lsd  
  22.   
  23.   
  24. #sqlserver DB  
  25. sqlserver.qa.db.driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver  
  26. sqlserver.qa.db.url=jdbc:sqlserver://1.1.1.11:1433;DatabaseName=dl_xbny  
  27. sqlserver.qa.db.user=dl_xbny  
  28. sqlserver.qa.db.password=dl123456  
  29.   
  30. sqlserver.dev.db.driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver  
  31. sqlserver.dev.db.url=jdbc:sqlserver://1.1.1.11:1433;DatabaseName=com_xbny  
  32. sqlserver.dev.db.user=com_xbny  
  33. sqlserver.dev.db.password=com123456  


mysql-oracle-beans.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"  
  4.        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
  5.        xmlns:rabbit="http://www.springframework.org/schema/rabbit"  
  6.        xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd  
  8.        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">  
  9.     <context:component-scan base-package="com.xy">  
  10.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  11.     </context:component-scan>  
  12.     <context:property-placeholder location="classpath:context/database.properties"/>  
  13.     <tx:annotation-driven/>  
  14.   
  15.      <bean id="abstractXADataSource" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean" init-method="init"  
  16.           destroy-method="close" abstract="true">  
  17.         <property name="xaDataSourceClassName" value="com.mysql.jdbc.Driver"/>  
  18.         <property name="poolSize" value="10" />  
  19.         <property name="minPoolSize" value="10"/>  
  20.         <property name="maxPoolSize" value="30"/>  
  21.         <property name="borrowConnectionTimeout" value="60"/>  
  22.         <property name="reapTimeout" value="20"/>  
  23.         <!-- 最大空闲时间 -->  
  24.         <property name="maxIdleTime" value="60"/>  
  25.         <property name="maintenanceInterval" value="60"/>  
  26.         <property name="loginTimeout" value="60"/>  
  27.         <property name="testQuery">  
  28.             <value>select 1</value>  
  29.         </property>  
  30.     </bean>  
  31.   
  32.    <bean id="qadataSource" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">  
  33.         <!-- value只要两个数据源不同就行,随便取名 -->  
  34.         <property name="uniqueResourceName" value="dlDB" />  
  35.         <property name="driverClassName">  
  36.             <value>com.mysql.jdbc.Driver</value>  
  37.         </property>  
  38.         <property name="url">  
  39.             <value>${mysql.qa.db.url}</value>  
  40.         </property>  
  41.         <property name="user">  
  42.             <value>${mysql.qa.db.user}</value>  
  43.         </property>  
  44.         <property name="password">  
  45.             <value>${mysql.qa.db.password}</value>  
  46.         </property>  
  47.     </bean>  
  48.   
  49.     <bean id="devdataSource" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">  
  50.         <!-- value只要两个数据源不同就行,随便取名 -->  
  51.         <property name="uniqueResourceName" value="nyxsDB" />  
  52.        <property name="driverClassName">  
  53.             <value>${oracle.dev.db.driverClassName}</value>  
  54.         </property>  
  55.         <property name="url">  
  56.             <value>${oracle.dev.db.url}</value>  
  57.         </property>  
  58.         <property name="user">  
  59.             <value>${oracle.dev.db.user}</value>  
  60.         </property>  
  61.         <property name="password">  
  62.             <value>${oracle.dev.db.password}</value>  
  63.         </property>  
  64.     </bean>  
  65.   
  66.     <bean id="qasqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  67.         <property name="dataSource" ref="qadataSource" />  
  68.         <property name="mapperLocations" value="classpath*:com/xy/dao/*.xml" />  
  69.     </bean>  
  70.   
  71.     <bean id="devsqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  72.         <property name="dataSource" ref="devdataSource" />  
  73.         <property name="mapperLocations" value="classpath*:com/xy/daodev/*.xml" />  
  74.     </bean>  
  75.   
  76.     <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"  
  77.           init-method="init" destroy-method="close">  
  78.         <property name="forceShutdown">  
  79.             <value>true</value>  
  80.         </property>  
  81.     </bean>  
  82.     <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">  
  83.         <property name="transactionTimeout" value="300" />  
  84.     </bean>  
  85.   
  86.     <bean id="transactionManager"  
  87.           class="org.springframework.transaction.jta.JtaTransactionManager">  
  88.         <property name="transactionManager">  
  89.             <ref bean="atomikosTransactionManager"/>  
  90.         </property>  
  91.         <property name="userTransaction">  
  92.             <ref bean="atomikosUserTransaction"/>  
  93.         </property>  
  94.         <!-- 必须设置,否则程序出现异常 JtaTransactionManager does not support custom isolation levels by default -->  
  95.         <property name="allowCustomIsolationLevels" value="true"/>  
  96.   
  97.     </bean>  
  98.   
  99.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  100.         <property name="basePackage" value="com.xy.dao"/>  
  101.         <property name="sqlSessionFactoryBeanName" value="qasqlSessionFactory" />  
  102.     </bean>  
  103.   
  104.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  105.         <property name="basePackage" value="com.xy.daodev"/>  
  106.         <property name="sqlSessionFactoryBeanName" value="devsqlSessionFactory" />  
  107.     </bean>  
  108. </beans>  


web.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.          version="2.5">  
  5.     <listener>  
  6.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  7.     </listener>  
  8.     <listener>  
  9.         <listener-class>  
  10.             org.springframework.web.context.request.RequestContextListener  
  11.         </listener-class>  
  12.     </listener>  
  13.       
  14.     <!--  
  15.     <context-param>  
  16.         <param-name>contextConfigLocation</param-name>  
  17.         <param-value>classpath:context/mysql-beans.xml</param-value>  
  18.     </context-param>  
  19.       
  20.      <context-param>  
  21.         <param-name>contextConfigLocation</param-name>  
  22.         <param-value>classpath:context/oracle-beans.xml</param-value>  
  23.     </context-param>  
  24.       
  25.      <context-param>  
  26.         <param-name>contextConfigLocation</param-name>  
  27.         <param-value>classpath:context/sqlserver-beans.xml</param-value>  
  28.     </context-param>  
  29.       
  30.       <context-param>  
  31.         <param-name>contextConfigLocation</param-name>  
  32.         <param-value>classpath:context/sqlserver-oracle-beans.xml</param-value>  
  33.      </context-param>  
  34.      -->  
  35.        
  36.     <span style="color:#ff6666;">  <context-param>  
  37.         <param-name>contextConfigLocation</param-name>  
  38.         <param-value>classpath:context/mysql-oracle-beans.xml</param-value>  
  39.      </context-param></span>  
  40.       
  41.     <servlet>  
  42.         <servlet-name>test</servlet-name>  
  43.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  44.         <init-param>  
  45.             <param-name>contextConfigLocation</param-name>  
  46.             <param-value>classpath:context/test-web.xml</param-value>  
  47.         </init-param>  
  48.         <load-on-startup>100</load-on-startup>  
  49.     </servlet>  
  50.     <servlet-mapping>  
  51.         <servlet-name>test</servlet-name>  
  52.         <url-pattern>/</url-pattern>  
  53.     </servlet-mapping>  
  54.     <welcome-file-list>  
  55.         <welcome-file>index</welcome-file>  
  56.     </welcome-file-list>  
  57.   
  58. </web-app>  

 

 

基本上看这个三个配置文件就可以了,不明白的话,可以下载完整工程,dbsql有完整的建表语句

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值