title: Is C3P0 thread-safe?
body: <p>An interruption exception (java.lang.InterruptedException) occurs as I'm trying to perform some simple read (SELECT) operations using C3P0 on a MySQL database. The exception occurs as I increase the number of parallel threads more than 100 (I have tried with 5,10,20,60 and 100). The statement I execute is as simple as :</p>
  
  <pre><code>SELECT `Model.id` FROM `Model` LIMIT 100;
  </code></pre>
  
  <p>My connections are pooled from a ComboPooledDataSource which is configured using the following properties (see also the <a href="http://www.mchange.com/projects/c3p0/index.html" rel="noreferrer">C3P0 manual</a>):</p>
  
  <pre><code>c3p0.jdbcUrl=jdbc:mysql... 
  c3p0.debugUnreturnedConnectionStackTraces=true
  c3p0.maxIdleTime=5
  c3p0.maxPoolSize=1000
  c3p0.minPoolSize=5
  c3p0.initialPoolSize=5
  c3p0.acquireIncrement=3
  c3p0.acquireRetryAttempts=50
  c3p0.numHelperThreads=20
  c3p0.checkoutTimeout=0
  c3p0.testConnectionOnCheckin=true
  c3p0.testConnectionOnCheckout=true
  user=***
  password=***
  </code></pre>
  
  <p>The MySQL server on the machine I run the tests is configured to accept 1024 connections and the unit tests I run are successfully executed (the data are retrieved from the database as expected). However, in the C3P0 log file, I find the following warning:</p>
  
  <pre><code>15:36:11,449  WARN BasicResourcePool:1876 - com.mchange.v2.resourcepool.BasicResourcePool@9ba6076 -- Thread unexpectedly interrupted while performing an acquisition attempt.
  java.lang.InterruptedException: sleep interrupted
      at java.lang.Thread.sleep(Native Method)
      at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
      at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
  </code></pre>
  
  <p>I'd like to know the reason for that warning and second its possible impact on the software's robustness and stability. Note that after use, I close the result set, the SQL statement and the connection. Finally, once the test is over, I close the pool by calling the method <code>ComboPooledDataSource#close()</code>. What is more weird (and seems to be to reveal a synchronization problem), is that if I give enough time to the pool using the following...</p>
  
  <pre><code>Thread.sleep(10000); // wait for some time
  datasource.close();
  </code></pre>
  
  <p>No warnings will appear in the logs! Dο you think this raises a thread safety issue for C3P0 or am I doing something the wrong way?</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>Let me mention that removing the <code>Thread.sleep(10000)</code>, apart from what already mention, causes the following info to be logged in the MySQL log file:</p>
  
  <pre><code>110221 14:57:13 [Warning] Aborted connection 9762 to db: 'myDatabase' user: 'root'
  host: 'localhost' (Got an error reading communication packets)
  </code></pre>
  
  <p>Might shed some more light...</p>
  
  <p>Update 2:</p>
  
  <p>Here is my MySQL server configuration. The number of maximum allowed connections by server is set to 1024 (as I mentioned above) which is adequate for what I'm trying to do. </p>
  
  <pre><code>[mysqld]
  max_allowed_packet  = 64M
  thread_concurrency      = 8
  thread_cache_size       = 8
  thread_stack        = 192K
  query_cache_size = 0
  query_cache_type = 0
  max_connections = 1024
  back_log = 50
  innodb_thread_concurrency = 6
  innodb_lock_wait_timeout = 120
  log_warnings
  </code></pre>
  
  <p>To obfuscate any doubt, I verified that the maximum number of connections is properly set by:</p>
  
  <pre><code>show global variables where Variable_name='max_connections';
  +-----------------+-------+
  | Variable_name   | Value |
  +-----------------+-------+
  | max_connections | 1024  | 
  +-----------------+-------+
  1 row in set (0.00 sec)
  </code></pre>