• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why do transactions not start when bean definitions are moved to <application>-servlet.xml?

 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two spring configuration files. applicationContext.xml springappcustomized-servlet.xml


Calling the savePerson service method throws..

org.hibernate.HibernateException: No Hibernate Session bound to thread, and conf
iguration does not allow creation of non-transactional one here


When I move the bean definitions of PersonDAO and personManager from springappcustomized-servlet.xml
to applicationContext.xml the savePerson service method works and the Person is inserted to the DB.

Also I can see from the logs that in the non working configuration transactions are not
started while in the working configuration transactions are started.



Is anyone able to explain what happens here and if I am missing anything?

Why is it that things work when the PersonDAO and personManager definitions
are moved to the applicationContext but not when they are in springappcustomized-servlet.xml?





applicationContext.xml


springappcustomized-servlet.xml




web.xml




The savePerson method of SimplePersonManager

 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Posted this also here at the springsource community forums in hope of a response..
 
ranger
Posts: 17347
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
Because you don't have a transaction manager defined in that config file.

Basically in Spring MVC apps you want and will have two separate ApplicationContexts. One for the web layer, one for the middle tier. The middle tier is where you define your Services, DAOs, DataSource, TransactionManager etc. In the Web Layer you will have your Controllers, MappingHandlers and ViewResolvers. For the middle tier that is usually called applicationContext.xml. The <application>-servlet.xml would be the web layer with controllers etc. The ApplicationContext created for the MiddleTier is called the Parent Application Context, and the one created via the <application>-servlet is the child Application Context. The Child has access to the parent application context classes, but not the other way around. So for an architecture for SpringMVC and lower layers you have


Web Layer - defined in the <application>-servlet,xml

All below defined in the applicationContext.xml
Service Layer
DAO Layer
Infrastructure Layer - includes DataSources, SessionFactory, TransactionManager etc

Now the applicationContext.xml you want loaded via the ContextLoaderListener defined in your web.xml and the <application>-servlet.xml is loaded via the DispatcherServlet through its init-param in the web.xml


Hope that helps clear things up. You have to think in terms of two application contexts getting created and ContextLoaderListener is there to create the middle tier beans and the DispatcherServlet for the web tier.

Mark
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ranger.

That clears things up quite a bit.

Would appreciate if you have any references on this at hand...
reply
    Bookmark Topic Watch Topic
  • New Topic