未找到当前线程的会话(Spring 3.1.X 和 Hibernate 4)

我正在尝试使用Spring 3.1和Hibernate 4来设置我的项目。我一直在线学习一些教程。我得到了一个奇怪的错误,根据春季论坛应该用Spring 3.1修复。春季虫子追踪器

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

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)

编辑:更新了我的spring-dao.xml根据Spring Spring 3.1交易文档。我尝试过将我的数据源换成org.apache.commons.dbcp.BasicDataSource。我的配置中是否缺少任何可能导致此问题的属性?****

这是我的春道.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" />

我的用户豆 (用户.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();
}
}

最后是我的服务...

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();
}
}

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


答案 1

我在Web应用程序中遇到了同样的问题。问题在于两个配置文件都存在:应用程序上下文.xml和webmvc上下文.xml。webmvc-context.xml在应用程序-context-.xml之后加载。我认为当加载应用程序上下文.xml时,DAO类首先加载事务引用,但是当加载webmvc上下文时,它将替换为另一个对象,没有事务引用.xml。无论如何,我都可以解决扫描的特定包的问题:

应用程序上下文.xml和

webmvc上下文.xml。<context:component-scan base-package="com.app.repository" /><context:component-scan base-package="com.app.web" />


答案 2

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

解决方案是将所有注释驱动、组件扫描、注释驱动的指令从根上下文.xml移动到 servlet 上下文.xml:

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

dataSource,sessionFactory和transacterManager仍然可以在根上下文中定义.xml。


推荐