0
votes

I am facing issue while connecting to MYSQL database.

Here is my application.properties

MySQL Database

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://dataurl.com:3306/
spring.datasource.username=usename
spring.datasource.password=paswd

It is working with different application but not this one. I excluded hsql in POM here is the snippet

    <dependency>
        >>><groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.hsqldb</groupId>
                <artifactId>hsqldb</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>
</dependencies>

But got this error: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public javax.sql.DataSource main.java.com.fileuploadutility.configurator.BatchConfiguration.dataSource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2016-06-02 13:00:25.417 INFO 7776 --- [ main] o.apache.catalina.core.StandardService : Stopping service Tomcat 2016-06-02 13:00:25.426 WARN 7776 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: java.lang.Object.wait(Native Method)

For trying differently I added

public class DataSourceConfiguration { 
@Bean(name = "mysqlDS") 

@ConfigurationProperties(prefix = "spring.mysql.ds")
public DataSource mysqlDS() {
    return DataSourceBuilder.create().build();
}

and

@Autowired
@Qualifier("mysqlDS")
public DataSource dataSource;

But getting same the error

1
Have you checked if DataSourceConfiguration is in the path or in a subpackage of your spring boot aplication? it seems like your @bean declarations are not being considered. - Arturo MontaƱo
YArturo, what do you mean by datasorceconfiguration? I am able to connect to database in the work space for different project but this failing. Other properties from Application.propererties are working fine. Thanks for your reply - Sam
The only difference is this is spring batch app and it came with hsql dependency which i had to exclude - Sam
Yes Arturo, DataSourceConfiguration class is in the same folder. - Sam

1 Answers

0
votes

I found that the issue was due to the eclipse maven conflict. Maven was not creating the resources folder. After reaserch found that the resource folder in eclipse can be " linked" to src folder and the error is gone as it got hold of properties file.