Translations of this page:

Running our Spring JPA example under Tomcat

The cool thing about what we have just done is to have effectively created an application with the power of a fully fledged EBJ 3.0 Enterprise Application that can run under a lightweight Java Web Server container such as Apache Tomcat or Jetty. No need for heavyweight application servers such as Weblogic, JBoss 4.0 or Websphere. Well I'm being slightly disingenuous here as we are bundling the Spring framework which replaces a lot of this functionality. Still it is a powerful concept.

Download and install version 6.0 of Tomcat from the Apache website (this example should also work okay with version 5.*). If, like me, you run Windows then it is really useful to install the Redhat Cygwin Unix-like Environment. To run tomcat go to the bin directory and type startup.sh. Tomcat gets run in the background and listens on port 8080 by default. Make sure you don't have another webserver running on this port. Surf over to

http://localhost:8080/

and you should see the welcome page. It is handy to be able to access the Tomcat Manager and Status applications but you will need to configure a user to do this. Go to the conf directory and edit tomcat-users.xml

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="manager"/>
  <user username="tomcat" password="tomcat" roles="tomcat,manager"/>
</tomcat-users>

Here we have created a user with the role of manager and Tomcat with the username and password shown.

Deploying Spring2JPA

To deploy Spring2JPA to tomcat simply copy the War file from the Maven target directory to the tomcat/webapps directory. Tomcat will unpack the application archive into a directory called, in our case: Spring-JPA-1.0. However we have got a little bit ahead of ourselves.

Setting up a Database

In our test application we used an in memory version of HSQLDB. This is not particularly useful for our deployment as we would like data to persist beyond the execution of the web application. The easiest thing is to configure a standalone version of HSQLDB. Back when we wrote our Spring MVC front end we specified that the service layer was configured using the file applicationContext-jpa.xml. This is similar to the applicationContext.xml file we used during testing however it is located in the WEB-INF directory. For ease of configuration we have moved the jdbc configuration information into a file called jdbc.properties under WEB-INF

jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://localhost:9001
jdbc.username=sa
jdbc.password=

We then tell Spring to access these values using the PropertyPlaceholderConfigurer bean.

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="/WEB-INF/jdbc.properties"/>
</bean>

Then we use these values in the datasource bean.

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
</bean>

Our database server should be listening on port 9001 on the same machine as the webserver.

Running the Database

To make life easy I've included a directory called db. Copy the hsqldb.jar file used during test to this location. Then change into the hsqldb subdirectory. Run server.bat to start the hsql database then run manager.bat. Execute the ../populateDB.txt file in the parent directory to create database tables and to add some test data.

ByteCode Weaving

There is just one more thing. In order for JPA support to work correctly the Spring application framework needs to support bytecode weaving. Weaving is the process of applying aspects (AOP - a whole topic in itself) to target objects at specific join points. We can do this at classload time but Tomcat's classloader does not support this. Instead we need to use a special Spring classloader.

First copying the spring-tomcat-weaver.jar from Spring's dist/weaver directory into Tomcat's lib directory. Then configure Tomcat to use the new classloader in the Web Application's META-INF/context.xml file:

<Context>
   <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>

Part V: Running our Spring JPA example under Jetty

Resources

tech/java/running-spring-jpa-under-tomcat.txt · Last modified: 2008/05/19 14:12 by davidof
Recent changes RSS feed