7

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>
PrecisionPete
  • 3,139
  • 5
  • 33
  • 52
  • If you use MySQL then maybe this example from the official Tomcat documentaion will help you: [MySQL DBCP Example](http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html#MySQL_DBCP_Example). By the way the default port for MySQL is **3306**, i.e. it should be `url="jdbc:mysql://localhost:3306/shop1"`, of course if you haven't changed it – informatik01 Jan 25 '13 at 00:08
  • Yes... I had the port there before. Same result though. I think there has to be a setting related to reading the context.xml file. If I set copyXML to true, then it does copy and rename it correctly. So it knows it's there. It just won't do anything with it. Again, if I include the context.xml text in the Host in the server.xml, then it works just fine. But that practice is not ideal... – PrecisionPete Jan 25 '13 at 00:40
  • I can always go back to that. But this was my opportunity to do it right... – PrecisionPete Jan 25 '13 at 00:51
  • 1
    Actually according to the official Tomcat docs related to [The Context container](http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Defining_a_context) (quote): "**It is NOT recommended to place elements directly in the server.xml file**. This is because it makes modifying the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat." So you are right about not wanting to include context in server.xml – informatik01 Jan 25 '13 at 13:47
  • Also **are you sure** you need `docBase` and `path` attributes in `Context` element (just in case here is the description: [Context attributes](http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Attributes))? Maybe trying without them will help. I usually just include custom _context.xml_ without `docBase` or `path` attributes under _META-INF_ in the form like I described in one of my questions here: http://stackoverflow.com/a/13631242/814702. Works like a charm. Of course, maybe YOU have a reason to include those attributes. I just never needed that. – informatik01 Jan 25 '13 at 14:29
  • Also have configured _web.xml_ accordingly like it is described in [MySQL DBCP Example](http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html#MySQL_DBCP_Example) (see `3. web.xml configuration`)? You just need to experiment. Sorry, if my suggestion didn't help you. – informatik01 Jan 25 '13 at 14:31
  • Tried just in context.xml but same problem. I added my web.xml above. Again, all this works if I stick it in the server.xml. Also, I added dummy parameters on both the server.xml context as in the context.xml. Tomcat complained about both so I know now it is actually reading both files. I just read something that commons-dbcp is getting old and there is a better tomcat-dbcp. Holding my breath. Thanks again. – PrecisionPete Jan 25 '13 at 20:33
  • Just to be clear, I have the tomcat-dbcp etc, mysql-connector, all in the tomcat/lib. None of the DB stuff is in the WEB-INF/lib. I just don't get it. – PrecisionPete Jan 25 '13 at 21:06
  • Using factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" gave the same result... Ugh. – PrecisionPete Jan 25 '13 at 21:15
  • Sorry, it really is weird. I also used new Tomcat JDBC Pool - no problems whatsoever ... – informatik01 Jan 25 '13 at 21:26
  • Do you need the element inside ? We don't use it, and Tomcat picks up META-INF/context.xml without needing to be told anything extra. – Darius X. Jan 25 '13 at 22:03
  • Without the Context in the Host, how does it determine the Path? i.e. / vs /somedir ? Interesting. I'll try that... – PrecisionPete Jan 26 '13 at 01:49
  • @PrecisionPete were you able to resolve the issue? – WannaBeGeek Dec 10 '14 at 08:50
  • No. I've just been living with it... – PrecisionPete Dec 11 '14 at 14:47
  • @PrecisionPete Ok. Thats very sad ! I am also having hard time finding the cause of this error. – WannaBeGeek Dec 12 '14 at 07:04
  • Same problem for me using postgres. Would also be desirable to anly need to include the web app and the server.xml into SVN, – user1767316 Aug 24 '16 at 09:33
  • Same problem for me using postgres. Would also be desirable to only need to include into SVN, the web app's context.xml instead of the Server config file server.xml – user1767316 Aug 24 '16 at 09:56

1 Answers1

0

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