找不到当前线程的会话(Spring 3.1.X和Hibernate 4) [英] No Session found for current thread (Spring 3.1.X and Hibernate 4)

查看:147
本文介绍了找不到当前线程的会话(Spring 3.1.X和Hibernate 4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Spring 3.1和Hibernate 4设置我的项目。我一直在线上学习一些教程。我得到一个奇怪的错误,根据春季论坛应该已经修复了Spring 3.1。 Spring Bug Tracker



当我的服务调用 getCurrentSession()时,它会抛出以下异常:

  org.hibernate.HibernateException:**没有找到当前线程的会话**]带有根本原因org.hibernate.HibernateException:没有找到当前线程的会话
在org.springframework.orm.hibernate4.SpringSessionContext.currentSession(
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:881)


$ SpringSessionContext.java:97) b $ b

****编辑:根据Spring更新我的spring-dao.xml Spring 3.1文档中的事务。我试着用org.apache.commons.dbcp.BasicDataSource替换掉我的数据源。我的配置中是否有任何属性可能导致此问题? ****



以下是我的spring-dao.xml:

 <! - 启用管理交易的注释风格 - > 
< tx:注解驱动的事务管理器=transactionManager/>

< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =hibernateProperties>
< value> hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect< / value>
< / property>
< / bean>

<! - 声明具有池化功能的数据源 - >
< bean id =dataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSource
destroy-method =close
p:driverClass =$ {app.jdbc。
p:jdbcUrl =$ {app.jdbc.url}
p:user =$ {app.jdbc.username}
p:password =$ {app.jdbc .password}
p:acquireIncrement =5
p:idleConnectionTestPeriod =60
p:maxPoolSize =100
p:maxStatements =50
p:minPoolSize =10/>

<! - 声明一个事务管理器 - >
< bean id =transactionManagerclass =org.springframework.orm.hibernate4.HibernateTransactionManager
p:sessionFactory-ref =sessionFactory/>

我的用户bean(User.java)

  package com.foo.lystra.beans; 

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name =users)
public class User implements Serializable {
private static final long serialVersionUID = -5527566191402296042L;

@Id
@Column(name =idusers)
private Integer user_id;

@Column(name =login_name)
private String loginName;

@Column(name =password)
private String password;

@Column(name =role)
私人字符串角色;

@Column(name =congregation_id)
private Integer congregation_id;

public Integer getUser_id(){
return user_id;
}
public void setUser_id(Integer user_id){
this.user_id = user_id;
}
public String getLoginName(){
return loginName;
}
public void setLoginName(String loginName){
this.loginName = loginName;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
public String getRole(){
return role;
}
public void setRole(String role){
this.role = role;
}
public Integer getCongregation_id(){
return congregation_id;
}
public void setCongregation_id(Integer congregation_id){
this.congregation_id = congregation_id;
}

public String toString(){
returnuser_name:+ this.loginName +congregation_id:+ this.congregation_id.toString();


$ / code $ / pre

最后我的服务...

  package com.foo.lystra.services; 

import java.util.List;

import javax.annotation.Resource;

导入org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foo.lystra.beans.User;
import com.foo.lystra.beans.Congregation;

@Service(congregationUserService)
@Transactional
public class CongregationUserService {
protected log Logger = LogFactory.getLog(CongregationUserService.class);

@Resource(name =sessionFactory)
private SessionFactory sessionFactory;

公共列表<用户> getAllUsers(){
logger.debug(获取所有用户);

//在下一行中引发异常:
Session session = sessionFactory.getCurrentSession();

Query query = session.createQuery(FROM users);
返回query.list();




$ b我知道我的数据源可能没有被使用。如果我忘记了包含任何配置,我可以更新这篇文章。另外如果需要Tomcat启动日志,我也可以提供它们。

解决方案

我在spring-4.0.6中遇到了这个问题和$ hibernate-4.3.6。

解决方案是将所有注释驱动的,组件扫描,注释驱动的指令从root-context.xml移动到servlet-context。 xml:


 < mvc:annotation-driven /> 
< context:component-scan base-package =ru.dd.demo/>
< tx:注解驱动的事务管理器=transactionManager/>

dataSource,sessionFactory和transactionManager仍然可以在root-context.xml中定义。


I'm trying to set up my project using Spring 3.1 and Hibernate 4. I've been following some tutorials online. I'm getting a strange error that according to the spring forums should have been fixed with Spring 3.1. Spring Bug Tracker

When my service calls getCurrentSession(), it throws the following exception:

org.hibernate.HibernateException: **No Session found for current thread**] with root cause org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:881)

****EDIT: updated my spring-dao.xml according to the Spring Spring 3.1 Documentation for Transactions. I've tried swapping out my datasource with a org.apache.commons.dbcp.BasicDataSource. Are there any properties I am missing from my configuration that could be causing this? ****

Here's my spring-dao.xml:

 <!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />   

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <value>hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect</value>
    </property>
</bean>

<!-- Declare a datasource that has pooling capabilities-->   
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close"
            p:driverClass="${app.jdbc.driverClassName}"
            p:jdbcUrl="${app.jdbc.url}"
            p:user="${app.jdbc.username}"
            p:password="${app.jdbc.password}"
            p:acquireIncrement="5"
            p:idleConnectionTestPeriod="60"
            p:maxPoolSize="100"
            p:maxStatements="50"
            p:minPoolSize="10" />

<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
            p:sessionFactory-ref="sessionFactory" />

My User bean (User.java)

package com.foo.lystra.beans;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="users")
public class User implements Serializable {
private static final long serialVersionUID = -5527566191402296042L;

@Id
@Column(name = "idusers")
private Integer user_id;

@Column(name="login_name")
private String loginName;

@Column(name="password")
private String password;

@Column(name="role")
private String role;

@Column(name="congregation_id")
private Integer congregation_id;

public Integer getUser_id() {
    return user_id;
}
public void setUser_id(Integer user_id) {
    this.user_id = user_id;
}
public String getLoginName() {
    return loginName;
}
public void setLoginName(String loginName) {
    this.loginName = loginName;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String getRole() {
    return role;
}
public void setRole(String role) {
    this.role = role;
}
public Integer getCongregation_id() {
    return congregation_id;
}
public void setCongregation_id(Integer congregation_id) {
    this.congregation_id = congregation_id;
}

public String toString() {
    return "user_name: " + this.loginName + " congregation_id: " + this.congregation_id.toString();
}
}

And finally my service...

package com.foo.lystra.services;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foo.lystra.beans.User;
import com.foo.lystra.beans.Congregation;

@Service("congregationUserService")
@Transactional
public class CongregationUserService {
protected static Log logger = LogFactory.getLog(CongregationUserService.class);

@Resource(name="sessionFactory")
private SessionFactory sessionFactory;

public List<User> getAllUsers() {
    logger.debug("getting all users");

            //Exception is thrown on this next line:
    Session session = sessionFactory.getCurrentSession();

    Query query = session.createQuery("FROM users");
    return query.list();
}
}

I realize that my datasource is probably not getting used. If I have forgotten to include any configurations I can update this post. Also if the Tomcat startup logs are needed I can provide them as well.

解决方案

I had this problem with spring-4.0.6 and hibernate-4.3.6.

Solution is to move all annotation-driven, component-scan, annotation-driven directives from root-context.xml to servlet-context.xml:

<mvc:annotation-driven />
<context:component-scan base-package="ru.dd.demo" />
<tx:annotation-driven transaction-manager="transactionManager" />

dataSource, sessionFactory and transactionManager can be still defined at root-context.xml.

这篇关于找不到当前线程的会话(Spring 3.1.X和Hibernate 4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆