@@ -226,18 +226,13 @@ private PooledConnection _obtainConnection() throws InterruptedException, SQLExc
226226 hitCount ++;
227227 // are other threads already waiting? (they get priority)
228228 if (waitingThreads == 0 ) {
229- PooledConnection freeConnection = extractFromFreeList ();
230- if (freeConnection != null ) {
231- return freeConnection ;
229+ PooledConnection connection = extractFromFreeList ();
230+ if (connection != null ) {
231+ return connection ;
232232 }
233- if (busyList .size () < maxSize ) {
234- // grow the connection pool
235- PooledConnection c = pool .createConnectionForQueue (connectionId ++);
236- int busySize = registerBusyConnection (c );
237- if (Log .isLoggable (DEBUG )) {
238- Log .debug ("DataSource [{0}] grow; id[{1}] busy[{2}] max[{3}]" , name , c .name (), busySize , maxSize );
239- }
240- return c ;
233+ connection = createConnection ();
234+ if (connection != null ) {
235+ return connection ;
241236 }
242237 }
243238 try {
@@ -257,13 +252,32 @@ private PooledConnection _obtainConnection() throws InterruptedException, SQLExc
257252 }
258253 }
259254
255+ private PooledConnection createConnection () throws SQLException {
256+ if (busyList .size () < maxSize ) {
257+ // grow the connection pool
258+ PooledConnection c = pool .createConnectionForQueue (connectionId ++);
259+ int busySize = registerBusyConnection (c );
260+ if (Log .isLoggable (DEBUG )) {
261+ Log .debug ("DataSource [{0}] grow; id[{1}] busy[{2}] max[{3}]" , name , c .name (), busySize , maxSize );
262+ }
263+ return c ;
264+ } else {
265+ return null ;
266+ }
267+ }
268+
260269 /**
261270 * Got into a loop waiting for connections to be returned to the pool.
262271 */
263272 private PooledConnection _obtainConnectionWaitLoop () throws SQLException , InterruptedException {
264273 long nanos = MILLIS_TIME_UNIT .toNanos (waitTimeoutMillis );
265274 for (; ; ) {
266275 if (nanos <= 0 ) {
276+ // We waited long enough, that a connection was returned, so we try to create a new connection.
277+ PooledConnection conn = createConnection ();
278+ if (conn != null ) {
279+ return conn ;
280+ }
267281 String msg = "Unsuccessfully waited [" + waitTimeoutMillis + "] millis for a connection to be returned."
268282 + " No connections are free. You need to Increase the max connections of [" + maxSize + "]"
269283 + " or look for a connection pool leak using datasource.xxx.capturestacktrace=true" ;
0 commit comments