在Heroku上部署后的HTTPS状态500 - Java,Maven?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (173)

我使用Java和Maven创建了一个Web应用程序(这是我的项目:https//github.com/lkobylanski/Parking_Book_System_Java)。其简单的停车位预订系统。它在我的本地tomcat上完美运行。但是当我使用WAR部署在Heroku上部署它时:https//devcenter.heroku.com/articles/war-deployment

应用程序运行,但每次我尝试对数据库执行任何操作时都会收到500 Server错误,例如添加新预订或获取API,无论是特定停车位还是全局预订。这是错误:

HTTP Status 500 – Internal Server Error

Type Exception Report

Message java.lang.IllegalStateException: Could not initialize database

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: java.lang.IllegalStateException: Could not initialize database
    com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.init(BookControllerServlet.java:40)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:748)

Root Cause

java.lang.IllegalStateException: Could not initialize database
    com.pbs.web.jdbc.ParkingBookSystem.BookDbUtil.<init>(BookDbUtil.java:27)
    com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.init(BookControllerServlet.java:37)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:748)

Root Cause

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
    javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
    javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
    javax.naming.InitialContext.lookup(InitialContext.java:417)
    com.pbs.web.jdbc.ParkingBookSystem.BookDbUtil.<init>(BookDbUtil.java:24)
    com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.init(BookControllerServlet.java:37)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:748)

Note The full stack trace of the root cause is available in the server logs.
Apache Tomcat/8.5.33

这是我连接到我的数据库的方式:我有一个放在root / src / main / webapp / META-INF中的context.xml文件

它看起来像(用户名和传递不是真的):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context path="/ParkingSystem">
    <Resource 
        name="jdbc/ParkingBookSystem" 
        auth="Container"
        type="javax.sql.DataSource"
        maxTotal="20"
        maxIdle="5"
        maxWaitMillis="10000"
        username="username"
        password="password"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://sql2.freemysqlhosting.net:3306/sql2226123"
    />
</Context>

这是连接构造方法:

public BookDbUtil() {
    try {
        Context initContext = new InitialContext();
        Context envContext = (Context) initContext.lookup("java:/comp/env");
        dataSource = (DataSource) envContext.lookup("jdbc/ParkingBookSystem");
    } catch (NamingException e) {
        throw new IllegalStateException("Could not initialize database", e);
    }
}
提问于
用户回答回答于

你需要添加Tomcat webapp-runner选项,--enable-naming并且--context-xml=path/to/context.xmlWEBAPP_RUNNER_OPTS喜欢这样:

$ heroku config:set WEBAPP_RUNNER_OPTS="--enable-naming --context-xml=path/to/context.xml"

您需要将context.xml作为WAR之外的文件,并且您需要--include在Heroku Java CLI中包含该选项。

扫码关注云+社区

领取腾讯云代金券