commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael Pradel (Created) (JIRA)" <>
Subject [jira] [Created] (DBCP-369) Exception when using SharedPoolDataSource concurrently
Date Wed, 12 Oct 2011 07:35:11 GMT
Exception when using SharedPoolDataSource concurrently

                 Key: DBCP-369
             Project: Commons Dbcp
          Issue Type: Bug
    Affects Versions: 1.4
         Environment: Java Hotspot Server VM 1.6.0_07, Linux
            Reporter: Michael Pradel


I get a ConcurrentModificationException when using instances of SharedPoolDataSource concurrently.
The problem occurs when one thread calls setDataSourceName() while another thread calls close(),
either on the same instance of SharedPoolDataSource or on different instances. I'll attach
a short example program that leads to an exception for almost every run on my machine.

The cause of the exception seems to be in InstanceKeyObjectFactory. registerNewInstance()
is synchronized, but removeInstance() isn't. This allows concurrent accesses to the non-thread-safe

Is this a bug? Or are SharedPoolDataSource and InstanceKeyObjectFactory not supposed to be


import java.util.ArrayList;
import org.apache.commons.dbcp.datasources.SharedPoolDataSource;

public class SharedPoolDataSourceTest {

	public static void main(String[] args) {
		new SharedPoolDataSourceTest().run();
	private void run() {
		final ArrayList<SharedPoolDataSource> dataSources = new ArrayList<SharedPoolDataSource>();
		for (int j = 0; j < 10000; j++) {
			SharedPoolDataSource dataSource = new SharedPoolDataSource();
		Thread t1 = new Thread(new Runnable() {
			public void run() {
				for (SharedPoolDataSource dataSource : dataSources) {
		Thread t2 = new Thread(new Runnable() {
			public void run() {
				for (SharedPoolDataSource dataSource : dataSources) {
					try { dataSource.close(); } catch (Exception e) {}	
		try {
		} catch (InterruptedException ie) {}

Exception in thread "Thread-0" java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextEntry(
	at java.util.HashMap$
	at org.apache.commons.dbcp.datasources.InstanceKeyObjectFactory.registerNewInstance(
	at org.apache.commons.dbcp.datasources.InstanceKeyDataSource.setDataSourceName(
	at potentialBugs.SharedPoolDataSourceTest$

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:!default.jspa
For more information on JIRA, see:


View raw message