使用多个事务管理器时,没有绑定到线程的Hibernate会话



我为其创建了两个事务管理器(一个用于READ请求,另一个用于READ-WRITE请求)和两个会话工厂。

重要片段如下:

<tx:annotation-driven />
<bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="SessionFactory" />
</bean>
<bean id="txManagerRead"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="SessionFactory" />
    <qualifier value="READ"/>
</bean>
<bean id="txManagerWrite"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="SessionFactory1" />
     <qualifier value="WRITE"/>
</bean>
<bean id="AbstractSessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
    abstract="true">
    <property name="annotatedClasses">
        <list>
.....
....
        </list>
    </property>
    <property name="exposeTransactionAwareSessionFactory">
        <value>true</value>
    </property>
</bean>
<bean id="SessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
    parent="AbstractSessionFactory">
    <property name="hibernateProperties">
        <props>
.........
.........
</props>
</property>
</bean>

<bean id="SessionFactory1"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
    parent="AbstractSessionFactory">
    <property name="hibernateProperties">
        <props>
.........
.........
</props>
</property>
</bean>

我得到以下异常:

Caused by: org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
    at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:574)
    at com.amazon.im.dao.hibernate.GenericDAOImpl.getSession(GenericDAOImpl.java:53)
    at com.amazon.im.dao.hibernate.CategoryDAOImpl.findAllActiveCategories(CategoryDAOImpl.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

调用DAO的方法如下:

 @Transactional(rollbackFor = { DependencyException.class, ServiceException.class }, readOnly = true, value="READ")
@Operation("getActiveCategories")
public GetActiveCategoriesResponse getActiveCategories() throws ServiceException, DependencyException
{ .....
......
......
}

有人能告诉我我做错了什么吗?

根据此尝试使用@Transactional("Tx_Manager_Name")。您的配置似乎是正确的,因为限定符也支持多个Txn管理器。

您的配置是错误的。Hibernate甚至spring都允许您为每个应用程序只创建一个会话工厂。查看此链接:Spring+Hibernate会话生命周期。您可以为每个事务创建会话。

还要检查此链接:http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/SessionFactory.html

相关内容

  • 没有找到相关文章

最新更新