I get DataSource is not supported when using DataSouceBuilder

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

If I declare a DB2 connection with XML as follows:

<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 into your code like this:

 @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 great. Be that as it may, I would like to use a DataSourceBuilder, as shown in the examples, so in the end I would like to go to:

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

But for some reason this does not work. I get

Called: java.lang.IllegalStateException: DataSource data type not supported

I 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 see that the error is happening in .build ().

I'm sure I miss something easy, but I can’t understand.

+5
source share
2 answers

M. Dane answered this. I missed the commons-dbcp of my dependencies! I thought it was so easy.

To use a DataSourceBuilder, you need to have commons-dbcp or tomcat-jdbc or hikaricp in your class path, otherwise it will not work. I you do not have one of them, you will receive a message when you receive.

+11
source

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

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

Source: https://habr.com/ru/post/1240614/


All Articles