tomcat 7 ignoring my context.xml

PrecisionPete picture PrecisionPete · Jan 24, 2013 · Viewed 15.7k times · Source

I hope this is something stupid. But I've been staring at it way too long.

I am upgrading to Tomcat 7 (from 6) on Linux and it's ignoring my context.xml file. If I include the Context (datasource) in the server.xml, it work fine. But I would like to finally get that stuff out of the server.xml.

I have Tomcat 7 running in Windows/Eclipse with the separated context and it works fine.

I have tried the Host copyXML parameter. Even if it does copy the context.xml, it won't use it... unless I include it within the Host in the server.xml. When it fails, it complains that it can't get my MySQL connection: "Unable to get connection, DataSource invalid"

I'm sure the rest of the config is fine since it works when I paste it into the server.xml...

Could someone please point out what I am missing? It's not supposed to be this difficult. Thanks!

BTW: It's plain Tomcat7 (not an rpm), JDK-7u11, on CentOS 5.9 (64).

Locations:

/var/webapps/shop1
    query.jsp
/var/webapps/shop1/META-INF
    context.xml

within server.xml:

<Host name="dev3.domain.net" appBase="/var/webapps" 
    unpackWARs="true" autoDeploy="true">

    <Context docBase="shop1" path="" reloadable="true" />
</Host>

context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource name="jdbc/shop1db" auth="Container"
            type="javax.sql.DataSource"
            factory="org.apache.commons.dbcp.BasicDataSourceFactory"
            maxActive="50" maxIdle="25" maxWait="10000"
            username="user" password="pass"
            driverClassName="com.mysql.jdbc.Driver"
            validationQuery="SELECT 1"
            url="jdbc:mysql://localhost/shop1"
            logAbandoned="true" removeAbandoned="true" removeAbandonedTimeout="60"
    />
</Context>

within web.xml:

<context-param>
    <param-name>javax.servlet.jsp.jstl.sql.dataSource</param-name>
    <param-value>jdbc/shop1db</param-value>
</context-param>

Answer

Xander Plooy picture Xander Plooy · Apr 19, 2013

Do any of these changes work?…

1: in your web.xml snippet, replace javax.servlet.jsp.jstl.sql.dataSource with just javax.sql.dataSource

2: replace your web.xml snippet completely (i.e. the entire context-param section) with:

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/shop1db</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

3: ensure that you really do have a MySQL connector (jar?) available to your web app—e.g. in WEB-INF/lib

Hope something there might help…

… and given this question is a couple of months old now, and should you have resolved it yourself in the meantime, please comment here with the fix that worked for you! :-P