14

在以下代码中(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 相同的会话工厂

上面的代码以事务方式正常工作- 特别是,当没有抛出异常时,每个 dao 操作都会被提交(到 2 个不同的数据源)。当抛出异常时,每个 dao 操作都会回滚。

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



有兴趣的更多细节:

每个数据源的定义如下:

<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>

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

<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>

事务管理器的定义如下:

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

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

getHibernateTemplate().save(obj);

对于 dao2:

getHibernateTemplate().merge(obj);
4

1 回答 1

14

问题:

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

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:

Configuration:

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

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.

The problem is in the test, so I'll change your test to prove that the single transaction manager is actually not working!

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

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


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();
}

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();
    }
    //...
}

The result:

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

Summary:

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"...)


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.

于 2012-04-03T20:53:26.480 回答