Spring和hibernate多个数据源的事务管理

1、准备工作

我的项目是struts2+spring+hibernate架构,web服务用tomcat;

现在遇到的问题是要连接多个数据库一个Oracle一个SqlServer,现在把我配置过程分享给大家!

使用jta事务,用tomcat+jotm提供事务管理器 货运专家

请先下载相应的jotm的jar包,放到工程中的lib包中

2、配置hibernate配置文件,有几个数据库就配几个这样的文件

    我的配sqlserver数据库的文件如下:

例如:hibernate_sqlserver.cfg.xml
<hibernate-configuration>
    <session-factory>
        <property name="connection.username">sa</property>
        <property name="connection.url">jdbc:microsoft:sqlserver://192.168.0.100:1433;DatabaseName=PrisonSoftWeb1
</property>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect
</property>
        <property name="myeclipse.connection.profile">test</property>
        <property name="connection.password">server</property>
    <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
</session-factory>
<hibernate-configuration>
其他数据库和其类似!

3、配置sping::applicationContext.xml中添加相应的session工厂

(1)以下两个bean用于spring对jotm初始化
  <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>
如果您的程序中要处理blob类型的数据就添加如下信息:
<!-- blob 处理 -->
    <bean id="nativeJdbcExtractor" lazy-init="true"  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>


以下信息为开发中依据实际情况,程序员或者设计人员添加的内容:
(2)添加session工厂(session工厂的添加注意,必须有一个工厂的id=“sessionFactory”)
<!-- oracle 数据库sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="configLocation"
           value="classpath:hibernate.cfg.xml">
       </property>
       <property name="hibernateProperties">
         <props>
          <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
          <prop key="hibernate.current_session_context_class">jta</prop>
          <prop key="hibernate.connection.release_mode">after_statement</prop>
          <prop key="hibernate.show_sql">true</prop>
          <prop key="hibernate.format_sql">true</prop>
         <prop key="hibernate.order_updates">true</prop>
         </props>
       </property>
       <!-- 为处理Blob类型字段的句柄声明 -->
       <property name="lobHandler">
          <ref local="lobHandler"/>
       </property><!--这就是上面声明的句柄-->
       <property name="jtaTransactionManager">
         <ref bean="jotm" />
      </property>
    </bean>


<!-- sqlserver 数据库sessionFactory -->

<bean id="sessionFactoryForSqlServer" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <property name="configLocation"
           value="classpath:hibernate_SqlServer.cfg.xml">
       </property>
       <property name="hibernateProperties">
         <props>
      <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
          <prop key="hibernate.show_sql">true</prop>
         </props>
       </property>
       <property name="jtaTransactionManager">
         <ref bean="jotm" />
      </property>
    </bean>
(3)为dao注入对应的session工厂
<bean id="sqlServerbaseDao" class="com.gkzx.dao.BaseDaoImpl">
        <property name="sessionFactory">
           <ref bean="sessionFactoryForSqlServer" />
       </property>  
     </bean>
4、手动提交:Dao实现中采用手动提交和回滚事务的办法,避免数据库因事务不能及时提交而引起死锁现象

     this.getSession().clear();
       Transaction tx = null;
    try{
        tx = this.getSessionFactory().getCurrentSession().beginTransaction();
      
        //这里写你的操作数据库的代码:例如:
         this.getHibernateTemplate().update(objBean);
        tx.commit();
    }catch(RuntimeException e){
        if(tx != null)tx.rollback();
        throw e;
    }finally{
        //this.getSession().close();
    }

原文地址:https://www.cnblogs.com/sky7034/p/2134411.html