"Could not obtain transaction-synchronized Session for current thread" Spring 事务管理中的错误



我正在将一个应用程序从Spring 3.2迁移到Spring 5。应用程序使用AWS RDS,其中主数据库作为主或主数据源,副本数据库作为只读数据源。应用程序为主数据源创建一个会话工厂(primarysessionfactory)实例,为只读数据源创建另一个会话工厂(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 - form和Spring -tx 3.2)完美地工作

请帮忙解决这个问题。

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

Session session;
try {
session = sessionFactory.getCurrentSession();
} catch (HibernateException e) {
session = sessionFactory.openSession();
}
return session;

最新更新