现在需要配置两个datasource,两个库交互的时候,想用jta解决全局事务的问题,
在网上找了相关的资料,大多数都是jdbc的,
我想用proxool.也有不过都是jotm的,有人知道纯jta的吗?
为此求助!!希望可以提供相关的配置资料!先谢啦!!

解决方案 »

  1.   

    这个使用jta+jotm比较合适,曾经测试用过的配置。<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" /> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="userTransaction">
    <ref local="jotm" />
    </property>
    </bean>
    <bean id="innerMysqlDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
    <property name="transactionManager">
    <ref local="jotm" />
    </property>
    <property name="driverName">
    <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://xxxxxxxxxxxxxxx</value>
    </property>
    </bean> <bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
    <property name="dataSource">
    <ref local="innerMysqlDataSource" />
    </property>
    <property name="maxSize">
    <value>5</value>
    </property>
    <property name="minSize">
    <value>2</value>
    </property>
    <property name="user">
    <value>root</value>
    </property>
    <property name="password">
    <value>root</value>
    </property>
    </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
    <list>
    <value>org/sh/model/People.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    </props>
    </property>
    <property name="jtaTransactionManager">
    <ref bean="jotm" />
    </property> </bean>
    <bean id="innerMysqlDataSource2" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
    <property name="transactionManager">
    <ref local="jotm" />
    </property>
    <property name="driverName">
    <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://xxxxxxxxxxxxxxx</value>
    </property>
    </bean> <bean id="dataSource2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
    <property name="dataSource">
    <ref local="innerMysqlDataSource2" />
    </property>
    <property name="maxSize">
    <value>5</value>
    </property>
    <property name="minSize">
    <value>2</value>
    </property>
    <property name="user">
    <value>root</value>
    </property>
    <property name="password">
    <value>root</value>
    </property>
    </bean>
    <bean id="mySessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource2" />
    <property name="mappingResources">
    <list>
    <value>org/sh/model/People.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    </props>
    </property>
    <property name="jtaTransactionManager">
    <ref bean="jotm" />
    </property> </bean>
    <!-- 事务拦截 开始-->
    <bean id="beanNameProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames">
    <list>
    <value>*DS</value>
    </list>
    </property>
    <property name="interceptorNames">
    <list>
    <value>transactionInterceptor</value>
    </list>
    </property>
    </bean>
    <bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributeSource"
    ref="txAttributeSource" />
    </bean>
    <bean id="txAttributeSource"
    class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
    <property name="properties">
    <props>
    <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
    </props>
    </property>
    </bean>
    <!-- 事务拦截 结束-->

    <bean id="PeopleDS" class="org.sh.service.PeopleDS">
    <property name="dao">
    <bean class="org.sh.dao.PeopleDAO">
    <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>
    </property>
    </bean>
    </beans>
      

  2.   

    http://love2java.javaeye.com/blog/524304看看这个吧,网上帖子还是比较多的。
      

  3.   

    我想问一下:org.enhydra这个包需要我去下载吗?这个怎么能使用proxool连接池呢?还用proxool吗?
      

  4.   

    使用JTA管理多数据库事务,使用的数据源必须是实现了XADataSource的数据源,比如xapool。http://topic.csdn.net/u/20091125/09/9f85c3a2-5fea-490c-8170-476579816c95.html
      

  5.   

    web.xml    <!--指明父亲applicationContext,start  -->
      <context-param>
        <param-name>locatorFactorySelector</param-name>
        <param-value>classpath*:beanRefContext.xml</param-value>
      </context-param>
      <context-param>
        <param-name>parentContextKey</param-name>
        <param-value>beangroup</param-value>
      </context-param>
      <!--指明父亲applicationContext,end  -->
      <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/applicationContext.xml</param-value>
      </context-param>
    applicationContext.xml<!-- XAPool配置,内部包含了一个XA数据源 -->
    <bean id="applyDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
    <property name="dataSource">
    <!-- 内部XA数据源 -->
    <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
    <property name="transactionManager">
    <ref bean="jotm"/>
    </property>
    <property name="driverName">
    <value>oracle.jdbc.driver.OracleDriver</value>
    </property>
    <property name="url">
    <value>jdbc:oracle:thin:@00.0.00.0:1521:db</value>
    </property>
    </bean>
    </property>
    <property name="user">
    <value>user</value>
    </property>
    <property name="password">
    <value>password</value>
    </property>
    </bean> <bean id="applySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="mappingLocations">
    <list>
    <value>classpath:/bo/*.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.use_outer_join">true</prop>
            <prop key="hibernate.max_fetch_depth">1</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
    </props>
    </property>
    <property name="lobHandler">
    <ref bean="lobHandler"/>
    </property>
    <property name="dataSource">
    <ref bean="applyDataSource"/>
    </property>
    </bean>

    <!-- ========================= Start of SERVICE DEFINITIONS ========================= -->  

    <bean id="applyDAO" class=".dao.impl.ApplyDAOImpl">
    <property name="sessionFactory">
    <ref bean="applySessionFactory"/>
    </property>
    </bean>

    <bean id="applyService" class="service.impl.ApplyServiceImpl">
    <!-- 应用的dao -->
    <property name="applyDAO">
    <ref bean="applyDAO" />
    </property>
    </bean>

    <bean id="processUCCTarget" class="impl.ProcessUCCImpl">
    <!-- 引擎的service -->
    <property name="processService">
    <ref bean="processService" />
    </property>
    <!-- 应用的service -->
    <property name="applyService">
    <ref bean="applyService" />
    </property>
    </bean>

    <bean id="processUCC" parent="baseTransactionProxy">
    <property name="proxyInterfaces">
    <list>
    <value>ucc.IProcessUCC</value>
    </list>
    </property>
    <property name="target">
    <ref bean="processUCCTarget"/>
    </property>
    </bean>
    beanRefContext.xml
    <beans>
      <bean id="beangroup" class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
          <list>
            <value>proContext.xml</value>        
          </list>
        </constructor-arg>
      </bean>
    </beans>proContext.xml<beans>
    <!--
    ========================= Start of PERSISTENCE DEFINITIONS
    =========================
    --> <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource">
    <ref bean="processDataSource" />
    </property>
    </bean> <bean id="namingTransactionAttribute"
    class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
    <property name="properties">
    <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="create*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="del*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean> <bean id="baseTransactionProxy"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    abstract="true">
    <property name="transactionAttributeSource">
    <ref bean="namingTransactionAttribute" />
    </property>
    </bean> <bean id="processDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverName">
    <value>oracle.jdbc.driver.OracleDriver</value>
    </property>
    <property name="url">
    <value>jdbc:oracle:thin:@00.0.000.1:1521:db</value>
    </property> <property name="user">
    <value>user</value>
    </property>
    <property name="password">
    <value>password</value>
    </property>
    </bean> <bean id="nativeJdbcExtractor"
    class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" />
    <bean id="lobHandler" lazy-init="true"
    class="org.springframework.jdbc.support.lob.OracleLobHandler">
    <property name="nativeJdbcExtractor">
    <ref bean="nativeJdbcExtractor" />
    </property>
    </bean> <!-- Hibernate SessionFactory Definition --> <bean id="processSessionFactory1"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="mappingLocations">
    <list>
    <value>cn/ccb/common/model/bo/BMFlowList.hbm.xml</value>
    <value>cn/ccb/common/model/bo/BMTaskInfo.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.use_outer_join">true</prop>
    <prop key="hibernate.max_fetch_depth">1</prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
    <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
    </props>
    </property>
    <property name="lobHandler">
    <ref bean="lobHandler" />
    </property>
    <property name="dataSource">
    <ref bean="processDataSource" />
    </property>
    </bean> <!--
    ========================= Start of SERVICE DEFINITIONS
    =========================
    --> <bean id="processDAO" class="dao.impl.ProcessDAOImpl">
    <property name="sessionFactory">
    <ref bean="processSessionFactory1" />
    </property>
    </bean> <bean id="processService" class="service.impl.ProcessServiceImpl">
    <!-- 引擎的dao -->
    <property name="processDAO">
    <ref bean="processDAO" />
    </property>
    </bean>
    </beans>