简体   繁体   English

寻求针对(3.0.5)的Spring解决方案:没有Hibernate Session绑定到线程,并且配置不允许在此处创建非事务性会话

[英]Seeking a Spring (3.0.5) Solution for: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

I have a Transaction problem on Spring 3.0.5. 我在Spring 3.0.5上有一个交易问题。 In my case I get the so-called exception "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here"... I have tried everything so far. 就我而言,我得到了所谓的异常“没有Hibernate Session绑定到线程,并且配置不允许在此处创建非事务性会话”……到目前为止,我已经尝试了所有方法。 I can see in my log that the transactional services are detected and registered as Spring beans and I can also see in the logs that they are proxied and associated with underlying Spring transaction interceptors. 我可以在日志中看到已检测到事务服务并将其注册为Spring Bean,还可以在日志中看到它们已被代理并与基础Spring事务拦截器相关联。 (Advise) When I run my Spring MVC app my controller will call the service...... :-( but the transaction interceptors are not triggered. (??) I expect my Spring service proxy to open a session and start a transaction before calling my target service method. Since this does not happen, I get the above exception. I have been almost two days on this problem. Tried everything which I found on the internet...but in vain. (建议)当我运行Spring MVC应用程序时,我的控制器将调用该服务...... :-(但是不会触发事务拦截器。(??)我希望我的Spring服务代理打开一个会话并启动一个在调用我的目标服务方法之前进行事务处理。由于这种情况没有发生,所以我得到了上面的异常。我已经花了将近两天的时间了,尝试了我在互联网上发现的所有内容,但是徒劳无功。

I have layered architecture: presentation (springmvc), service (transaction annotated), dataacess (Spring/Hibernate classic sessionfactory). 我有分层的体系结构:表示(springmvc),服务(带事务注释),数据访问(Spring / Hibernate经典sessionfactory)。 My model objects are annotated with jpa (javax.persistence.*). 我的模型对象使用jpa(javax.persistence。*)进行了注释。 My spring context config files are separated in appContext.xml, appContext-service.xml and appContext-dao.xml. 我的spring上下文配置文件位于appContext.xml,appContext-service.xml和appContext-dao.xml中。 I have defined my Spring LocalSessionFactoryBean, Datasource and TransactionManager (HibernateTransactionManager) in appContext-dao.xml. 我已经在appContext-dao.xml中定义了Spring LocalSessionFactoryBean,Datasource和TransactionManager(HibernateTransactionManager)。 I have put in appContext-service.xml where my service implementations resides. 我已将appContext-service.xml放入我的服务实现所在的位置。 In all of my config files I have included and to detect my beans through Controller, Service and Repository annotations. 在我所有的配置文件中,都包含并通过Controller,Service和Repository批注检测我的bean。

I appreciate any kind of help. 我感谢任何帮助。

It sounds like you are doing everything correctly and you know what you are doing. 听起来您做的一切正确,并且知道自己在做什么。 There's not much we can do here unless you show some configuration. 除非您显示一些配置,否则我们在这里无能为力。

What I'd suggest is some debugging. 我建议进行一些调试。

First: do you have Unit tests in the service layer that test the queries you are using? 第一:在服务层中是否有单元测试可以测试您正在使用的查询? Perhaps you can find the error in the service layer. 也许您可以在服务层中找到错误。

Then: debug the MVC app, check the types of the injected services. 然后:调试MVC应用程序,检查注入的服务的类型。 Verify that they are proxies, not the original types. 验证它们是代理,而不是原始类型。

  • If they are the original types, you have an error in your transaction configuration . 如果它们是原始类型,则您的事务配置中有错误。
  • If they are proxies, step through the query methods and verify that the transaction logic is applied. 如果它们是代理,请遍历查询方法并验证是否应用了事务逻辑。

This sounds like accessing a lazily-loaded list or set of you dao after the closing of the transaction. 这听起来像是在事务结束后访问延迟加载的列表或一组您的请求。 This typically happens if you access that list in the view in stead of the controller, as your controller probably calls methods in transaction scope, and then leaves the transaction and forwards the loaded bean to the view. 如果您代替控制器访问视图中的该列表,通常会发生这种情况,因为您的控制器可能会调用事务范围内的方法,然后离开该事务并将加载的Bean转发到视图。

Simple solutions: 简单的解决方案:

  • Configure your data bean to eagerly load 配置您的数据Bean以热切加载
  • Force loading of the dependencies in the controller (just loop through them) 在控制器中强制加载依赖项(只需循环遍历它们)
  • have a look at this article ans possibly also quite a few right here on SO on lazy loading / lazy fetching of one-to-many associations and the like 看一下这篇文章,可能还有很多关于延迟加载/延迟获取一对多关联的信息等等。

Imagine: 想像:

// your dao
public class Foo {
    // lots of other properties
    List<Something> stuff;
    // getters and setter for stuff
}

// repository
@Transactional 
public class FooRepo {
    public Foo loadFoo(int id) {
       ...
    }
}

// Controller
public class Controller {
   public void Control() {
       sessionContext.set("foo", repo.loadFoo(1)); // transaction managed by spring
   }
   public void setFooRepo(FooRepo repo) {this.repo = repo};
}

// View
for (Something something : foo.getStuff()) {
    // Ooops transaction is closed! stuff is lazily loaded so NOT AVAILABLE
    // practical solutions:
    // - do not load lazily (Foo.hbm.xml)
    // - or force loading by getting all Somethings in the controller
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring MVC + Hibernate:没有Hibernate Session绑定到线程,配置不允许在这里创建非事务性的 - Spring MVC + Hibernate: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here Spring:没有Hibernate Session绑定到线程,并且配置不允许在此处创建非事务性会话 - Spring: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here 没有Hibernate会话绑定到线程,配置不允许在这里创建非事务性的 - No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here 没有Hibernate Session绑定到线程,并且配置不允许在不使用@contoller的情况下在此处创建非事务性会话 - No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here without using @contoller 没有Hibernate会话绑定到线程,配置不允许在这里创建非事务性的 - No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here 我收到HibernateException“没有Hibernate Session绑定到线程,并且配置不允许在此处创建非事务性会话” - I am receiving HibernateException “No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here” Spring 3 MVC Hibernate 3.5.4 hibernateTemplate没有关闭连接(非事务性) - Spring 3 MVC Hibernate 3.5.4 hibernateTemplate not closing connections (non-transactional) 非事务性jmsTemplate等待会话结束 - Non-transactional jmsTemplate waits for session end 在另一个实例中从事务方法调用 spring 非事务方法时,事务是否得到传播? - When calling a spring Non-Transactional method from a Transactional Method in another instance, does the transaction get propagated? 带注释的Spring + Hibernate:没有Hibernate会话绑定到线程 - Spring + Hibernate with annotations: No Hibernate Session bound to thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM