无法使用 Heroku postgres 将数据添加到 Spring Boot 上的数据库
Posted
技术标签:
【中文标题】无法使用 Heroku postgres 将数据添加到 Spring Boot 上的数据库【英文标题】:Cant add data to databases on Spring Boot using Heroku postgres 【发布时间】:2020-05-31 16:17:41 【问题描述】:我一直在尝试使用 Heroku 启动和运行 Rest 存储库。但是,当我正在使用的表被添加到数据源时,我无法上传任何数据行。
Shows that my tables are being added to postgres datasource
我没有收到任何错误消息或部署服务的问题,我只是无法添加数据。
我在互联网上寻找解决方案,目前 application.properties 是一堆不同的代码,我在所有指南中复制了如何让数据库运行。
#spring.h2.console.enabled=true
#spring.datasource.url=jdbc:h2:mem:testdb
#spring.datasource.driverClassName=org.h2.Driver
#spring.datasource.username=sa
#spring.datasource.password=
#spring.h2.console.settings.web-allow-others=true
#spring.main.banner-mode=off
#logging.level.org.springframework=ERROR
spring.jpa.hibernate.ddl-auto=update
#spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
#spring.datasource.url=postgres://npwovtbfrmcgap:8cb0a1d61d6608e756d4340bb79926156b43c5d602580ab21884d058b7adf230@ec2-52-23-14-156.compute-1.amazonaws.com:5432/deu5uhuf0in93s
#spring.datasource.url=$JDBC_DATABASE_URL
#spring.datasource.username=$SPRING_DATABASE_USERNAME
#spring.datasource.password=$SPRING_DATABASE_PASSWORD
#spring.jpa.show-sql=true
#spring.jpa.generate-ddl=true
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.datasource.url=$JDBC_DATABASE_URL
spring.datasource.username=$JDBC_DATABASE_USERNAME
spring.datasource.password=$JDBC_DATABASE_PASSWORD
spring.jpa.show-sql=false
spring.jpa.generate-ddl=true
为了测试,我使用了 h2,它运行良好,没有任何问题。尽管我一直在关注使用 JDBC _DATABASE 以便于设置,但我已经制作了一个数据库配置文件。
package com.example.chess.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
@Configuration
public class DatabaseConfig
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource()
return new org.apache.tomcat.jdbc.pool.DataSource();
这里没有很多,但据我了解,如果我使用的是 JDBC 数据库,我不应该需要一个。
我的一些想法......
Heroku 可能会迫使我使用他们的指南来准备用于生产的 Spring Boot 应用程序 https://devcenter.heroku.com/articles/preparing-a-spring-boot-app-for-production-on-heroku
我可以在我拥有的表的代码上输入更好的 @GeneratedValue。
package com.example.chess.Model;
import javax.persistence.*;
@Entity
public class Chat
@GeneratedValue
@Id
@Column
private int chatID;
@Column
private String chatMsg;
public int getChatID()
return chatID;
public String getChatMsg()
return chatMsg;
public void setChatID(int chatID)
this.chatID = chatID;
public void setChatMsg(String chatMsg)
this.chatMsg = chatMsg;
过去我不太确定该怎么做。感谢您提供任何反馈。
感谢所有帮助,但我仍然没有得到它的工作。我使用 heroku 日志来查找更多数据。我还使用了 spring.database.platform 和 spring.datasource.driver-class-name。应用程序属性的代码已更新。
2020-02-18T02:22:38.000000+00:00 app[api]: Build succeeded
2020-02-18T02:22:41.021726+00:00 app[web.1]: 2020-02-18 02:22:41.017 INFO 4 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$28f59fce] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-02-18T02:22:41.264675+00:00 app[web.1]:
2020-02-18T02:22:41.264732+00:00 app[web.1]: . ____ _ __ _ _
2020-02-18T02:22:41.264786+00:00 app[web.1]: /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2020-02-18T02:22:41.264849+00:00 app[web.1]: ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2020-02-18T02:22:41.264905+00:00 app[web.1]: \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2020-02-18T02:22:41.264972+00:00 app[web.1]: ' |____| .__|_| |_|_| |_\__, | / / / /
2020-02-18T02:22:41.265009+00:00 app[web.1]: =========|_|==============|___/=/_/_/_/
2020-02-18T02:22:41.265886+00:00 app[web.1]: :: Spring Boot :: (v2.1.7.RELEASE)
2020-02-18T02:22:41.265938+00:00 app[web.1]:
2020-02-18T02:22:41.335856+00:00 app[web.1]: 2020-02-18 02:22:41.335 INFO 4 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-02-18T02:22:41.566210+00:00 app[web.1]: 2020-02-18 02:22:41.565 INFO 4 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2020-02-18T02:22:41.566423+00:00 app[web.1]: 2020-02-18 02:22:41.566 WARN 4 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/application/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
2020-02-18T02:22:41.570798+00:00 app[web.1]: 2020-02-18 02:22:41.570 INFO 4 --- [ main] com.example.chess.ChessApplication : No active profile set, falling back to default profiles: default
2020-02-18T02:22:42.945648+00:00 app[web.1]: 2020-02-18 02:22:42.943 INFO 4 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2020-02-18T02:22:43.135659+00:00 app[web.1]: 2020-02-18 02:22:43.135 INFO 4 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 157ms. Found 4 repository interfaces.
2020-02-18T02:22:43.711365+00:00 app[web.1]: 2020-02-18 02:22:43.711 INFO 4 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=de70dafe-40e0-316f-93ef-ef4b324ee017
2020-02-18T02:22:43.965692+00:00 app[web.1]: 2020-02-18 02:22:43.965 INFO 4 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$cdb9cd1] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-02-18T02:22:44.025903+00:00 app[web.1]: 2020-02-18 02:22:44.025 INFO 4 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$28f59fce] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-02-18T02:22:44.561043+00:00 app[web.1]: 2020-02-18 02:22:44.560 INFO 4 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 37710 (http)
2020-02-18T02:22:44.670131+00:00 app[web.1]: 2020-02-18 02:22:44.669 INFO 4 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-02-18T02:22:44.670471+00:00 app[web.1]: 2020-02-18 02:22:44.670 INFO 4 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2020-02-18T02:22:45.421352+00:00 app[web.1]: 2020-02-18 02:22:45.421 INFO 4 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-02-18T02:22:45.421626+00:00 app[web.1]: 2020-02-18 02:22:45.421 INFO 4 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3826 ms
2020-02-18T02:22:45.859027+00:00 app[web.1]: 2020-02-18 02:22:45.858 INFO 4 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-02-18T02:22:46.292693+00:00 app[web.1]: 2020-02-18 02:22:46.292 INFO 4 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-02-18T02:22:46.414129+00:00 app[web.1]: 2020-02-18 02:22:46.413 INFO 4 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
2020-02-18T02:22:46.414131+00:00 app[web.1]: name: default
2020-02-18T02:22:46.414131+00:00 app[web.1]: ...]
2020-02-18T02:22:46.563652+00:00 app[web.1]: 2020-02-18 02:22:46.563 INFO 4 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core 5.3.10.Final
2020-02-18T02:22:46.566628+00:00 app[web.1]: 2020-02-18 02:22:46.566 INFO 4 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2020-02-18T02:22:46.824490+00:00 app[web.1]: 2020-02-18 02:22:46.824 INFO 4 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations 5.0.4.Final
2020-02-18T02:22:47.331763+00:00 app[web.1]: 2020-02-18 02:22:47.331 INFO 4 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2020-02-18T02:22:47.616894+00:00 app[web.1]: 2020-02-18 02:22:47.616 INFO 4 --- [ main] o.h.e.j.e.i.LobCreatorBuilderImpl : HHH000421: Disabling contextual LOB creation as hibernate.jdbc.lob.non_contextual_creation is true
2020-02-18T02:22:47.624236+00:00 app[web.1]: 2020-02-18 02:22:47.623 INFO 4 --- [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@37e4d7bb
2020-02-18T02:22:48.869877+00:00 app[web.1]: 2020-02-18 02:22:48.869 INFO 4 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-02-18T02:22:49.329464+00:00 app[web.1]: 2020-02-18 02:22:49.329 INFO 4 --- [ main] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
2020-02-18T02:22:50.308149+00:00 app[web.1]: 2020-02-18 02:22:50.307 INFO 4 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-02-18T02:22:50.411820+00:00 app[web.1]: 2020-02-18 02:22:50.409 WARN 4 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-02-18T02:22:50.461378+00:00 app[web.1]: 2020-02-18 02:22:50.461 INFO 4 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
2020-02-18T02:22:51.073701+00:00 heroku[web.1]: State changed from starting to up
2020-02-18T02:22:50.863588+00:00 app[web.1]: 2020-02-18 02:22:50.863 INFO 4 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 37710 (http) with context path ''
2020-02-18T02:22:50.865846+00:00 app[web.1]: 2020-02-18 02:22:50.865 INFO 4 --- [ main] com.example.chess.ChessApplication : Started ChessApplication in 12.027 seconds (JVM running for 12.976)
【问题讨论】:
日志中有任何内容吗?可以尝试将 PostgreSQL 方言添加到应用程序属性中。 不幸的是我没有使用日志记录。我已将spring.datasource.platform=postgres
添加到 application.properties,但没有成功。我还能添加什么?如果我仍然没有让它工作,我会研究日志记录以及我必须让它在原始问题中工作的其他想法。
您应该添加日志。如果您在日志中发现任何异常,那么您就知道从哪里开始调试。
【参考方案1】:
终于修好了。男孩,我觉得自己很笨吗,问题出在我发布并从 localhost:8080 获取的角度前端。
$http.get("http://localhost:8080/letter").then(function (response)
$scope.letter = response.data;
);
$scope.postLetter = function(letter)
var data =
contactName: contactName,
email: email,
message: message
;
$http.post("http://localhost:8080/letter", JSON.stringify(data));
location.reload();
;
再次感谢所有尝试提供帮助的人。
【讨论】:
【参考方案2】:尝试添加方言、驱动程序类名称和数据库平台,就像您评论的那样。
【讨论】:
以上是关于无法使用 Heroku postgres 将数据添加到 Spring Boot 上的数据库的主要内容,如果未能解决你的问题,请参考以下文章
Heroku Postgres 升级后 Flyway 无法连接到数据库
无法将 AWS-Postgres 服务器与带有 heroku 托管的 Spring Boot 应用程序连接起来