spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Multiple Datasource autowire error message.

Open ahirsh opened this issue 11 years ago • 4 comments

I'm not sure if thats the exact place to open the issue, but in my @Configuration class i have 2 DataSource beans functions like:

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
       dataSource.setDriverClassName("com.mysql.jdbc.Driver");
       dataSource.setUrl(env.getProperty("db.url"));
       dataSource.setUsername(env.getProperty("db.user"));
       dataSource.setPassword(env.getProperty("db.pass"));

       return dataSource;
    }

    @Bean(name="dataSourceQuartz")
    public DataSource dataSourceQuartz() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
       dataSource.setDriverClassName("com.mysql.jdbc.Driver");
       dataSource.setUrl(env.getProperty("db.s.url"));
       dataSource.setUsername(env.getProperty("db.s.user"));
       dataSource.setPassword(env.getProperty("db.s.pass"));

       return dataSource;
    }

Now this causes the spring boot to fail to load with exception:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat

now after reading section: "Configure Two DataSources" in the reference documentation I understand that one should be annotated with @Primary , and that fixed the issue. BUT i think spring should issue a better error message on such case , that will help better understand what is the problem.

Now thanks to @olivergierke for the response in DATA JPA JIRA (https://jira.spring.io/browse/), i can see the real response very deep inside the exception :

Error creating bean with name 'securityFilterChainRegistration' defined in class path resource 
[org/springframework/boot/autoconfigure/security/SpringBootWebSecurityConfiguration.class]: 
Unsatisfied dependency expressed through constructor argument with index 0 of type [javax.servlet.Filter]: 
: Error creating bean with name 'org.springframework.security.filterChainProxy':
Cannot resolve reference to bean 'org.springframework.security.filterChains' while setting constructor argument; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains':
Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0];
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': 
Cannot resolve reference to bean 'digestFilter' while setting constructor argument with key [2]; 
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'digestFilter' defined in class path resource [webSecurityConfig.xml]:
Cannot resolve reference to bean 'userService' while setting bean property 'userDetailsService'; 
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'userService':
Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dataSource' defined in class path resource [ServicesExternal/common/conifg/MySqlConfig.class]: 
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; 
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: dataSource,dataSourceQuartz

SO, I think this real error should be seen from the first place and not to need dig inside the exception for it.... what do you think ?

Thanks, Avishay

ahirsh avatar Sep 02 '15 12:09 ahirsh

As indicated already, that's the way exceptions work in Java. Not sure what to improve about it.

odrotbohm avatar Sep 02 '15 12:09 odrotbohm

Ok, Thanks for your quick response oliver.

ahirsh avatar Sep 02 '15 14:09 ahirsh

@ahirsh You might be interested in the recent change we made in 1.3.0.M5 which updates the log configuration to print the root cause first.

philwebb avatar Sep 04 '15 20:09 philwebb

@philwebb , thanks i will look into it.

ahirsh avatar Sep 06 '15 07:09 ahirsh