Connection Reactive Database

0

I'm 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 for connecting to the database, such as if the environment variables are incorrect, using data from application.properties, if it is also using configuration class, regardless of this order, would like a similar result, it is possible to configure this way, is there any doc that I can follow?

I believe I have a more dynamic and responsive connection environment 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;
    }
}
    
asked by anonymous 15.05.2018 / 20:05

1 answer

0

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

As I build it will search for the defined profile, if there is any specific, it will use the pre-defined data and build the connection, I believe it is still not 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;
    }
}
    
19.05.2018 / 02:56