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.
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.
In my case, add spring-boot-starter-jdbc dependency works:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With