1

我正在将应用程序从 Spring 3.2 迁移到 Spring 5。该应用程序使用 AWS RDS,主数据库作为主数据源或主数据源,副本数据库作为只读数据源。应用程序为主要数据源创建一个会话工厂 (primarySessionFacotry) 实例,为只读数据源创建另一个 (readOnlySessionFactory) 实例,以便通过连接每个会话工厂,可以将相同的 DAO 用于两个数据源。

appContext-hibernate.xml

<bean id="primaryDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="jdbcUrl">
        <value>${database.jdbcurl}</value>
    </property>
    <property name="user">
        <value>${database.dbuser}</value>
    </property>
    <property name="password">
        <value>${database.dbpassword}</value>
    </property>

</bean>

<bean id="primarySessionFactory"
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="primaryDataSource"/>
    <property name="mappingResources" ref="hbmFileLocations">
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">${hibernate.showSQL}</prop>
            <prop key="hibernate.format_sql">${hibernate.formatSQL}</prop>
        </props>
    </property>
</bean>

<util:list id="hbmFileLocations" value-type="java.lang.String">
    <value>com/xxxx/yyyyy/persistence/mappings/users.hbm.xml</value>
</util:list>

<bean id="primaryTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="primarySessionFactory">
    </property>
</bean>

<tx:annotation-driven transaction-manager="primaryTransactionManager"/>

appContext-hibernate-ro.xml

<bean id="readOnlyDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="jdbcUrl">
        <value>${database.readonly.jdbcurl}</value>
    </property>
    <property name="user">
        <value>${database.readonly.dbuser}</value>
    </property>
    <property name="password">
        <value>${database.readonly.dbpassword}</value>
    </property>
    
</bean>

<bean id="readOnlySessionFactory"
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="readOnlyDataSource"/>
    <property name="mappingResources" ref="hbmFileLocations">
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">${hibernate.readonly.showSQL}</prop>
            <prop key="hibernate.format_sql">${hibernate.readonly.formatSQL}</prop>
        </props>
    </property>

</bean>

<bean id="readOnlyTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="readOnlySessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="readOnlyTransactionManager"/>

DAO 是这样实例化的

<bean id="userDao" class="xxxxx.persistence.UserDao">
    <property name="sessionFactory" ref="primarySessionFactory"/>
</bean>
<bean id="readOnlyUserDao" class="xxxxx.persistence.UserDao">
    <property name="sessionFactory" ref="readOnlySessionFactory"/>
</bean>

这个 UserDao 有一个类似下面的方法

@Transactional
public List<User> getItems()
{
    return sessionFactory.getCurrentSession().createQuery("from User user where isDeleted=false").list();
}

当使用带有 readOnlyUserDao 实例的 UserDao.getItems() 方法时,我们遇到了异常。

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:143)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:497)

请注意,相同的代码与 Spring 完美配合(spring-orm 和 spring-tx 3.2)

请协助解决此问题。

4

1 回答 1

0

我遇到了同样的问题,并按照此处提到的步骤操作,这对我的情况很好。 Spring Hibernate - 无法为当前线程获取事务同步会话

Session session;
try {
    session = sessionFactory.getCurrentSession();
} catch (HibernateException e) {
    session = sessionFactory.openSession();
}
return session;
于 2021-03-18T08:37:27.070 回答