---
title: "spring mybatis jta jotm atomikos 配置 "
author: "onlyor"
date: "2012-04-11T14:07:04Z"
tags: []
categories: ["企业架构"]
slug: "spring-mybatis-jta-jotm-atomikos"
draft: false
lastmod: "2012-04-11T14:09:15Z"
id: 1483289
url: "https://onlyor.iteye.com/blog/1483289"
---

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.0.xsd 

http://www.springframework.org/schema/context 

    http://www.springframework.org/schema/context/spring-context-2.0.xsd 

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"

default-lazy-init="true"&gt;

 

&lt;!-- JOTM 数据源 

&lt;bean id="jotm" class="com.demo.spring.jotm.jta.JotmFactoryBean" /&gt;

 

&lt;bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;

&lt;property name="userTransaction" ref="jotm" /&gt; 

&lt;/bean&gt;

&lt;bean id="myDataSource1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"&gt;

&lt;property name="dataSource"&gt;

&lt;bean class="org.enhydra.jdbc.standard.StandardXADataSource"&gt;

&lt;property name="transactionManager"&gt;

&lt;ref local="jotm" /&gt;

&lt;/property&gt;

&lt;property name="driverName"&gt;

&lt;value&gt;org.postgresql.Driver&lt;/value&gt;

&lt;/property&gt;

&lt;property name="url"&gt;

&lt;value&gt;jdbc:postgresql://127.0.0.1:5432/test&lt;/value&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;/property&gt;

&lt;property name="user"&gt;

&lt;value&gt;postgres&lt;/value&gt;

&lt;/property&gt;

&lt;property name="password"&gt;

&lt;value&gt;root&lt;/value&gt;

&lt;/property&gt;

&lt;property name="maxSize"&gt;

&lt;value&gt;5&lt;/value&gt;

&lt;/property&gt;

&lt;property name="minSize"&gt;

&lt;value&gt;2&lt;/value&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;bean id="myDataSource2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"&gt;

&lt;property name="dataSource"&gt;

&lt;bean class="org.enhydra.jdbc.standard.StandardXADataSource"&gt;

&lt;property name="transactionManager"&gt;

&lt;ref local="jotm" /&gt;

&lt;/property&gt;

&lt;property name="driverName"&gt;

&lt;value&gt;org.postgresql.Driver&lt;/value&gt;

&lt;/property&gt;

&lt;property name="url"&gt;

&lt;value&gt;jdbc:postgresql://127.0.0.1:5432/news&lt;/value&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;/property&gt;

&lt;property name="user"&gt;

&lt;value&gt;postgres&lt;/value&gt;

&lt;/property&gt;

&lt;property name="password"&gt;

&lt;value&gt;root&lt;/value&gt;

&lt;/property&gt;

&lt;property name="maxSize"&gt;

&lt;value&gt;5&lt;/value&gt;

&lt;/property&gt;

&lt;property name="minSize"&gt;

&lt;value&gt;2&lt;/value&gt;

&lt;/property&gt;

&lt;/bean&gt;

--&gt;

 

&lt;!-- ATOMIKOS mysql 数据源 

&lt;bean id="myDataSource1" class="com.atomikos.jdbc.AtomikosDataSourceBean"&gt;

&lt;property name="uniqueResourceName"&gt;

&lt;value&gt;MYSQL/test&lt;/value&gt;

&lt;/property&gt;

&lt;property name="xaDataSourceClassName"&gt;

&lt;value&gt;com.mysql.jdbc.jdbc2.optional.MysqlXADataSource&lt;/value&gt;

&lt;/property&gt;

&lt;property name="xaProperties"&gt;

&lt;props&gt;

&lt;prop key="user"&gt;root&lt;/prop&gt;

&lt;prop key="password"&gt;root&lt;/prop&gt;

&lt;prop key="URL"&gt;jdbc:mysql://127.0.0.1:3306/test&lt;/prop&gt;

&lt;/props&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;bean id="myDataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean"&gt;

&lt;property name="uniqueResourceName"&gt;

&lt;value&gt;MYSQL/news&lt;/value&gt;

&lt;/property&gt;

&lt;property name="xaDataSourceClassName"&gt;

&lt;value&gt;com.mysql.jdbc.jdbc2.optional.MysqlXADataSource&lt;/value&gt;

&lt;/property&gt;

&lt;property name="xaProperties"&gt;

&lt;props&gt;

&lt;prop key="user"&gt;root&lt;/prop&gt;

&lt;prop key="password"&gt;root&lt;/prop&gt;

&lt;prop key="URL"&gt;jdbc:mysql://127.0.0.1:3306/news&lt;/prop&gt;

&lt;/props&gt;

&lt;/property&gt;

&lt;/bean&gt;

--&gt;

 

&lt;!-- ATOMIKOS postgres 数据源 --&gt;

&lt;bean id="myDataSource1" class="com.atomikos.jdbc.AtomikosDataSourceBean"&gt;

&lt;property name="uniqueResourceName"&gt;

&lt;value&gt;postgre/test&lt;/value&gt;

&lt;/property&gt;

&lt;property name="xaDataSourceClassName"&gt;

&lt;value&gt;org.postgresql.xa.PGXADataSource&lt;/value&gt;

&lt;/property&gt;

&lt;property name="xaProperties"&gt;

&lt;props&gt;

&lt;prop key="user"&gt;postgres&lt;/prop&gt;

&lt;prop key="password"&gt;root&lt;/prop&gt;

&lt;prop key="databaseName"&gt;test&lt;/prop&gt;

&lt;prop key="serverName"&gt;127.0.0.1&lt;/prop&gt;

&lt;prop key="portNumber"&gt;5432&lt;/prop&gt;

&lt;/props&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;bean id="myDataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean"&gt;

&lt;property name="uniqueResourceName"&gt;

&lt;value&gt;postgre/news&lt;/value&gt;

&lt;/property&gt;

&lt;property name="xaDataSourceClassName"&gt;

&lt;value&gt;org.postgresql.xa.PGXADataSource&lt;/value&gt;

&lt;/property&gt;

&lt;property name="xaProperties"&gt;

&lt;props&gt;

&lt;prop key="user"&gt;postgres&lt;/prop&gt;

&lt;prop key="password"&gt;root&lt;/prop&gt;

&lt;prop key="databaseName"&gt;news&lt;/prop&gt;

&lt;prop key="serverName"&gt;127.0.0.1&lt;/prop&gt;

&lt;prop key="portNumber"&gt;5432&lt;/prop&gt;

&lt;/props&gt;

&lt;/property&gt;

&lt;/bean&gt;

 

 

&lt;bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean"&gt;

&lt;property name="configLocation" value="classpath:config1.xml" /&gt;

&lt;property name="dataSource" ref="myDataSource1" /&gt;

&lt;/bean&gt;

 

&lt;bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean"&gt;

&lt;property name="configLocation" value="classpath:config2.xml" /&gt;

&lt;property name="dataSource" ref="myDataSource2" /&gt;

&lt;/bean&gt;

 

&lt;bean id="userDAO" class="com.anxin.dao.impl.UserDAOImpl"&gt;

&lt;property name="sqlSessionFactory" ref="sqlSessionFactory1" /&gt;

&lt;/bean&gt;

 

&lt;bean id="studentDAO" class="com.anxin.dao.impl.StudentDAOImpl"&gt;

&lt;property name="sqlSessionFactory" ref="sqlSessionFactory2" /&gt;

&lt;/bean&gt;

 

&lt;bean id="studentService" class="com.anxin.service.impl.StudentServiceImpl"&gt;

&lt;property name="dao" ref="studentDAO" /&gt;

&lt;/bean&gt;

 

&lt;bean id="userService" class="com.anxin.service.impl.UserServiceImpl"&gt;

&lt;property name="dao" ref="userDAO" /&gt;

&lt;/bean&gt;

 

&lt;bean id="complexService" class="com.anxin.service.impl.ComplexServiceImpl"&gt;

&lt;property name="userDao" ref="userDAO" /&gt;

&lt;property name="studentDao" ref="studentDAO" /&gt;

&lt;/bean&gt;

 

&lt;!-- atomikosTransactionManager --&gt;

&lt;bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"

init-method="init" destroy-method="close"&gt;

&lt;property name="forceShutdown"&gt;

&lt;value&gt;true&lt;/value&gt;

&lt;/property&gt;

&lt;/bean&gt;

 

&lt;bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"&gt;

&lt;property name="transactionTimeout"&gt;

&lt;value&gt;300&lt;/value&gt;

&lt;/property&gt;

&lt;/bean&gt;

 

&lt;bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;

&lt;property name="transactionManager"&gt;

&lt;ref bean="atomikosTransactionManager" /&gt;

&lt;/property&gt;

&lt;property name="userTransaction"&gt;

&lt;ref bean="atomikosUserTransaction" /&gt;

&lt;/property&gt;

&lt;/bean&gt;

 

&lt;aop:config&gt;

&lt;aop:pointcut id="pc" expression="execution(* com.anxin.service.\*.\*(..))" /&gt;

&lt;aop:advisor advice-ref="txAdvice" pointcut-ref="pc" /&gt;

&lt;/aop:config&gt;

 

&lt;tx:advice id="txAdvice" transaction-manager="springTransactionManager"&gt;

&lt;tx:attributes&gt;

&lt;tx:method name="get\*" propagation="REQUIRED" read-only="true" /&gt;

&lt;tx:method name="find\*" propagation="REQUIRED" read-only="true" /&gt;

&lt;tx:method name="has\*" propagation="REQUIRED" read-only="true" /&gt;

&lt;tx:method name="locate\*" propagation="REQUIRED" read-only="true" /&gt;

&lt;tx:method name="\*" propagation="REQUIRED" rollback-for="Exception" /&gt;

&lt;/tx:attributes&gt;

&lt;/tx:advice&gt;

 

&lt;!-- 手动代理

&lt;bean id="transactionProxyFactoryBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 

abstract="true"&gt; 

&lt;property name="transactionManager"&gt; 

&lt;ref bean="springTransactionManager" /&gt; 

&lt;/property&gt; 

&lt;property name="transactionAttributes"&gt; 

&lt;props&gt; 

&lt;prop key="save\*"&gt;PROPAGATION\_REQUIRED,-Exception&lt;/prop&gt; 

&lt;/props&gt;

&lt;/property&gt; 

&lt;/bean&gt; 

&lt;bean id="complexServiceProxy" parent="transactionProxyFactoryBean"&gt; 

&lt;property name="target"&gt; 

&lt;ref bean="complexService" /&gt; 

&lt;/property&gt; 

&lt;/bean&gt;

--&gt;

 

 

&lt;/beans&gt;