获取“HibernateException:没有 Hibernate 会话绑定到线程”,&配置不允许创建..”在执行 Spring Hibernate 时
我正在尝试编写一个带有事务管理支持的简单 spring-hibernate 命令行应用程序,但我不断得到 “org.hibernate.HibernateException:没有 Hibernate Session 绑定到线程,并且配置不允许在此处创建非事务性会话”
我的 Spring 配置文件(applicationContext.xml)
<!-- Context Provider -->
<bean class="com.rps.util.ContextUtil" />
<!-- The transactional service objects -->
<bean id="service" class="com.rps.service.DefaultService" p:parentDao-ref="parentDao" p:childDao-ref="childDao" />
<!--the transactional advice-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!--this transactional advice runs for any service method execution-->
<aop:config>
<aop:advisor pointcut="execution(* com.rps.service..*.*(..))" advice-ref="txAdvice" />
</aop:config>
<!-- The PlatformTransactionManager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />
<!-- The DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/OsivTest" p:username="XXX" p:password="YYY" />
<!-- Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource">
<property name="mappingResources">
<list>
<value>com/rps/domain/Parent.hbm.xml</value>
<value>com/rps/domain/Child.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Data Access Objects -->
<bean id="parentDao" class="com.rps.data.hbm.HibernateParentDao" p:sessionFactory-ref="sessionFactory" />
<bean id="childDao" class="com.rps.data.hbm.HibernateChildDao" p:sessionFactory-ref="sessionFactory" />
DAO 代码(Session Factory 对象集)通过上面上下文文件中配置的 IoC 容器)
public List<T> findAll() {
return sessionFactory.getCurrentSession().createCriteria(getPersistentClass()).list();
}
DefaultService 类:(从 IoC 容器注入的parentDao)
public class DefaultService implements Service {
@Override
public List<Parent> getAllParent() {
return parentDao.findAll();
}
//Other methods
}
主要方法代码:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Service s = (Service) context.getBean("service");
s.getAllParent();
}
我正在使用 hibernate 版本 3.1.3 和 spring 3。 我们如何为每个线程配置一个会话?我认为问题在于没有会话与我的执行线程关联。我假设事务管理器将打开一个会话(如果尚未找到),但它不会这样做。
I am trying to write a simple spring-hibernate command line application with transaction management support, but I am constantly getting
"org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here"
my spring config file (applicationContext.xml)
<!-- Context Provider -->
<bean class="com.rps.util.ContextUtil" />
<!-- The transactional service objects -->
<bean id="service" class="com.rps.service.DefaultService" p:parentDao-ref="parentDao" p:childDao-ref="childDao" />
<!--the transactional advice-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!--this transactional advice runs for any service method execution-->
<aop:config>
<aop:advisor pointcut="execution(* com.rps.service..*.*(..))" advice-ref="txAdvice" />
</aop:config>
<!-- The PlatformTransactionManager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />
<!-- The DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/OsivTest" p:username="XXX" p:password="YYY" />
<!-- Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource">
<property name="mappingResources">
<list>
<value>com/rps/domain/Parent.hbm.xml</value>
<value>com/rps/domain/Child.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Data Access Objects -->
<bean id="parentDao" class="com.rps.data.hbm.HibernateParentDao" p:sessionFactory-ref="sessionFactory" />
<bean id="childDao" class="com.rps.data.hbm.HibernateChildDao" p:sessionFactory-ref="sessionFactory" />
DAO Code (Session Factory object set by IoC container as configured in context file above)
public List<T> findAll() {
return sessionFactory.getCurrentSession().createCriteria(getPersistentClass()).list();
}
DefaultService Class: (parentDao injected from IoC container)
public class DefaultService implements Service {
@Override
public List<Parent> getAllParent() {
return parentDao.findAll();
}
//Other methods
}
Main method code:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Service s = (Service) context.getBean("service");
s.getAllParent();
}
I am using hibernate version 3.1.3 and spring 3.
How do we configure one session per thread? I believe the issues is that no session is being associated with my execution thread. I was assuming that transaction manager will open a session if not already found, but it does not do so.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引用1:
引用2:
(来源)
顺便说一句,当前稳定的 Hibernate 版本是 3.6.7
Quote 1:
Quote 2:
(Source)
BTW, the current stable Hibernate version is 3.6.7