@Test public void connectionsEvictedWhenIdleLongEnough() throws Exception { ConnectionPool pool = new ConnectionPool(1, 100L, TimeUnit.NANOSECONDS); pool.setCleanupRunnableForTest(emptyRunnable); RealConnection c1 = new RealConnection(routeA1); c1.idleAtNanos = 50L; c1.socket = new Socket(); assertFalse(c1.socket.isClosed()); pool.put(c1); // Running at time 50, the pool returns that nothing can be evicted until time 150. assertEquals(100L, pool.cleanup(50L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); // Running at time 60, the pool returns that nothing can be evicted until time 150. assertEquals(90L, pool.cleanup(60L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); // Running at time 149, the pool returns that nothing can be evicted until time 150. assertEquals(1L, pool.cleanup(149L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); // Running at time 150, the pool evicts. assertEquals(0, pool.cleanup(150L)); assertEquals(0, pool.getConnectionCount()); assertTrue(c1.socket.isClosed()); // Running again, the pool reports that no further runs are necessary. assertEquals(-1, pool.cleanup(150L)); assertEquals(0, pool.getConnectionCount()); assertTrue(c1.socket.isClosed()); }
@Test public void inUseConnectionsNotEvicted() throws Exception { ConnectionPool pool = new ConnectionPool(1, 100L, TimeUnit.NANOSECONDS); pool.setCleanupRunnableForTest(emptyRunnable); RealConnection c1 = new RealConnection(routeA1); c1.allocationCount = 1; c1.idleAtNanos = 50L; c1.socket = new Socket(); assertFalse(c1.socket.isClosed()); pool.put(c1); // Running at time 50, the pool returns that nothing can be evicted until time 150. assertEquals(100L, pool.cleanup(50L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); // Running at time 60, the pool returns that nothing can be evicted until time 160. assertEquals(100L, pool.cleanup(60L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); // Running at time 160, the pool returns that nothing can be evicted until time 260. assertEquals(100L, pool.cleanup(160L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); }
public ManagedPoolItem(ConnectionPool cm, ManagedConnectionFactory mcf, ManagedConnection conn) { _cm = cm; _id = _cm.generateId(); _mcf = mcf; _mConn = conn; _poolStartTime = Alarm.getCurrentTime(); _poolEventTime = Alarm.getCurrentTime(); _connectionStartTime = cm.getConnectionTimeProbe().start(); // Gets the resource object from the driver try { if (cm.isXATransaction()) { XAResource xaResource = conn.getXAResource(); try { _defaultTransactionTimeout = xaResource.getTransactionTimeout(); } catch (Throwable e) { log.log(Level.FINE, e.toString(), e); } _xaResource = xaResource; } } catch (NotSupportedException e) { _cm.setXATransaction(false); log.log(Level.FINER, e.toString(), e); } catch (Exception e) { log.log(Level.FINE, e.toString(), e); } if (_xaResource == null) _isXATransaction = false; // Gets the local transaction from the driver try { if (_cm.isLocalTransaction()) _localTransaction = conn.getLocalTransaction(); } catch (NotSupportedException e) { _cm.setLocalTransaction(false); log.log(Level.FINER, e.toString(), e); } catch (Exception e) { log.log(Level.FINER, e.toString(), e); } _mConn.addConnectionEventListener(this); if (log.isLoggable(Level.FINE)) log.fine( "create: " + this + "(active:" + _cm.getConnectionActiveCount() + ", total:" + _cm.getConnectionCount() + ")"); }
@Test public void cleanupPrioritizesEarliestEviction() throws Exception { ConnectionPool pool = new ConnectionPool(1, 100L, TimeUnit.NANOSECONDS); pool.setCleanupRunnableForTest(emptyRunnable); RealConnection c1 = new RealConnection(routeA1); c1.idleAtNanos = 75L; c1.socket = new Socket(); pool.put(c1); RealConnection c2 = new RealConnection(routeB1); c2.idleAtNanos = 50L; c2.socket = new Socket(); pool.put(c2); // Running at time 75, the pool returns that nothing can be evicted until time 150. assertEquals(75L, pool.cleanup(75L)); assertEquals(2, pool.getConnectionCount()); // Running at time 149, the pool returns that nothing can be evicted until time 150. assertEquals(1L, pool.cleanup(149L)); assertEquals(2, pool.getConnectionCount()); // Running at time 150, the pool evicts c2. assertEquals(0L, pool.cleanup(150L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); assertTrue(c2.socket.isClosed()); // Running at time 150, the pool returns that nothing can be evicted until time 175. assertEquals(25L, pool.cleanup(150L)); assertEquals(1, pool.getConnectionCount()); // Running at time 175, the pool evicts c1. assertEquals(0L, pool.cleanup(175L)); assertEquals(0, pool.getConnectionCount()); assertTrue(c1.socket.isClosed()); assertTrue(c2.socket.isClosed()); }