Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Spring/Hibernate - No Hibernate Session bound to thread, and configuration does not allow creation..

 
Greenhorn
Posts: 6
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear ranchers,

I have started a project in my spare time to update my Java skills but I've reached a bit of a road block. I've been really impressed with spring and hibernate but the transactions are proving a bit tricky. For information I am using the annotation based declarative transactions. I keep getting the following exception when I make an HTTP request that tries to use the hibernate session factory.



I must have spent 8-10 hours so far trying to fix this one and I'm really at my wits end. If you google this exception then most of the solutions are either missing @Transactional or missing <tx:annotation-driven /> from the config. I have both of these already so I don't think this is my problem. Other bits of advice just say to read the manual, I have done this and as far as I can see I am doing everything required.

Here are the relevant bits of code.

The spring context XML looks like this:


This is the XML config for the data layer


Here is the service implementation class. The interface is just a straight interface with no annotations.


Here is my DAO implementation. Again the interface is simple with no annotations.


Finally here is my simple entity.


I've had a look in debug and the sessionFactory is there but its transactionManager property is null. I must be missing something but I've checked countless tutorials etc and can't for the life of me find it. Hopefully someone can help me. Looking forward to your suggestions!

Thanks in advance
Ben
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd have to guess it is your Propagation setting of SUPPORTS. SUPPORTS never creates a new transaction, so I am assuming anytime anyone calls the service method it is calling without a Transaction and Hibernate requires a Transaction always. You can set the Repository to @Transactional and set its propagation to REQUIRED. Or you can change your service propagation.

Mark
 
Ben Thurley
Greenhorn
Posts: 6
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I tried this suggestion but it didn't make any difference. I also had this suggestion plus a couple others on the spring source site.
springsource topic

To save clicking the link here's the solution.


I also just noticed that you have a component-scan in your other xml file (probably your x-servlet.xml). You should also know that the tx:annotation-driven ONLY works on beans in the SAME applicationcontext. So if your tx:annotation-driven is in your root context (ContextLoaderListener) and you are scanning your services in your DispatcherServlet there is no transactional stuff going to happen.



Based on this advice I moved the tx:annotation-driven to the root context. This did the trick!

Thanks for taking the time to help.
Ben
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Thurley wrote:Hi,

I tried this suggestion but it didn't make any difference. I also had this suggestion plus a couple others on the spring source site.
springsource topic

To save clicking the link here's the solution.


I also just noticed that you have a component-scan in your other xml file (probably your x-servlet.xml). You should also know that the tx:annotation-driven ONLY works on beans in the SAME applicationcontext. So if your tx:annotation-driven is in your root context (ContextLoaderListener) and you are scanning your services in your DispatcherServlet there is no transactional stuff going to happen.



Based on this advice I moved the tx:annotation-driven to the root context. This did the trick!

Thanks for taking the time to help.
Ben



It still doesn't sound completely correct. Here's why.

In a web environment you will have two ApplicationContext, one being the parent one the child. The parent should contain your middle tier objects, tx manager and sessionFactory. Is should also contain the tx:annotation-driven. This parent is created via the ContextLoaderListener.

The child ApplicationContext is the one loaded by the DispatcherServlet and should only load the Web tier beans. The Controllers, ViewResolvers etc. It should be scanning for Service classes, that goes in the parent ApplicationContext.

So now it look like you are loading your Services in the web layer child ApplicationContext and moved your tx:annotation-driven to your xml for the child ApplicationContext, and that isn't where you want it.

Mark

Ah, in the ContextLoaderListener it creates an ApplicationContext that should contain your beans for your middle tier and contain the tx:annotation-driven. Whereas the DispatcherServlet with its init-param will create a Separate child Application context for beans for your web layer. By doing that the parent Application(middle tier beans) can be injected into you web layer beans, but your web
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic