[英]No Hibernate session bound to the thread while using multiple transation managers
I have created two transaction managers(one for READ requests and another for READ-WRITE requests) and two session factories for the same. 我创建了两个事务管理器(一个用于READ请求,另一个用于READ-WRITE请求)和两个会话工厂。
Important snippets are as follows: 重要的片段如下:
<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>
I am getting following exception : 我得到以下异常:
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)
Method which calls DAO is as follows: 调用DAO的方法如下:
@Transactional(rollbackFor = { DependencyException.class, ServiceException.class }, readOnly = true, value="READ")
@Operation("getActiveCategories")
public GetActiveCategoriesResponse getActiveCategories() throws ServiceException, DependencyException
{ .....
......
......
}
Can anyone point me to the part I am doing wrong? 有人能指出我做错的部分吗?
Your configuration is wrong. 您的配置错误。 Hibernate and even spring allows you to create only one session factory per app.
Hibernate甚至spring允许您为每个应用程序仅创建一个会话工厂。 Check out this link : Spring + Hibernate session lifecycle .
看看这个链接: Spring + Hibernate会话生命周期 。 You can create session per transaction.
您可以为每个事务创建会话。
Also check this link : http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/SessionFactory.html 另请查看此链接: http : //docs.jboss.org/hibernate/core/3.5/api/org/hibernate/SessionFactory.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.