@Test public void testGetNewConnection() { ODatabaseDocumentPool poolMock = mock(ODatabaseDocumentPool.class); when(poolMock.acquire(expectedDatabaseUrl, expectedUserName, expectedPassword)) .thenReturn(expectedConnection); connectionManager = new ConnectionManager(expectedDatabaseUrl, expectedUserName, expectedPassword, poolMock); ODatabaseDocument newConnection = connectionManager.getNewConnection(); assertEquals("Created connection not same as expected.", expectedConnection, newConnection); verify(poolMock).acquire(expectedDatabaseUrl, expectedUserName, expectedPassword); verifyZeroInteractions(expectedConnection); }
/** * @return A connection from the pool. Call close on the connection when done to return to the * pool. * @throws InternalServerErrorException */ ODatabaseDocumentTx getConnection() throws InternalServerErrorException { ODatabaseDocumentTx db = null; int maxRetry = 100; // give it up to approx 10 seconds to recover int retryCount = 0; synchronized (dbLock) { while (db == null && retryCount < maxRetry) { retryCount++; try { db = pool.acquire(dbURL, user, password); if (retryCount > 1) { logger.info( "Succeeded in acquiring connection from pool in retry attempt {}", retryCount); } retryCount = maxRetry; } catch (com.orientechnologies.common.concur.lock.OLockException ex) { // TODO: remove work-around once OrientDB resolves this condition if (retryCount == maxRetry) { logger.warn( "Failure reported acquiring connection from pool, retried {} times before giving up.", retryCount, ex); throw new InternalServerErrorException( "Failure reported acquiring connection from pool, retried " + retryCount + " times before giving up: " + ex.getMessage(), ex); } else { logger.info("Pool acquire reported failure, retrying - attempt {}", retryCount); logger.trace("Pool acquire failure detail ", ex); try { Thread.sleep(100); // Give the DB time to complete what it's doing before retrying } catch (InterruptedException iex) { // ignore that sleep was interrupted } } } } } return db; }