4

I am building a maven test project which runs with: Spring MVC 3.1.1 JPA, with Hibernate 4.1.4 as jpa provider MYSQL 5.5.x TOMCAT 7

I'm trying to create a simple end-end basic use case which involves reading from DB and update. Since I like to stick with pure JPA I'm using spring's LocalContainerEntityManagerFactoryBean, injected to my dao using @PersistenceContext annotation.

The problems I had at this point:

  1. I could not persist to DB (no exception thrown but no db reflection as well).
  2. I could not delete form DB (no exception thrown but no db reflection as well).

I could although read from the DB.

After going over the documentation I saw that I might missing some JPA support since TOMCAT does not include this out of the box.

I went through this and this and that (and a few more..) and applied the following:

  • Added the spring-instrument-tomcat dependency to my pom.xml (just to retrieve the jar).
  • I copied that jar to my tomcat's lib directory
  • Under META-INF directory, I created a context.xml file with the following content:

    <Context docBase="SpringTest" path="/SpringTest" reloadable="true"
        source="org.eclipse.jst.jee.server:SpringTest">
        <Loader
            loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"
            useSystemClassLoaderAsParent="false" />
    </Context>
    
  • I added InstrumentationLoadTimeWeaver to my entity-manager definition

And that was supposed to do the trick - but it didn't. I experienced the same behaviour.

Then I encountered this question from stackoverflow. One of the answers suggested to add the transactions definition: <tx:annotation-driven proxy-target-class="true" /> to the DispatcherServlet context (so far I had it only in my root application context). After doing that things started to work out, the transactions got committed to the DB.

At this point I started wondering if all the rest of the stuff are still required, so I rolled back everything I did beside the addition to the DispatcherServlet and it still works perfectly!

SO, my questions: How does it work? Do I really need all those to support JPA in tomcat? And finally, why do I need to copy the declaration to the DispatcherServlet in order to resolve this?

Thanks in advance, Yogi

Community
  • 1
  • 1
forhas
  • 11,551
  • 21
  • 77
  • 111
  • I had the same problem, and I have no idea. Good question, it would be interesting to know. – Ido Cohn Aug 28 '12 at 09:34
  • I think to sum it up no Tomcat doesn't handle transactions for you automatically. No container should. And you need a transaction to make any database changes. There was likely an error or message being displayed somewhere in the loggers but your levels didn't expose them. No you do not need the instrument stuff you added. Annotated transaction support is in my opinion the best way to implement transaction handling in any modern Spring 3.x application. I'm not too sure about why the declaration was not working when it was in root context. It should hypothetically work there. – skel625 Aug 29 '12 at 06:00

1 Answers1

0

you dont need "spring-instrument-tomcat dependency to my pom.xml" in order to CRUD your database. The fact that you can read but not write or update your databse with zero exceptions shows ok configuration that is missing spring-transactions (proper annotating @Transactional, proper declaring annotation driven spring context). Dont load Tomcat with unessecary jars. Start a new spring template project (Simple spring jpa utility project) in Spring-source toolsuite and check what dependencies are really needed for spring-hibernate-jpa projects

George Papatheodorou
  • 1,539
  • 19
  • 23