开发者

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

开发者 https://www.devze.com 2023-02-14 05:56 出处:网络
I have a Transaction problem on 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 a Transaction problem on 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. 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. (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.

I have layered architecture: presentation (sprin开发者_Go百科gmvc), service (transaction annotated), dataacess (Spring/Hibernate classic sessionfactory). My model objects are annotated with jpa (javax.persistence.*). My spring context config files are separated in appContext.xml, appContext-service.xml and appContext-dao.xml. I have defined my Spring LocalSessionFactoryBean, Datasource and TransactionManager (HibernateTransactionManager) in appContext-dao.xml. I have put in appContext-service.xml where my service implementations resides. In all of my config files I have included and to detect my beans through Controller, Service and Repository annotations.

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. 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.

Simple solutions:

  • Configure your data bean to eagerly load
  • 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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消