多个会话工厂/事务管理器/事务模板的问题 - Spring / Hibernate

时间:2012-08-16 09:55:20

标签: java spring hibernate

我们需要为同一数据源定义2个会话工厂。在其中一个会话工厂中,我配置了jdbc批处理,另一个没有配置。以下是spring.xml的外观如下所示。我想保持会话工厂不同的原因在某些情况下我们想要hibernate批处理,在其他情况下我们不要。

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  ..datasource props here...       
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="show_sql">${show_sql}</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            ..packages here....
        </list>
    </property>
</bean>

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

<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager"/>
</bean>

<bean id="alwaysNewTransactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW"/>
</bean>

<tx:annotation-driven/>

<bean id="sessionFactoryForBatching" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="show_sql">${show_sql}</prop>
            <prop key="hibernate.jdbc.batch_size">${jdbc.batchSize}</prop>
            <prop key="hibernate.order_inserts">true</prop>
            <prop key="hibernate.order_updates">true</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            ..same packages here..
        </list>
    </property>
</bean>

<bean id="transactionManagerForBatching" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactoryForBatching"/>
    <qualifier value="txManagerBatch"/>
</bean>

<bean id="transactionTemplateForBatching" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManagerForBatching"/>
</bean>

<bean id="alwaysNewTransactionTemplateForBatching" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManagerForBatching"/>
    <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW"/>
    <qualifier value="txnTemplateWithBatching"/>
</bean>

在代码中我将方法标记为

@Transactional("txManagerBatch")
public void method(){

    // stuff happens here where we got to the database to get stuff

    txnTemplate..execute(new TransactionCallback<Integer>() {
        @Override
        public Integer doInTransaction(TransactionStatus transactionStatus) {
            //stuff here that happens in the inner transaction
        }
    });

    //stuff happens here where we update the database    
}

在此配置中,当方法执行时,我们会收到会话未找到的异常。有人知道为什么吗?我们使用spring 3.1.1和hibernate 4.0.1

0 个答案:

没有答案