I'd like to know how can I configure a datasource programmatically withtout using application.properties.
Spring Boot way:
@Configuration
public class KubDataSourceConfig {
@Bean
public DataSource getDataSource() {
Map<String, String> credentials = getCredentials();
return DataSourceBuilder.create()
.url(credentials.get("url"))
.username(credentials.get("username"))
.password(credentials.get("password"))
.build();
}
}
While there isn't a way to do this similar to Spring Boot, you can achieve that with this workaround.
Create a class that implements javax.sql.DataSource, just like below:
`
@ApplicationScoped
public class CustomDataSource extends OracleDataSource {
private static final long serialVersionUID = 1L;
public CustomDataSource() throws SQLException {
super();
}
@Override
public Connection getConnection() throws SQLException {
OracleDataSource oracleRootSource = new OracleDataSource();
oracleRootSource.setServerName("hostname");
oracleRootSource.setServiceName("sid");
oracleRootSource.setPortNumber(1521);
oracleRootSource.setDriverType("thin");
oracleRootSource.setUser("user");
oracleRootSource.setPassword("pass");
return oracleRootSource.getConnection();
}
}
`
It's not that easy, but you can create an AgroalDataSource directly
// create supplier
AgroalDataSourceConfigurationSupplier dataSourceConfiguration = new AgroalDataSourceConfigurationSupplier();
// get reference to connection pool
AgroalConnectionPoolConfigurationSupplier poolConfiguration = dataSourceConfiguration.connectionPoolConfiguration();
// get reference to connection factory
AgroalConnectionFactoryConfigurationSupplier connectionFactoryConfiguration = poolConfiguration.connectionFactoryConfiguration();
// configure pool
poolConfiguration
.initialSize(10)
.maxSize(10)
.minSize(10)
.maxLifetime(Duration.of(5, ChronoUnit.MINUTES))
.acquisitionTimeout(Duration.of(30, ChronoUnit.SECONDS));
// configure supplier
connectionFactoryConfiguration
.jdbcUrl("jdbcUrl")
.credential(new NamePrincipal("username"))
.credential(new SimplePassword("password"));
AgroalDataSource datasource=AgroalDataSource.from(dataSourceConfiguration.get());
This can all be collapsed to a couple lines, just broke it apart to see the pieces
There's a DataSources class in Quarkus that looks like it should be able to be invoked programtically, but it expects some properties to be set at build time.
You can also use an AgroalPropertiesReader
Map<String,String> props=new HashMap<>();
props.put(AgroalPropertiesReader.MAX_SIZE,"10");
props.put(AgroalPropertiesReader.MIN_SIZE,"10");
props.put(AgroalPropertiesReader.INITIAL_SIZE,"10");
props.put(AgroalPropertiesReader.MAX_LIFETIME_S,"300");
props.put(AgroalPropertiesReader.ACQUISITION_TIMEOUT_S,"30");
props.put(AgroalPropertiesReader.JDBC_URL,"jdbcUrl");
props.put(AgroalPropertiesReader.PRINCIPAL,"username");
props.put(AgroalPropertiesReader.CREDENTIAL,"password");
AgroalDataSource datasource = AgroalDataSource.from(new AgroalPropertiesReader()
.readProperties(props)
.get());