Disable / remove spring boot datasource by profile

6.6k Views Asked by At

Using spring boot yaml config, I have a datasource that looks like this:

datasource:
  url: jdbc:postgresql://somehost/somedb
  username: username
  password: password
  hikari:
    connection-timeout: 250
    maximum-pool-size: 1
    minimum-idle: 0

I can succesfully point to different DBs based on profile, but I'd like to setup a profile that does not use this datasource at all. When I use that profile, however, I get this:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.

Reason: Failed to determine a suitable driver class

How do I use this datasource in some profiles, but not in others?

2

There are 2 best solutions below

2
On

You can skip the bean for specific profiles using `@Profile("!dev") annotation

profile names can also be prefixed with a NOT operator e.g. “!dev” to exclude them from a profile

from docs here

If a given profile is prefixed with the NOT operator (!), the annotated component will be registered if the profile is not active — for example, given @Profile({"p1", "!p2"}), registration will occur if profile 'p1' is active or if profile 'p2' is not active.

Profiles can also be configured in XML – the tag has “profiles” attribute which takes comma separated values of the applicable profiles:here

 <beans profile="dev">
  <bean id="devDatasourceConfig"
  class="org.baeldung.profiles.DevDatasourceConfig" />
</beans>
2
On

Change to:

spring: 
   datasource:
      url: jdbc:postgresql://somehost/somedb
      username: username
      password: password
      hikari:
         connection-timeout: 250
         maximum-pool-size: 1
         minimum-idle: 0

Springboot works with Autoconfiguration by default, but you can customize excluding some AutoConfiguration classes

Edit your configuration to skip AutoConfiguration:

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})

Make your own datasource by profile

@Bean
  @Profile("dev")
  DataSource dataSourceDevProfile(org.springframework.core.env.Environment environment) throws Exception {
     return DataSourceBuilder.create().url("").driverClassName("").password("").username("").build();
   }

@Bean
@Profile("!dev")
DataSource dataSourceNoDev(org.springframework.core.env.Environment environment) throws Exception {
    return DataSourceBuilder.create().url(environment.getProperty("spring.datasource.url")).driverClassName("").password(environment.getProperty("spring.datasource.password")).username(environment.getProperty("spring.datasource.username")).build();
}

Or Totally Programatically

@Bean
DataSource dataSource2(org.springframework.core.env.Environment environment) throws Exception {
    if (environment.acceptsProfiles("dev")){
        //return datasource dev
    }else{
        //return datasource prod
    }