question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to configure a datasource programmatically

See original GitHub issue

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();
  }
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:8
  • Comments:6

github_iconTop GitHub Comments

6reactions
cpmoorecommented, Jun 17, 2020

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());

1reaction
Guisicommented, Mar 7, 2020

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();
}

} `

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring a DataSource Programmatically in Spring Boot
Learn how to configure a Spring Boot DataSource programmatically, thereby side-stepping Spring Boot's automatic DataSource configuration ...
Read more >
Configure DataSource programmatically in Spring Boot
In my case I have properties starting with datasource.postgres prefix. E.g @ConfigurationProperties(prefix = "datasource.postgres") @Bean @ ...
Read more >
How to configure a Datasource programmatically
This article covers how to define a Datasource resource programmatically using the DataSourceDefinition annotation. Then, we will show to to ...
Read more >
Spring Boot DataSource Configuration - HowToDoInJava
2. Configuring a DataSource ... Spring boot allows defining datasource configuration in following ways: ... During application startup, the ...
Read more >
Java DataSource, JDBC DataSource Example - DigitalOcean
Make sure that above configurations match with your local setup. Also make sure you have MySQL and Oracle DB JDBC jars included in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found