0

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

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

在这个配置中,当方法执行时,我们得到一个 session not found 异常。有谁知道为什么?我们使用 spring 3.1.1 和 hibernate 4.0.1

4

0 回答 0