spring结合 atomikos 使用

 
atomikos 所需jar :

1.transactions-jdbc.jar

2.transactions.jar

3.transactions-jta.jar

4.transactions-api.jar 

5.atomikos-util.jar 

jta.properties 配置文件 放在src目录即可

com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.console_file_name = tm.out 
com.atomikos.icatch.log_base_name = tmlog 
com.atomikos.icatch.tm_unique_name = com.atomikos.spring.jdbc.tm 
com.atomikos.icatch.console_log_level =INFO

spring配置文件

<!--   tomcat  数据源方式
    <jee:jndi-lookup id="dataSource1" jndi-name="jdbc/t1"/>
    <jee:jndi-lookup id="dataSource2" jndi-name="jdbc/t2"/>

-->


    
    
      <!--  读取数据库连接配置文件 -->
     <bean id="propertyConfig"
         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                 <value>classpath:jdbc.properties</value>
             </list>
         </property>
     </bean>
    
    
    <!-- 第一个数据库   -->  
    <bean id="dataSource1" class="com.atomikos.jdbc.AtomikosDataSourceBean"  
        init-method="init" destroy-method="close">
        <property name="uniqueResourceName" value="mysql/product1" />
        <property name="xaDataSourceClassName"
            value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />  
        <property name="xaProperties">
            <props>
                <prop key="user">${jdbc.username.a}</prop>
                <prop key="password">${jdbc.password.a}</prop>
                <prop key="URL">${jdbc.url.a}</prop>
            </props>
        </property>
        <property name="testQuery">  
            <value>SELECT 1</value>  
        </property>
    </bean>  
    <!-- 第二个数据库-->  
    <bean id="dataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean"  
        init-method="init" destroy-method="close">
        <property name="uniqueResourceName" value="mysql/product2" />
        <property name="xaDataSourceClassName"
            value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />  
        <property name="xaProperties">
            <props>
                <prop key="user">${jdbc.username.b}</prop>
                <prop key="password">${jdbc.password.b}</prop>
                <prop key="URL">${jdbc.url.b}</prop>
            </props>
        </property>
        <property name="testQuery">  
            <value>SELECT 1</value>  
        </property>
    </bean> 
    
    <!-- SessionFactory -->
    <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource1" /> 
    </bean> 
    
    <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource2" /> 
    </bean>  
  
  <!--      
           spring管理atomikos 
-->
    
    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" 
                                    init-method="init" destroy-method="close">
        <description>UserTransactionManager</description>
        <property name="forceShutdown">
            <value>true</value>
        </property>
    </bean>
    
    
    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">  
        <property name="transactionTimeout" value="300" />  
    </bean> 
     
    <bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">  
        <property name="transactionManager" ref="atomikosTransactionManager"/> 
        <property name="userTransaction" ref="atomikosUserTransaction" /> 
        <property name="allowCustomIsolationLevels" value="true"/> 
    </bean> 
    
    <aop:aspectj-autoproxy /> 
    
    <aop:config  proxy-target-class="true">  
        <aop:advisor pointcut="execution(* secoo.server.impl.*.*(..))" 
            advice-ref="txAdvice" /> 
        </aop:config> 
  
    <tx:advice id="txAdvice" transaction-manager="springTransactionManager">  
        <tx:attributes> 
            <tx:method name="insert*" propagation="REQUIRED" read-only="true" />
            <tx:method name="delete*" propagation="REQUIRED" read-only="true" />
            <tx:method name="update*" propagation="REQUIRED" read-only="true" />
            <tx:method name="*" propagation="SUPPORTS" rollback-for="Exception"  />  
        </tx:attributes>  
    </tx:advice>

 
 

<!—mapping 配置 并且指定 用哪个ssessio工厂 -->

<bean id="product1" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface" value="secoo.mapper.Product1"></property>
    <property name="sqlSessionFactory" ref="sqlSessionFactory1"></property>
</bean>

<bean id="product2" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface" value="secoo.mapper.Product2"></property>
    <property name="sqlSessionFactory" ref="sqlSessionFactory2"></property>
</bean>


你可能感兴趣的:(spring,AOP,bean,数据库,Class,attributes)