Spring 3, Hibernate JPA, Tomcat transactions

I tried to deploy a webapp that uses Spring 3, JPA with the Hibernate implementation and Tomcat. There are domain classes with a DAO layer which is called from the Service layer. The classes are annotated correctly. All of the unit tests work. Actually the whole webapp works deployed on other web containers : but not on Tomcat!

The problem was that in my service’s save() method I required a transaction. Of course! That’s what you have to do. The actual problem is that I needed a transaction in that method but one was not being created.

In the spring config files I did have the usual wiring:

I know you’ve seen this all before.

It helps to modify your logging level for Spring’s transaction classes. Here’s an excerpt from my log4j.properties.

You see that my config did include the tx:annotation-driven element which creates transaction proxies for methods annotated with @Transactional.

One thing I modified was the transaction manager’s mode.

Another part of the problem is Tomcat’s classloader. You need to specify a special classloader. I created a context.xml in my webapp directly (you can create it in various other Tomcat locations).

So, I created a file named src/main/resources/META-INF/context.xml

Since I was using the SpringSource Tools Suite which includes Tomcat that was it. If you are using a Tomcat distribution from Apache, you need to add a Spring jar to you library directory. This jar is included in the Spring download. If you use Maven then you can add the following dependency:

The add it to your tomcat library directory by copying from your Maven repository.

Resources

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.