java - 驱动程序 :org. postgresql.Driver@3ed03652 为 URL 返回 null ... 在将 spring boot 部署到 Heroku 时

标签 java spring postgresql maven heroku

我尝试在 Heroku 上部署我的应用程序,但有一些我无法修复的错误 我的 application.poperties

    spring.mvc.view.prefix = /WEB-INF/view/
spring.mvc.view.suffix = .jsp

spring.datasource.url = jdbc:postgres://user:pass@ec2-54-247-166-129.eu-west-1.compute.amazonaws.com:5432/database

# Username and password
spring.datasource.username = user
spring.datasource.password = pass

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

spring.datasource.driverClassName=org.postgresql.Driver

# ===============================
# = JPA / HIBERNATE
# ===============================

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

我的应用程序类是用于应用程序启动的标准 spring 引导类

我的 pom.xml(其中的一部分):

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>

    </dependencies>
    <build>
        <finalName>spring</finalName>
        <plugins>
            <!-- Spring Boot Maven -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.github.jsimone</groupId>
                                    <artifactId>webapp-runner</artifactId>
                                    <version>8.0.30.2</version>
                                    <destFileName>webapp-runner.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我收到的错误

> 12:06:42.061  INFO 12352 --- [ost-startStop-1]
> o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter:
> 'applicationContextIdFilter' to: [/*] 2017-03-10 12:06:42.488 ERROR
> 12352 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      :
> Unable to create initial connections of pool.
> 
> java.sql.SQLException: Driver:org.postgresql.Driver@760cf594 returned
> null for
> URL:jdbc:postgres://fqwebtafwgsgmp:17ff02102b2660162ce1b053ef28340dfa7cf4d71e14b4bf1b187a8ad7d36579@ec2-54-247-166-129.eu-west-1.compute.amazonaws.com:5432/daapseha11ekml
>   at
> org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:329)
> ~[tomcat-jdbc-8.5.11.jar:na]  at
> org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203)
> ~[tomcat-jdbc-8.5.11.jar:na]  at
> org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:732)
> [tomcat-jdbc-8.5.11.jar:na]   at
> org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:664)
> [tomcat-jdbc-8.5.11.jar:na]   at
> org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:479)
> [tomcat-jdbc-8.5.11.jar:na]   at
> org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
> [tomcat-jdbc-8.5.11.jar:na]   at
> org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
> [tomcat-jdbc-8.5.11.jar:na]   at

如果有人解释我的错误,我将不胜感激

最佳答案

您在 JDBC URL 中使用 jdbc:postgres 而不是 jdbc:postgresql(注意额外的 ql)。

Heroku 已经为您设置了 SPRING_DATASOURCE_URL 环境变量,因此您应该能够完全删除 application.properties 中的 spring.datasource 条目.

如果您无法删除它们,请尝试:

spring.datasource.url=${JDBC_DATABASE_URL}

如果这不起作用,那一定意味着您没有使用 Java buildpack 或其他东西。

还要确保运行:

heroku pg:credentials --reset

关于java - 驱动程序 :org. postgresql.Driver@3ed03652 为 URL 返回 null ... 在将 spring boot 部署到 Heroku 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42715888/

相关文章:

java - 使用SimpleXml解析xml时第-1行报错

java - Android 检测运行失败 - 断言错误

java - 在单个 webview Android 中从一个页面导航到另一个页面

java - Spring 启动和 hibernate : print/log DDL

html - 我可以禁用为复选框标记 : in Spring MVC? 生成隐藏字段吗

django - 如何在 Django 数组字段中执行全文搜索

sql - 提高 Postgresql 查询的性能

JAVA - boolean 修正 - 按“播放”时仍然继续

java - 为什么我在尝试执行涉及 2 个表之间联接的 HQL 查询时会获取此 "ErrorCounter - Path expected for join!"?

postgresql - SQL (PostgreSQL) : How to write a query to fetch the average salary of top 100 highest paid employees?