I am getting DataSource Not Supported when using DataSouceBuilder

▼魔方 西西 提交于 2019-12-04 03:09:50

问题


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.


回答1:


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.




回答2:


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

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
   </dependency>


来源:https://stackoverflow.com/questions/34790924/i-am-getting-datasource-not-supported-when-using-datasoucebuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!