Main Tutorials

BasicDataSource causing java.util.ConcurrentModificationException in WebSphere

Problem

With Spring, declares data source as “org.apache.commons.dbcp.BasicDataSource“. When deployed to WebSphere, everything work perfectly.

File : spring-datasource.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">

    <bean 
       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="location">
           <value>classpath:config/database/database.properties</value>
       </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
         destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
	<property name="url" value="${jdbc.url}" />
	<property name="username" value="${jdbc.username}" />
	<property name="password" value="${jdbc.password}" />
    </bean>

</beans>

However, when restart the web application in WebSphere, it get following exception :


org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dataSource' defined in class path resource 
[config/database/spring/spring-datasource.xml]: Instantiation of bean failed; 
......
Caused by: java.lang.ExceptionInInitializerError
	at java.lang.J9VMInternals.initialize(J9VMInternals.java:222)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	... 114 more
Caused by: java.util.ConcurrentModificationException
	at java.util.AbstractList$SimpleListIterator.next(Unknown Source)
	at java.sql.DriverManager.getDrivers(Unknown Source)
	at org.apache.commons.dbcp.BasicDataSource.<clinit>(BasicDataSource.java:57)
	at java.lang.J9VMInternals.initializeImpl(Native Method)
	at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
	... 121 more

Look like the datasource (BasicDataSource) is causing “java.util.ConcurrentModificationException“.

Solution

You need to use WebSphere data source, not “org.apache.commons.dbcp.BasicDataSource“. To fix it, just register a WebSphere data source, and put jndi name like “jdbc/anythingDS“. In Spring, declares jdni datasource like this :

File : spring-datasource.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/anythingDS"/>
        <property name="lookupOnStartup" value="false"/>
        <property name="cache" value="true"/>
        <property name="proxyInterface" value="javax.sql.DataSource"/>
    </bean>

</beans>

In WebSphere development, you have to follow WebSphere way of doing things.

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Amit
11 years ago

Hi,
Thanks a lot for this reply. I also would like to know how you got to the conclusion that this would be causing error. how did you reach to the root of this problem

Maulik
12 years ago

Dear friend,

Thanks a lot for the reply.
I also doubted this solution, but by reading your blog I feel like confirming my concept. Let’s see if this works out.

Regards,
Maulik