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

我为每个配置创建了两个具有不同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()返回错误。

最佳答案

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

07-24 15:11