Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am getting DataSource Not Supported when using DataSouceBuilder

I am new to Spring-Batch (and Spring in general), and have been following on line documentation to teach myself what I need to do this task. I am trying to connect to a DB2 database.

If I declare the DB2 connection with XML like this:

    <bean id="wcs_dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
    <property name="url" value="jdbc:db2://127.0.0.1/DEV" />
    <property name="username" value="user" />
    <property name="password" value="pass5" />
</bean>

Then load it in my code like so:

@Bean
    public JdbcCursorItemReader<Product> databaseItemReader() {             
        ApplicationContext context = 
                 new ClassPathXmlApplicationContext("context-datasource.xml");
        DataSource dataSource = (DataSource) context.getBean("wcs_dataSource");
        ((ConfigurableApplicationContext)context).close();

        JdbcCursorItemReader<Product> result = new JdbcCursorItemReader<Product>();
        result.setDataSource(dataSource);
        result.setSql(sqlString);
        result.setRowMapper(new ProductRowMapper());
        return result;
    }

It works perfectly. How ever I would like to use the DataSourceBuilder like the examples show so ultimately I would like to get to :

@ConfigurationProperties(prefix="DEV.datasource")
public DataSource Wcs_DataSource(){
  return DataSourceBuilder.create().build();
}

But for some reason that does not work. I get

Caused by: java.lang.IllegalStateException: No supported DataSource type found

I have also tried:

public DriverManagerDataSource dataSource() {               
    DataSourceBuilder DSBuilder = DataSourceBuilder.create();   
    DSBuilder.url("jdbc:db2://127.0.0.1/DEV");
    DSBuilder.username("user");
    DSBuilder.password("password");
    DSBuilder.driverClassName("com.ibm.db2.jcc.DB2Driver");
    DriverManagerDataSource result = (DriverManagerDataSource) DSBuilder.build();
    return result;      
}

And I get the same error. If I run it in the debugger, I can see that the error happens on the .build().

I am sure I am missing something easy, but I can not figure it out.

like image 230
VydorScope Avatar asked Jan 14 '16 13:01

VydorScope


2 Answers

M. Deinum answered it. I was missing commons-dbcp from my dependencies! I figured it was something easy like that.

To use the DataSourceBuilder you need to have commons-dbcp, or tomcat-jdbc or hikaricp on your classpath else it won't work. I you don't have one of those you will get the message as you get.

like image 86
VydorScope Avatar answered Sep 21 '22 19:09

VydorScope


In my case, add spring-boot-starter-jdbc dependency works:

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
   </dependency>
like image 33
Vincent Avatar answered Sep 24 '22 19:09

Vincent