Blaine Lafreniere
Blaine Lafreniere

Reputation: 3600

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

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.

Upvotes: 4

Views: 19307

Answers (1)

Daniil Bratin
Daniil Bratin

Reputation: 131

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

Upvotes: 2

Related Questions