2

I have this resource declared in my src/main/webapp/META-INF/context.xml

<Resource name="jdbc/myDB" type="javax.sql.DataSource" auth="Container" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://..." username="..." password="..." />

When I deploy my app to Tomcat 8 it runs fine and the resource is available.
But when I try to run via webapp-runnner (locally or on Heroku) using this command:

java -jar target/dependency/webapp-runner.jar target/*.war --enable-naming

I get this warning and the resource is not available:

WARNING: Failed to register in JMX: javax.naming.NamingException: Could not create resource factory instance [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory]

I tried adding these dependencies to my pom.xml but it makes no difference:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <version>2.1.1</version>
</dependency>   
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-dbcp</artifactId>
    <version>7.0.65</version>
</dependency>   

Please advise.

2 Answers2

4

Just a note for those who declare database resources in context.xml:
if it doesn't work because of javax.naming.NoInitialContextException, just remember to run webapp-runner.jar with --enable-naming option, as webapp-runner has JNDI naming disabled by default (unlike Tomcat)

In this case you will need to put tomcat-dbcp in your classpath, as webapp-runner does't have it (unlike Tomcat)

I prefer to use Maven plugin to deploy to Heroku mvn heroku:deploy (not mvn heroku:deploy-war)
pom.xml would include something like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.github.jsimone</groupId>
                                <artifactId>webapp-runner</artifactId>
                                <version>8.5.11.2</version>
                                <destFileName>webapp-runner.jar</destFileName>
                            </artifactItem>
                            <artifactItem>
                                <groupId>org.apache.tomcat</groupId>
                                <artifactId>tomcat-dbcp</artifactId>
                                <version>8.0.33</version>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>com.heroku.sdk</groupId>
            <artifactId>heroku-maven-plugin</artifactId>
            <version>1.1.3</version>
            <configuration>
                <appName>RELPACE_THIS_WITH_YOUR_HEROKU_APP_NAME</appName>
                <processTypes>
                    <web>java $JAVA_OPTS -cp 'target/dependency/*' webapp.runner.launch.Main target/*.war --enable-naming --port $PORT</web>
                </processTypes>
            </configuration>
        </plugin>

    </plugins>
</build>
1

The dbcp2 JAR files need to be place on the classpath of the java command. To do this, you'll need to use the -cp option instead of the -jar option. You command will look like this (assuming the dbcp2 JARs are also in the target/dependency dir):

java -cp target/dependency/*.jar webapp.runner.launch.Main target/*.war --enable-naming
codefinger
  • 10,088
  • 7
  • 39
  • 51