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

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

当我的服务调用getCurrentSession() ,它会抛出以下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) 

****编辑:根据Spring Spring 3.1 Documentation for Transactions更新了我的spring-dao.xml。 我尝试用org.apache.commons.dbcp.BasicDataSource交换我的数据源。 我的配置中是否有任何可能导致此问题的属性? ****

这是我的spring-dao.xml:

       hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect       

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

最后我的服务……

 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 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启动日志,我也可以提供它们。

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

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

    

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

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

对于application-context.xml,和

对于webmvc-context.xml。

它是一个Web应用程序吗? 如果是这样,请考虑使用OpenSessionInViewFilter。 因为我相信当使用currentSession(绑定到当前线程)时,代码中必须有一个点从线程解除绑定会话。

我不确定交易经理是否这样做。

我和你的错误一样。

这是一个尚未解决的错误。

https://jira.springsource.org/browse/SPR-9028

尝试将hibernate jar文件更改为3.6。 因为Spring使用它。

http://mvnrepository.com/artifact/org.springframework/spring-orm/3.1.0.RELEASE

这里是Spring 3.1工件和依赖项

如Spring Reference (3.2.x)中所述:

在Web MVC框架中,每个DispatcherServlet都有自己的WebApplicationContext,它inheritance了根WebApplicationContext中已定义的所有bean。 可以在特定于servlet的作用域中重写这些inheritance的bean,并且可以为给定的Servlet实例定义新的作用域特定的bean。

因此,使用定义或扫描的Bean将在您的控制器中可见,因此您可以@Autowired它们,但在其他applicationContext *文件中将不可见,因此除非不是在DispatcherServlet的配置中定义的@Transactional将不起作用。

所以我猜你可能在你的DispatcherServlet的配置中有 ,在applicationContext * .xml中有声明,所以@Autowired工作正常,但@Transactional不行。

我有同样的问题并测试了所有已回答的解决方案。 Vali的回答非常有帮助。 对我有用的是将这些bean从applicationContext.xml移动到web-servlet.xml中:

    classpath:hibernate.cfg.xml    org.hibernate.dialect.MySQL5Dialect true        

此外,您需要添加web-servlet.xml:

 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation=" http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " 

在web.xml中添加OpenSessionInViewFilterfilter

  hibernateFilter org.springframework.orm.hibernate4.support.OpenSessionInViewFilter  sessionFactoryBeanName sessionFactory    hibernateFilter /* REQUEST FORWARD  

不确定,但问题可能出在p:packagesToScan 。 您的ConfigurationUserService在com.foo.lystra.services包中,但是p:packagesToScancom.foo.lystra.beans

您的配置未指向带注释的类。 添加它们

    hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect    test.package.Foo test.package.Bar    

它类似于之前存在的AnnotationSessionFactoryBean 。 在这里查看Api。

我相信你需要:

  

否则,spring上下文将找不到您的服务,因此不会使用Transactional方面包装您的方法。

有完全相同的错误,它只是通过为我的服务创建一个接口解决。 所以在你的情况下,我会创建:

 public interface ICongregationUserService { public List getAllUsers(); } 

然后更改CongregationUserService以实现它:

 @Service("congregationUserService") @Transactional public class CongregationUserService implements ICongregationUserService{ //... } 

并且您自动assembly了CongregationUserService,自动assemblyICongregationUserService:

 @Autowired private ICongregationUserService congregationUserService; 

我通过在dispatcher-servlet.xml中放置而不是任何其他xml配置文件来解决这个问题。

我认为这种方式允许bean在相同的spring环境中共存。

我发现这个问题是spring的错误

这个链接https://jira.springsource.org/browse/SPR-9020报告问题..

解决它我已经使用了Matias Mirabelli的解决方法,可以在这个链接上找到https://gist.github.com/seykron/4770724

发生的事情是使用Propagation.SUPPORTS注释的方法支持事务但是如果没有绑定到线程的事务而不是创建新的会话,则抛出HibernateException

为了配置sollution,你可以使用hibernate属性:

hibernate.current_session_context_class = com.your.package.TransactionAwareSessionContext

我放

  #(some class files are annotaed by @Repository,@Service,@Component)   

int root.xml。

我说

  #(some class files are annotaed by @Controller)  

int servlet-context.xml。

有用。