简体   繁体   English

Heroku Webapp运行程序JNDI命名,资源不可用

[英]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.

相关问题 Heroku Webapp运行程序-启用命名 - Heroku webapp-runner --enable-naming Java Web App在带有webapp-runner的Heroku上崩溃 - Java Web App Crashes on Heroku with webapp-runner webapp运行程序错误(NoInitialContextException) - webapp-runner error (NoInitialContextException) 需要帮助Java Web App(webapp运行程序,tomcat)heroku(应用程序崩溃) - Need Help Java Web App (webapp-runner, tomcat) heroku (app crashed) 在webapp-runner中运行的对Heroku Web-app的API调用最终失败,并出现google.common中的NoSuchMethodError和NoClassDefFoundError - API calls to Heroku web-app running in webapp-runner eventually fail with NoSuchMethodError then NoClassDefFoundError in google.common 上下文路径与 webapp-runner 中的 Spring UrlTag 冲突? - Context path conflict with Spring UrlTag in webapp-runner? Spymemcached与hibernate-memcached和webapp-runner发生冲突 - Spymemcached conflict with hibernate-memcached and webapp-runner 使用&#39;webapp-runner&#39;运行Spring Boot应用后,它在“ INFO:Starting ProtocolHandler [“ http-nio-8080”]”行中停止 - After running Spring boot app with 'webapp-runner' it's stoping on a line “INFO: Starting ProtocolHandler [”http-nio-8080“]” 在gradle中为Heroku创建webapp-runner.jar - Creating webapp-runner.jar for Heroku in gradle Heroku 找不到 webapp-runner.jar - Heroku can't find webapp-runner.jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM