【问题标题】:Heroku cannot find Postgres JDBC DriverHeroku 找不到 Postgres JDBC 驱动程序
【发布时间】:2016-08-09 13:02:18
【问题描述】:

使用 Maven 为 Heroku 编写纯 Java 服务器应用程序。 连接到 Heroku 的 Postgres 数据库。
本地运行时一切正常,使用 IntelliJ 的配置运行 Java 应用程序,将 Heroku 的 DB URL 指定为环境变量。该应用程序工作正常,我可以连接到数据库。
注意:我发现 IntelliJ 以某种方式自动使用它自己的 Postgres 驱动程序,我在 pom.xml 中指定的驱动程序显然被忽略了
但是,当我部署应用程序时,我一连接到数据库就会在 Heroku 日志中收到以下错误:
java.lang.ClassNotFoundException: org.postgresql.Driver

我按照Heroku的教程,但是看到没有连接的时候,我放了

Class.forName("org.postgresql.Driver");

之前

String dbUrl = System.getenv("JDBC_DATABASE_URL");
return DriverManager.getConnection(dbUrl);

只是为了确保找到驱动程序。如果我删除 Class.forName()... 检查,我会收到 no suitable driver found for [my db url] 错误。
关于最后一个错误,我尝试了 JDBC_DATABASE_URLDATABASE_URL 环境变量,甚至使用 sslmode=require 等“手动”构建 DB URL,但仍然没有运气。
我的 pom.xml 指定了 modelVersion、groupId、artifactId、versionname,以及:

<dependencies>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4.1208</version>
    </dependency>
</dependencies>

Heroku 用来启动应用程序的我的 Procfile 如下所示:

web: java $JAVA_OPTS -cp target/classes:"target/dependency/*" Main --port $PORT

当我部署应用程序时,我没有收到任何错误,构建成功。

驱动程序无法加载的原因可能是什么?我是否忘记了 pom.xml 或 Procfile 中的某些内容或其他内容?

【问题讨论】:

  • 澄清一下,当您添加Class.forName 调用时,数据库连接是否有效?
  • 在本地,forName() 捕获 IntelliJ 的 jdbc 驱动程序并且连接正常。在 Heroku 上,forName() 触发 ClassNotFoundException
  • 您能否通过运行heroku run ls target/dependency/ 来检查postgres jar 文件是否在您的Heroku slug 中?
  • 我收到ls: cannot access target/dependency/: No such file or directory
  • 这表明您的 JAR 文件没有被出售(即在构建期间复制到目标目录中)。您是使用git push 还是mvn heroku:deploy 进行部署?

标签: java postgresql maven heroku jdbc


【解决方案1】:

有时,便利性(比如像我一样使用 IntelliJ 的 git 集成推送到 Heroku)会导致不必要的问题。

我使用git push 在 Heroku 上更新我的应用程序。一切都很好,除了所需的库没有到位。
试过mvn heroku:deploy,经过测试,一切正常!感谢codefinger 的建议。

如果有人问,如果您以前从未这样做过,请按照以下方式操作:
1) 将Heroku Maven Plugin 添加到您的pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>com.heroku.sdk</groupId>
                <artifactId>heroku-maven-plugin</artifactId>
                <version>2.0.6</version>
            </plugin>
        </plugins>
    </build>

2) 执行 maven 目标 heroku:deploy.
就是这样

【讨论】:

    【解决方案2】:

    我在本地系统的连接工作中遇到了同样的问题,但在部署时却没有。是同样的问题,没有可访问的依赖项,但是对于使用 JAR 的构建,我只需要确保我使用的是自包含所有依赖项的 JAR。

    【讨论】:

      【解决方案3】:

      有一个比切换到heroku-maven-plugin更直接的解决方案:你可以将postgresql依赖标记为具有runtime范围:

      <dependencies>
          <dependency>
              <groupId>org.postgresql</groupId>
              <artifactId>postgresql</artifactId>
              <version>42.3.1</version>
              <scope>runtime</scope>
          </dependency>
      </dependencies>
      

      默认范围是compile,这会导致 Maven 将其排除在外,因为它没有在代码中引用。

      我添加了这个答案,因为我遇到了同样的问题,但在使用 heroku-maven-plugin 方法时遇到了很多麻烦(我的项目有多个模块,这使事情变得复杂)。采用该插件可能还有其他好处,但在我的情况下,配置依赖项的范围要简单得多。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-17
        • 1970-01-01
        • 2015-06-15
        • 2013-03-05
        • 1970-01-01
        相关资源
        最近更新 更多