[英]Heroku webapp-runner JNDI naming, resource not available
I have this resource declared in my src/main/webapp/META-INF/context.xml
我在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. 当我将应用程序部署到Tomcat 8时,它运行良好并且资源可用。
But when I try to run via webapp-runnner (locally or on Heroku) using this command: 但是,当我尝试使用以下命令通过webapp-runnner(本地或在Heroku上)运行时:
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: 我尝试将这些依赖项添加到我的pom.xml中,但这没有什么区别:
<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. 请指教。
Just a note for those who declare database resources in context.xml
: 只是为那些在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) 如果由于javax.naming.NoInitialContextException
而无法使用,请记住使用--enable-naming
选项运行webapp-runner.jar,因为webapp-runner默认禁用了JNDI命名(与Tomcat不同)
In this case you will need to put tomcat-dbcp in your classpath, as webapp-runner does't have it (unlike Tomcat) 在这种情况下,您需要将tomcat-dbcp放在类路径中,因为webapp-runner没有它(与Tomcat不同)
I prefer to use Maven plugin to deploy to Heroku mvn heroku:deploy
(not mvn heroku:deploy-war
) 我更喜欢使用Maven插件部署到Heroku mvn heroku:deploy
(而不是mvn heroku:deploy-war
)
pom.xml would include something like this: pom.xml将包含以下内容:
<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>
The dbcp2 JAR files need to be place on the classpath of the java
command. dbcp2 JAR文件需要放置在java
命令的类路径上。 To do this, you'll need to use the -cp
option instead of the -jar
option. 为此,您需要使用-cp
选项而不是-jar
选项。 You command will look like this (assuming the dbcp2 JARs are also in the target/dependency
dir): 您的命令将如下所示(假设dbcp2 JAR也位于target/dependency
目录中):
java -cp target/dependency/*.jar webapp.runner.launch.Main target/*.war --enable-naming
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.