多个SessionFactory / TransactionManager:找不到当前线程的会话

时间:2016-02-22 22:50:16

标签: java spring hibernate

我有两个并行运行的数据库。

我为每个配置创建了两个具有不同GenericDAO / SessionFactories的配置,但是要实例化的第二个SessionFactory存在问题。当我尝试 getCurrentSession()时,我收到此错误: org.hibernate.HibernateException:找不到当前线程的会话

Config1.xml

<bean id="mainDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/main" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

<bean id="mainSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="mainDataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.avocat.domain.entities.main.User</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<bean id="mainTransactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="dataSource" ref="mainDataSource" />
    <property name="sessionFactory" ref="mainSessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="mainTransactionManager" />
<context:annotation-config />

Config2.xml

<bean id="fakeDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/fake" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

<bean id="fakeSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="fakeDataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.avocat.domain.entities.intra.Bla</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<bean id="fakeTransactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="dataSource" ref="fakeDataSource" />
    <property name="sessionFactory" ref="fakeSessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="fakeTransactionManager" />
<context:annotation-config />

MainGenericDAO

@Autowired
@Qualifier("mainSessionFactory")
private SessionFactory mainSessionFactory;

protected Session currentSession()
{
    return mainSessionFactory.getCurrentSession();
}

FakeGenericDAO

@Autowired
@Qualifier("fakeSessionFactory")
private SessionFactory fakeSessionFactory;

protected Session currentSession()
{
    return fakeSessionFactory.getCurrentSession();
}

Config1在Config2之前声明。
此外,使用MainGenericDAO / MainSessionFactory的查询也可以。 但是FakeGenericDAO的 currentSession()会返回错误。

1 个答案:

答案 0 :(得分:1)

似乎未在DAO的第二个FakeSessionFactory上创建交易。您必须使用@Transactional("fakeTransactionManager")上的FakeGenericDAO