#Conditionally disabling Spring DataSource

1 messages · Page 1 of 1 (latest)

surreal gust
#

Hello, I am working on a Spring project using Spring Data JPA as a database. I would like to conditionally enable/disable a DataSource on startup.
Specifically, in case the DataSource information is invalid, or a user does not want to use a database, it should be possible to not configure a DataSource. However, if the DataSource is invalid the application simply crashes.

I am creating the DataSource using DataSourceBuilder in a @SpringBootConfiguration class

subtle juncoBOT
#

<@&1004656351647117403> please have a look, thanks.

subtle juncoBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

surreal gust
#

This is the way I'm creating the DataSource:

    @Bean
    public DataSource getDataSource() throws IOException {
        if (url == null || username == null || password == null) {
            return askForDataSource();
        }
        DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName(driverClassName);
        dataSourceBuilder.url(url);
        dataSourceBuilder.username(username);
        dataSourceBuilder.password(password);
        return dataSourceBuilder.build();
    }

Although I don't think thats super relevant here

subtle juncoBOT
# surreal gust This is the way I'm creating the DataSource: ```java @Bean public DataSo...

Detected code, here are some useful tools:

Formatted code
@Bean
public DataSource getDataSource() throws IOException {
  if (url == null  || username == null  || password == null ) {
    return askForDataSource();
  }
  DataSourceBuilder<? > dataSourceBuilder = DataSourceBuilder.create();
  dataSourceBuilder.driverClassName(driverClassName);
  dataSourceBuilder.url(url);
  dataSourceBuilder.username(username);
  dataSourceBuilder.password(password);
  return dataSourceBuilder.build();
}
solid gale
#

Why would it make sense to allow the user to use to use a Database or not?

#

Springboot automatically generates queries for databases to create them and the tables and tabs and stuff

#

If your database requires a database then it will always require one

#

You have two options you can switch between databases such as H2 and MySQL

#

Please think about why you are doing this.

surreal gust
#

I know that

#

I can either choose to use spring boot, or I can save data in the classpath

#

Either will work, and it depends on your use case

#

I just want to know how I can disable the JPA stuff at runtime

solid gale
#

You would have to disable everything that requires it

#

As well