Peter Penzov
Peter Penzov

Reputation: 1728

'url' attribute is not specified and no embedded datasource could be configured

I'm trying to implement Spring application which uses 2 databases. I tried this:

application.properties

spring.production.datasource.url=jdbc:mariadb://localhost:3306/production_gateway
spring.production.datasource.username=wildfly
spring.production.datasource.password=qwerty
spring.production.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.production.datasource.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.production.datasource.jpa.show-sql = true
spring.production.datasource.jpa.hibernate.ddl-auto = update

spring.warehouse.datasource.url=jdbc:mariadb://localhost:3306/production_warehouse
spring.warehouse.datasource.username=wildfly
spring.warehouse.datasource.password=qwerty
spring.warehouse.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.warehouse.datasource.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.warehouse.datasource.jpa.show-sql = true
spring.warehouse.datasource.jpa.hibernate.ddl-auto = update

Configuration Bean:

@Configuration
@EnableJpaRepositories(
        basePackages = "org.plugin.production.service", 
        entityManagerFactoryRef = "productionEntityManagerFactory", 
        transactionManagerRef = "productionTransactionManager"
    )
@EnableTransactionManagement
public class ContextProductionDatasource {

    @Autowired
    private Environment env;

    @Primary
    @Bean(name = "productionDataSourceProperties")
    @ConfigurationProperties(prefix="spring.production.datasource")
    public LocalContainerEntityManagerFactoryBean userEntityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(userDataSource());
        em.setPackagesToScan(new String[] { "org.plugin.production.service" });

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", env.getProperty("jpa.hibernate.ddl-auto"));
        properties.put("hibernate.dialect", env.getProperty("jpa.properties.hibernate.dialect"));
        em.setJpaPropertyMap(properties);

        return em;
    }

    @Primary
    @Bean(name = "productionDataSourceProperties")
    @ConfigurationProperties(prefix="spring.production.datasource")
    public DataSource userDataSource()
    {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("datasource.url"));
        dataSource.setUsername(env.getProperty("username"));
        dataSource.setPassword(env.getProperty("password"));

        return dataSource;
    }

    @Primary
    @Bean(name = "productionTransactionManager")
    @ConfigurationProperties("spring.production.datasource")
    public PlatformTransactionManager productionTransactionManager(final EntityManagerFactory emf) {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

    @Primary
    @Bean(name = "productionExceptionTranslation")
    @ConfigurationProperties("spring.production.datasource")
    public PersistenceExceptionTranslationPostProcessor productionExceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }
}

But when I start the application I get error:

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

Description:

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

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
        If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

Do you know how I can fix this issue?

Upvotes: 0

Views: 3983

Answers (1)

Mykhailo Moskura
Mykhailo Moskura

Reputation: 2211

Please exclude DataSourceAutoConfiguration.class:

  @SpringBootApplication(exclude = {
    DataSourceAutoConfiguration.class})

Upvotes: 1

Related Questions