Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3DF2D7054 for ; Wed, 12 Oct 2011 07:35:43 +0000 (UTC) Received: (qmail 25807 invoked by uid 500); 12 Oct 2011 07:35:37 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 25680 invoked by uid 500); 12 Oct 2011 07:35:37 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 25632 invoked by uid 99); 12 Oct 2011 07:35:34 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Oct 2011 07:35:34 +0000 X-ASF-Spam-Status: No, hits=-2000.5 required=5.0 tests=ALL_TRUSTED,RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.116] (HELO hel.zones.apache.org) (140.211.11.116) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Oct 2011 07:35:32 +0000 Received: from hel.zones.apache.org (hel.zones.apache.org [140.211.11.116]) by hel.zones.apache.org (Postfix) with ESMTP id E17B5304933 for ; Wed, 12 Oct 2011 07:35:11 +0000 (UTC) Date: Wed, 12 Oct 2011 07:35:11 +0000 (UTC) From: "Michael Pradel (Created) (JIRA)" To: issues@commons.apache.org Message-ID: <2136528991.4000.1318404911924.JavaMail.tomcat@hel.zones.apache.org> Subject: [jira] [Created] (DBCP-369) Exception when using SharedPoolDataSource concurrently MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 Exception when using SharedPoolDataSource concurrently ------------------------------------------------------ Key: DBCP-369 URL: https://issues.apache.org/jira/browse/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 Hi, 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 'instanceMap'. Is this a bug? Or are SharedPoolDataSource and InstanceKeyObjectFactory not supposed to be thread-safe? Thanks! 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 dataSources = new ArrayList(); for (int j = 0; j < 10000; j++) { SharedPoolDataSource dataSource = new SharedPoolDataSource(); dataSources.add(dataSource); } Thread t1 = new Thread(new Runnable() { public void run() { for (SharedPoolDataSource dataSource : dataSources) { dataSource.setDataSourceName("a"); } } }); Thread t2 = new Thread(new Runnable() { public void run() { for (SharedPoolDataSource dataSource : dataSources) { try { dataSource.close(); } catch (Exception e) {} } } }); t1.start(); t2.start(); try { t1.join(); t2.join(); } catch (InterruptedException ie) {} } } Exception in thread "Thread-0" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793) at java.util.HashMap$KeyIterator.next(HashMap.java:828) at org.apache.commons.dbcp.datasources.InstanceKeyObjectFactory.registerNewInstance(InstanceKeyObjectFactory.java:51) at org.apache.commons.dbcp.datasources.InstanceKeyDataSource.setDataSourceName(InstanceKeyDataSource.java:246) at potentialBugs.SharedPoolDataSourceTest$1.run(SharedPoolDataSourceTest.java:23) at java.lang.Thread.run(Thread.java:619) -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa For more information on JIRA, see: http://www.atlassian.com/software/jira