Spring:HibernateTransactionManager 处理多个数据源 [英] Spring: HibernateTransactionManager handling multiple datasources

查看:54
本文介绍了Spring:HibernateTransactionManager 处理多个数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码段(Spring 3)中:

In the following piece of code (Spring 3):

@Transactional("txManager")
public class DaoHolder {

    @Transactional(value="txManager", readOnly=false, propagation=Propagation.REQUIRES_NEW, rollbackFor={Exception.class})
    private void runTransactionalMethod() throws Exception {
        dao1.insertRow();
        dao2.insertRow();
        //throw new Exception();
    }
    //...
}

  • dao1 使用附加到 datasource1 的会话工厂
  • dao2 使用附加到 datasource2 的会话工厂
  • txManager 是一个 HibernateTransactionManager 使用与 dao1 相同的会话工厂
    • dao1 uses a session factory attached to datasource1
    • dao2 uses a session factory attached to datasource2
    • txManager is a HibernateTransactionManager using the same session factory as dao1
    • 上面的代码以事务方式正常工作 - 特别是,当没有抛出异常时,每个 dao 操作都会被提交(到 2 个不同的数据源).当抛出异常时,每个 dao 操作都会回滚.

      The code above works correctly in a transactional manner - in particular, when no exception is thrown, each dao operation gets committed (to 2 different datasources). When an exception is thrown each dao operation gets rolled back.

      我的问题是:它为什么有效?我读过的所有地方都告诉我在处理多个数据源时使用 JtaTransactionManager.我不想使用 JTA.如果我让它在 HibernateTransactionManager 下运行会产生什么后果?

      My question is: why does it work? Everywhere I've read I've been told to use a JtaTransactionManager when handling multiple datasources. I'd prefer not to use JTA. What might be the consequences if I leave it running under a HibernateTransactionManager?

      感兴趣的更多细节:

      每个数据源的定义如下:

      Each datasource is defined like so:

      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
          <property name="driverClassName" value="${jdbc.driverClassName}" />
          <property name="url" value="${jdbc.url}" />
          <property name="username" value="${jdbc.username}" />
          <property name="password" value="${jdbc.password}" />
          <property name="initialSize" value="${jdbc.initial_size}" />
          <property name="maxActive" value="${jdbc.max_active}" />
      </bean>
      

      每个会话工厂的定义如下:

      Each session factory is defined like so:

      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          <property name="dataSource" ref="dataSource" />
          <property name="mappingResources">
              <list>
                  ... multiple *.hbm.xml files here ...
              </list>
          </property>
          <property name="hibernateProperties">
              <props>
                  <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                  <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
              </props>
          </property>
      </bean>
      

      事务管理器的定义如下:

      The transaction manager is defined like so:

      <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory"/>
      </bean>
      

      每个 dao 类都扩展了 HibernateDaoSupport,insertRow 方法的内容或多或少与 dao1 类似:

      Each dao class extends HibernateDaoSupport and the content of the insertRow method is more or less like so for dao1:

      getHibernateTemplate().save(obj);
      

      对于 dao2:

      getHibernateTemplate().merge(obj);
      

      推荐答案

      问题:

      我刚刚花了一天时间处理这个确切的问题:为什么跨数据源的事务似乎与一个休眠事务管理器一起工作?

      和你一样,我也在多个地方读到我需要使用 JtaTransactionManager ......结果证明他们是对的!我来解释一下:

      Like you, I also read in multiple places that I needed to use a JtaTransactionManager...and it turns out they were right! I'll explain:

      和您一样,我从 2 个数据源、2 个会话工厂和 1 个 HibernateTransactionManager 开始.

      Just like you, I started with 2 data sources, 2 session factories, and 1 HibernateTransactionManager.

      我的测试代码看起来也与你的非常相似,我可以成功地将对象保存到两个数据库中.如果我手动抛出异常,则数据库中不会出现任何保存.所以看起来两者都被正确回滚了.但是,当我打开 hibernate 的调试日志记录时,我可以看到实际上没有将保存发送到数据库,因此没有什么可以回滚.

      My test code also looked very similar to yours and I could save objects to both databases successfully. If I manually threw an exception, neither save would appear in the database. So it seemed that both were being rolled back correctly. However, when I turned on hibernate's debug logging, I could see that neither save was actually sent to the databases so there was nothing to rollback.

      问题出在测试中,所以我将更改您的测试以证明单个事务管理器实际上不起作用!

      JB Nizet 于 1 月 2 日提出了我们需要的更改:

      The change we need was suggested by JB Nizet on Jan 2:

      在抛出异常之前,您是否尝试过在两个会话上调用flush?

      Have you tried calling flush on both sessions before throwing the exception?

      <小时>

      更好的测试:

      首先,为每个 DAO 添加一个刷新函数.这是我的样子:


      A better test:

      First, add a flush function to each of your DAO's. This is what mine looks like:

      public void flush() {
          sessionFactory.getCurrentSession().flush();
      }
      

      你的可能看起来像这样:

      Yours will probably look like this:

      public void flush() {
          getHibernateTemplate().flush();
      }
      

      现在,修改您的测试以在异常之前刷新每个 dao:

      Now, modify your test to flush each dao before the exception:

       @Transactional("txManager")
      public class DaoHolder {
      
          @Transactional(value="txManager", readOnly=false, propagation=Propagation.REQUIRES_NEW, rollbackFor={Exception.class})
          private void runTransactionalMethod() throws Exception {
              dao1.insertRow();
              dao2.insertRow();
      
              dao1.flush();
              dao2.flush();
      
              throw new Exception();
          }
          //...
      }
      

      结果:

      仅回滚与 txManager 关联的数据源.这是有道理的,因为 txManager 不知道其他数据源.

      The result:

      Only the datasource associated to txManager is rolled back. That makes sense, because txManager does not know about the other data source.

      就我而言,我不需要在一个事务中访问 2 个数据库,单独的事务就可以了.所以我简单地定义了第二个事务管理器:

      In my case, I do not need to access 2 databases in one transaction, separate transactions is fine. So I simply defined a second transaction manager:

      <bean id="txManager2" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory2"/>
      </bean>
      

      并在我访问第二个数据库的任何地方在事务注释中按名称引用它:

      And referenced this by name in the Transactional annotation wherever I access the second database:

      @Transactional(value="txManager2"...)
      


      我现在可以为我的第二个数据库获取带注释的事务,但我仍然无法获取跨两个数据库的事务...看来您需要一个 JtaTransactionManager 来实现这一点.


      I can now get annotated transactions for my second database, but I still cannot get transactions across both databases...it seems that you will need a JtaTransactionManager for that.

      这篇关于Spring:HibernateTransactionManager 处理多个数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆