java.sql.SQLException: HikariDataSource HikariDataSource (HikariPool-1) has been closed

huangapple go评论72阅读模式
英文:

java.sql.SQLException: HikariDataSource HikariDataSource (HikariPool-1) has been closed

问题

> 在线程“task-2”中的异常 org.springframework.jdbc.datasource.init.UncategorizedScriptException: 无法执行数据库脚本;嵌套异常为 org.springframework.jdbc.CannotGetJdbcConnectionException: 无法获取 JDBC 连接;嵌套异常为 java.sql.SQLException: HikariDataSource HikariDataSource (HikariPool-1) 已关闭。

我有一个仅在应用程序启动时插入一些记录的 SQL 脚本。

spring.datasource.data=classpath:kana.sql

它似乎可以正常运行约 10-20 条记录,然后突然停止。

我的 application.properties 文件:

## 默认连接池
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

## PostgreSQL
spring.datasource.url=jdbc:postgresql://10.0.0.100:5432/langsite
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driverClassName=org.postgresql.Driver

spring.datasource.initialization-mode=always
spring.datasource.data=classpath:kana.sql

server.port=4000

# 删除并重新创建表,适用于测试,在生产环境中请将此注释掉
# javax.persistence.schema-generation.scripts.action=create
# javax.persistence.schema-generation.create-source=metadata
# javax.persistence.schema-generation.drop-source=metadata

初始连接似乎是成功的,因为它可以创建表,插入一些记录,但在插入过程中突然出现问题。

英文:

> Exception in thread "task-2" org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: HikariDataSource HikariDataSource (HikariPool-1) has been closed.

I have an SQL script that just inserts some records on app start

spring.datasource.data=classpath:kana.sql

It seems to run through about 10-20 just fine and then suddenly it quits

My application.properties:

## default connection pool
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

## PostgreSQL
spring.datasource.url=jdbc:postgresql://10.0.0.100:5432/langsite
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driverClassName=org.postgresql.Driver

spring.datasource.initialization-mode=always
spring.datasource.data=classpath:kana.sql

server.port=4000

#drop n create table again, good for testing, comment this in production
# javax.persistence.schema-generation.scripts.action=create
# javax.persistence.schema-generation.create-source=metadata
# javax.persistence.schema-generation.drop-source=metadata

It seems to connect successfully initially, because it can create the tables, insert a few records, but then part-way through the insertions it just craps out.

答案1

得分: 2

似乎你的应用程序在连接池中的可用连接上耗尽了。我猜这两者产生了冲突:

这个

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

和这个

spring.datasource.initialization-mode=always
spring.datasource.data=classpath:kana.sql

如果你想通过Hibernate创建模式,请尝试使用此选项来加载初始数据

spring.jpa.properties.hibernate.hbm2ddl.import_files=classpath:kana.sql

无论如何,请参考这篇文章。它应该会有帮助。
https://dimitr.im/loading-initial-data-with-spring

英文:

Looks like your app runs out of available connections in the pool.
I guess these two things conflicting:

this

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

and this

spring.datasource.initialization-mode=always
spring.datasource.data=classpath:kana.sql

If you want to create schema by Hibernate try this option to load initial data

spring.jpa.properties.hibernate.hbm2ddl.import_files=classpath:kana.sql

Anyway refer this article. It should help
https://dimitr.im/loading-initial-data-with-spring

huangapple
  • 本文由 发表于 2020年9月12日 07:27:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63855444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定