2
votes

I am writing a web application with Struts 3 using Tomcat 7 with Eclipse. When I launch Tomcat, it seems the context is loaded twice (log file shows that applicationContext.xml is read twice).

I have a context.xml file under src/main/webapp/META-INF for Tomcat :

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/dbname" docBase="dbname" reloadable="true" debug="1">
  <Resource name="jdbc/dbname"
            username="dbusername"
            password="dbpassword"
            auth="Container"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/dbname"
            type="javax.sql.DataSource"
            initialSize="5"
            maxActive="120"
            maxIdle="5"
            maxWait="5000"
            poolPreparedStatements="true"
            validationQuery="select 1" />
</Context>

My server.xml declares also (under Host and Engine) :

<Context docBase="dbname" path="/dbname" source="org.eclipse.jst.jee.server:dbname"/>

Without this, Tomcat does not launch the application.

In web.xml, I declare the contextConfigLocation like this :

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>

applicationContext.xml is located under src/main/resources/spring

Those are the two lines from the log file showing the reload :

2012-10-14 00:17:08,191 INFO [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'name': initialization completed in 438 ms
2012-10-14 00:17:10,972 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - Closing WebApplicationContext for namespace 'name-servlet': startup date [Sun Oct 14 00:17:07 CEST 2012]; parent: Root WebApplicationContext

Issue is that on the second reload, it fails because Spring can't find any data source (although one is definitely defined since Spring finds it during the first boot)

3

3 Answers

3
votes

I have a context.xml file under src/main/webapp/META-INF for Tomcat: [...]

My server.xml declares also (under Host and Engine) :

So, you've deployed your webapp twice. Were you expecting something different?

Remove the <Context> from server.xml: it doesn't belong there. Then, your webapp should only load once.

1
votes

I had the war and exploded war deployed in my tomcat server, hence it was loading two application contexts, removing one of them solved the problem

0
votes

To prevent this, we have to set autoDeploy=”false” and deployOnStartup=”false” in the tag:

<Host name="localhost" appBase="webapps" unpackWARs="true"
  autoDeploy="false" deployOnStartup="false">
    <Context path="" docBase="ExampleApp"></Context>

    <!-- Further settings for localhost -->
</Host>