When trying to initialize the application, I got an error.
r2dbc + Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.r2dbc.pool.ConnectionPool]: Factory method 'connectionFactory' threw exception with message: Unable to create a ConnectionFactory for 'ConnectionFactoryOptions{options={database=company, host=127.0.0.1, driver=postgresql, password=REDACTED, port=5433, user=postgres}}'. Available drivers: [ pool ]
I found a solution, maybe it will be useful to someone. (for migrating scripts for databases, flywaydb is used)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>9.16.1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
DB_IP: 127.0.0.1
DB_PORT: 5432
DB_DATABASE_NAME: db
DB_SCHEMA: public
database:
url: postgresql://localhost:5432/db
server:
port: 8090
error:
include-message: always
spring:
r2dbc:
username: postgres
password: postgres
url: r2dbc:postgresql://127.0.0.1:5432/db
flyway:
baseline-description: true
baseline-on-migrate: true
create-schemas: true
enabled: true
default-schema: ${DB_SCHEMA}
locations: classpath:db/migration
password: ${spring.r2dbc.password}
schemas: public
url: jdbc:${database.url}
user: ${spring.r2dbc.username}
validate-on-migrate: true
It's too late but it might help someone.
I had the same issue with spring boot 3.3.4, java 21.
Resolved by adding the following poms;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
</dependency>
Then created a Initializer Class;
@Component
public class Initializer {
@Bean
public ConnectionFactoryInitializer initializerYangu(ConnectionFactory connectionFactory) {
ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
initializer.setConnectionFactory(connectionFactory);
CompositeDatabasePopulator populator = new CompositeDatabasePopulator();
populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("data.sql")));
populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("schema.sql")));
initializer.setDatabasePopulator(populator);
return initializer;
}
}
This is my repository;
public interface ProductRepo extends ReactiveCrudRepository<Product, Integer> {
}
The data.sql and schema.sql files should be placed in the resources folder;
data.sql
CREATE SCHEMA IF NOT EXISTS myschema;
CREATE TABLE IF NOT EXISTS myschema.product (
id INT NOT NULL PRIMARY KEY,
name TEXT,
description TEXT
);
schema.sql
insert into myschema.product values (1,'prod','mpya');
The application.properties file is as below;
spring.application.name=reactjaribio
server.port=9090
spring.r2dbc.url=r2dbc:postgresql://localhost/jaribiodb
spring.r2dbc.username=postgres
spring.r2dbc.password=postgres
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With