18

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.

Bacteria
8,63211 gold badges57 silver badges73 bronze badges
asked Jan 14, 2016 at 13:30
2
  • 10
    First off all the build returns a DataSource and this should be the type you return, don't cast it to a DriverManagerDataSource as that will fail. 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. Another thing do you really need a custom namespace (DEV.datasource) why not simply use the default? Commented Jan 14, 2016 at 15:07
  • Namespace will be because there will be more than one datasource. This is the documentation I was following for that is: docs.spring.io/spring-boot/docs/current/reference/html/… I have tried it without and with DriverManagerDataSource with the same results. There was another howto I was looking that that used that So I tried it (was grasping for straws by that point), but it did not help. I have removed it since posting this. BUT adding commons-dbcp to my dependencies did work! Thanks! Commented Jan 14, 2016 at 15:59

2 Answers 2

24

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.

Unni Kris
3,1034 gold badges39 silver badges58 bronze badges
answered Jan 14, 2016 at 16:00
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, commons-dbcp does not worked for me , however hikaricp solved the issue
21

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

 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-jdbc</artifactId>
 </dependency>
answered Apr 17, 2019 at 4:14

3 Comments

Adding org.springframework.boot:spring-boot-starter-data-jpa helps too.
Thanks this was it, it needed additional jars to initialize specific data source
Thank you so much! This solution worked perfectly, and it solved my problem. I really appreciate your help!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.