aspose file tools*
The moose likes Spring and the fly likes Why do transactions not start when bean definitions are moved to <application>-servlet.xml? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Why do transactions not start when bean definitions are moved to <application>-servlet.xml?" Watch "Why do transactions not start when bean definitions are moved to <application>-servlet.xml?" New topic
Author

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

Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
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

Joined: Aug 05, 2008
Posts: 347
Posted this also here at the springsource community forums in hope of a response..
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17256
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Thanks ranger.

That clears things up quite a bit.

Would appreciate if you have any references on this at hand...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do transactions not start when bean definitions are moved to <application>-servlet.xml?