Reactive Database Connection

Asked

Viewed 138 times

0

I am developing an app with Spring Boot and I have a configuration class that generates a @Bean Datasource.

I would like to generate alternative connection levels of connection to the database, as if the environment variables are incorrect, use application.properties data, if this is also, use configuration class, regardless of this order, would like a similar result, it is possible to configure this way, have some doc I can follow ?

I believe I have a more dynamic and reactive environment for connecting in this way.

Datasource Bean Class

package foo.bar.configuration;

import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
public class DataBaseConfiguration {

    private final static String DATABASE_CONNECTION_ROOT_USERNAME = "foo";
    private final static String DATABASE_CONNECTION_ROOT_PASSWORD = "bar";
    private final static String DATABASE_CONNECTION_URL = "jdbc:mysql://localhost:3306/foobar";

    @Bean
    @Primary
    public DataSource dataSource() {
        final DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(DatabaseDriver.MYSQL.getDriverClassName());
        driverManagerDataSource.setUrl(DATABASE_CONNECTION_URL);
        driverManagerDataSource.setUsername(DATABASE_CONNECTION_ROOT_USERNAME);
        driverManagerDataSource.setPassword(DATABASE_CONNECTION_ROOT_PASSWORD);
        return driverManagerDataSource;
    }
}
  • 1

    I don’t quite understand your problem: you will have configuration information in several places, several Property source, for example? Take a look at import Configuration, Property sources, etc. Moreover, it seems to have nothing to do with reactivity, since JDBC is not reactive, being by default blocking -, if this is your problem, look for clients SQL that are.

  • Thanks for the feedback. Yes I believe I was wrong to say about reactivity, I confess that I am still learning a little, I thought that the expression, would be more appropriate to the questioning, however would have, some tip for reactive sql clients ?

1 answer

0


For this problem I improved the above class, to use two sources, application.yml file and also data in code, being able to expand to other search alternatives.

The way I built it will look for the defined profile, if there is one in specific, it will use the pre-defined data and build the connection, I believe not yet be the best way, but it helps a lot with my problem.

@Configuration
public class DataBaseConfiguration {

    private final static String DATABASE_CONTAINER_CONNECTION_ROOT_USERNAME = "foo";
    private final static String DATABASE_CONTAINER_CONNECTION_ROOT_PASSWORD = "foobar123";
    private final static String DATABASE_CONTAINER_CONNECTION_URL = "jdbc:mysql://localhost:3306/bar?autoReconnect=true&useSSL=false";

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.url}")
    private String url;

    @Autowired
    Environment environment;

    @Bean
    @Primary
    public DataSource dataSource() {
        this.setConnectionConfiguration();

        final DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(DatabaseDriver.MYSQL.getDriverClassName());
        driverManagerDataSource.setUrl(this.url);
        driverManagerDataSource.setUsername(this.username);
        driverManagerDataSource.setPassword(this.password);

        return driverManagerDataSource;
    }

    private void setConnectionConfiguration() {
        if(Arrays.stream(environment.getActiveProfiles()).anyMatch(profile -> (profile.equalsIgnoreCase("container")))) {
            setterConnectionValues(DATABASE_CONTAINER_CONNECTION_ROOT_USERNAME, DATABASE_CONTAINER_CONNECTION_ROOT_PASSWORD, DATABASE_CONTAINER_CONNECTION_URL);
        }
    }

    private void setterConnectionValues(String username, String password, String url) {
        this.username = username;
        this.password = password;
        this.url = url;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.