`

Spring3 集成 Hibernate4,使用Atomikos3.8实现分布式事务控制

阅读更多

Spring3 集成 Hibernate4,使用Atomikos实现分布式事务控制

 

注意事项

<!-- 5. 配置事务管理器 注意是,hibernate4,配置成hibernate3报错 -->

           <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">

             <property name="sessionFactory" ref="sessionFactory" />

           </bean>

           

配置文件:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        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-3.0.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" 
> 
           
           <!-- 分布式 事务处理 开始.....-->
            <!-- PropertyPlaceholderconfigure是一个Bean后处理器,它读取属性文件信息,
                    并将这些信息设置为Spring配置文件元数据,这里用于读取数据库连接信息 
            -->
            <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="locations">
                <list>
                  <value>dbconn.properties</value>
                  <!-- 这里可列出多个属性文件 -->
                </list>
              </property>  
            </bean>
            
            <!--1. 配置数据源,使用 Atomikos连接池-->
            <!--mysql数据源-->  
            <bean id="mysqlDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">  
                <description>mysql xa datasource</description>  
                <property name="uniqueResourceName">  
                    <value>mysql_ds</value>  
                </property>  
                <property name="xaDataSourceClassName" value="${dbmysql.driverClassName}" />  
                <property name="xaProperties">  
                    <props>  
                        <prop key="user">${dbmysql.username}</prop>  
                        <prop key="password">${dbmysql.password}</prop>  
                        <prop key="URL">${dbmysql.url}</prop>  
                    </props>  
                </property>  
                <!-- 连接池里面连接的个数 -->   
                <property name="poolSize" value="3"/>   
            </bean>  
    
            <!--oracle数据源-->  
            <bean id="oracleDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"  init-method="init" destroy-method="close">  
                <description>oracle xa datasource</description>  
                <property name="uniqueResourceName">  
                    <value>oracle_ds</value>  
                </property>  
                <property name="xaDataSourceClassName">  
                    <value>${dboracle.driverClassName}</value>  
                </property>  
                <property name="xaProperties">  
                    <props>  
                        <prop key="user">${dboracle.username}</prop>  
                        <prop key="password">${dboracle.password}</prop>  
                        <prop key="URL">${dboracle.url}</prop>  
                    </props>  
                </property>  
                <!-- 连接池里面连接的个数 -->   
                <property name="poolSize" value="3"/>  
            </bean>
            
            <!-- 2.  配置sessionFactory -->
            <!-- 2.1 配置mysql 的 sessionFactory -->
           <bean id="mysqlSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
             <!--dataSource属性指定要用到的连接池--> 
             <property name="dataSource" ref="mysqlDataSource"/> 
           
             <!--指定要用到的实体映射文件--> 
             <property name="mappingResources"> 
                <list> 
                  <value>com/test/hibernate/mapping/News.hbm.xml</value> 
                </list> 
             </property> 
           
             <!--配置Hibernate的属性--> 
             <property name="hibernateProperties"> 
                <value> 
                  hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
                  hibernate.hbm2ddl.auto=update
                  hibernate.show_sql=true
                  hibernate.format_sql=true
                  hibernate.temp.use_jdbc_metadata_defaults=false
                </value> 
             </property>
           </bean>
           
           <!-- 2.2 配置oracle 的 sessionFactory -->
           <bean id="oracleSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
             <!--dataSource属性指定要用到的连接池--> 
             <property name="dataSource" ref="oracleDataSource"/> 
           
             <!--指定要用到的实体映射文件--> 
             <property name="mappingResources"> 
                <list> 
                  <value>com/test/hibernate/mapping/News.hbm.xml</value> 
                </list> 
             </property> 
           
             <!--配置Hibernate的属性--> 
             <property name="hibernateProperties"> 
                <value> 
                  hibernate.dialect=org.hibernate.dialect.OracleDialect
                  hibernate.hbm2ddl.auto=update
                  hibernate.show_sql=true
                  hibernate.format_sql=true
                  hibernate.temp.use_jdbc_metadata_defaults=false
                </value> 
             </property>
           </bean>
            
            <!--3. 配置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>  
    
            <!--4.配置 spring 的 jta分布式事务管理器 -->  
            <bean id="txManager"   class="org.springframework.transaction.jta.JtaTransactionManager">  
                <property name="transactionManager">  
                    <ref bean="atomikosTransactionManager" />  
                </property>  
                <property name="userTransaction">  
                    <ref bean="atomikosUserTransaction" />  
                </property>  
            </bean>
            
           <!--5. 事务配置,主要是使用的transaction-manager不一样
                配置增强处理的bean(相当于切面类),也是Spring自动生成普通
                业务逻辑bean(targetBean)的代理Bean.里面的tx:method配置每个方法的事务属性,
                name配置方法名,可使用通配符.
           -->
           <tx:advice id="txJpaAdvice" transaction-manager="txManager">
              <!-- 配置详细的事务语义 -->
              <tx:attributes>
                <!-- 表示get开头的方法是只读的 -->
                <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
                <!-- 其他方法使用默认的事务设置 -->
                <tx:method name="*" propagation="REQUIRED"/>
              </tx:attributes>
           </tx:advice>
           
           <!-- 6. 全局事务切面配置,AOP元素配置 -->
           <aop:config>
             <!--
                  配置一个切入点 test.hibernate.business包下面所有类的所有方法,都会被加入事务
             -->
             <aop:pointcut id="myJpaPoint" 
                           expression="execution(* test.hibernate.business.*.*(..))" />
             <!-- 配置 (事务代理)切入点(aop:pointcut) 和 切面类(tx:advice),将二者关联起来  -->
             <aop:advisor advice-ref="txJpaAdvice" pointcut-ref="myJpaPoint" />
           </aop:config>
            
            
            <!--7. 配置两个数据源的Dao,并注入 sessionFactory-->
            <bean id = "jtaDaoMysqlImp1" class = "test.hibernate.pojo.dao.NewsDao">   
               <property name="sessionFactory" ref="mysqlSessionFactory"/>   
            </bean>
            <bean id = "jtaDaoOracleImp1" class = "test.hibernate.pojo.dao.NewsDao">   
               <property name="sessionFactory" ref="oracleSessionFactory"/>   
            </bean>
            
            <!-- 8. 配置business业务逻辑层Bean,需要注入两个Dao层Bean -->  
            <bean id = "newsBusiness" class = "test.hibernate.business.NewsBusiness">   
               <property name="jtaNewsDaoMysql" ref="jtaDaoMysqlImp1"/>
               <property name="jtaNewsDaoOracle" ref="jtaDaoOracleImp1"/>   
            </bean>
            
           
           
           
</beans> 

 

分享到:
评论
2 楼 jackyin5918 2014-06-04  
hemingyuan 写道
博主,你的压缩文件貌似打不开啊,能不能上传一个完整的例子,别分成四个文件?谢谢!

不分成四个文件的话,文件尺寸超限制了,这个下载后要改文件扩展名为对应的编号001-004,即 xxxx.001 -xxxx.004四个文件,然后用最新的7z解压xxxx.001即可。
1 楼 hemingyuan 2014-05-30  
博主,你的压缩文件貌似打不开啊,能不能上传一个完整的例子,别分成四个文件?谢谢!

相关推荐

Global site tag (gtag.js) - Google Analytics