Loading projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/ldap/LdapConnectionPool.java +93 −55 Original line number Diff line number Diff line Loading @@ -89,7 +89,8 @@ import com.unboundid.ldap.sdk.SimpleBindRequest; /** * This object is designed to be shared between the DAO classes * for access to LDAP. There should only be a single instance. * It contains a connection pool object from the UnboundID library. * It wraps a connection pool object from the UnboundID library. * This object is thread-safe. * * @author majorb */ Loading @@ -108,29 +109,38 @@ public class LdapConnectionPool private long lastPoolCheck = System.currentTimeMillis(); LdapConnectionPool() public LdapConnectionPool() { this(LdapConfig.getLdapConfig()); } LdapConnectionPool(LdapConfig config) public LdapConnectionPool(LdapConfig config) { if (config == null) throw new IllegalArgumentException("config required"); connectionOptions = new LDAPConnectionOptions(); connectionOptions.setUseSynchronousMode(true); connectionOptions.setAutoReconnect(true); this.currentConfig = config; currentConfig = config; synchronized (poolMonitor) { pool = createPool(currentConfig); profiler.checkpoint("Create pool"); } } protected LDAPConnection getReadOnlyConnection() throws TransientException { synchronized (poolMonitor) public LDAPConnection getReadOnlyConnection() throws TransientException { poolCheck(); try { LDAPConnection conn = pool.getReadConnection(); LDAPConnection conn = null; synchronized (poolMonitor) { conn = pool.getReadConnection(); } logger.debug("Read pool statistics after borrow:\n" + pool.getReadPoolStatistics()); profiler.checkpoint("get read only connection"); conn.setConnectionOptions(connectionOptions); Loading @@ -142,18 +152,22 @@ public class LdapConnectionPool throw new TransientException("Failed to get read only connection", e); } } } protected LDAPConnection getReadWriteConnection() throws TransientException { synchronized (poolMonitor) public LDAPConnection getReadWriteConnection() throws TransientException { poolCheck(); try { LDAPConnection conn = pool.getWriteConnection(); LDAPConnection conn = null; synchronized (poolMonitor) { conn = pool.getWriteConnection(); } logger.debug("write pool statistics after borrow:\n" + pool.getWritePoolStatistics()); profiler.checkpoint("get read write connection"); conn.setConnectionOptions(connectionOptions); return conn; } Loading @@ -162,32 +176,39 @@ public class LdapConnectionPool throw new TransientException("Failed to get read write connection", e); } } } protected void releaseReadOnlyConnection(LDAPConnection conn) public void releaseReadOnlyConnection(LDAPConnection conn) { pool.releaseReadConnection(conn); logger.debug("Read pool statistics after release:\n" + pool.getReadPoolStatistics()); } protected void releaseReadWriteConnection(LDAPConnection conn) public void releaseReadWriteConnection(LDAPConnection conn) { pool.releaseWriteConnection(conn); logger.debug("write pool statistics after release:\n" + pool.getWritePoolStatistics()); } protected LdapConfig getCurrentConfig() public LdapConfig getCurrentConfig() { return currentConfig; } protected void shutdown() public void shutdown() { logger.debug("Shutting down pool"); pool.close(); profiler.checkpoint("Shutdown pool"); } @Override public void finalize() { // just in case the client doesn't call shutdown() if (!pool.isClosed()) pool.close(); } private void poolCheck() { if (timeToCheckPool()) Loading @@ -198,13 +219,25 @@ public class LdapConnectionPool if (!newConfig.equals(currentConfig)) { logger.debug("Detected ldap configuration change, rebuilding pools"); this.currentConfig = newConfig; boolean poolRecreated = false; final LDAPReadWriteConnectionPool oldPool = pool; synchronized (poolMonitor) { // check to see if another thread has already // done the work if (timeToCheckPool()) { this.currentConfig = newConfig; pool = createPool(currentConfig); profiler.checkpoint("Rebuild pool"); lastPoolCheck = System.currentTimeMillis(); poolRecreated = true; } } if (poolRecreated) { // close the old pool in a separate thread Runnable closeOldPool = new Runnable() { Loading @@ -218,16 +251,21 @@ public class LdapConnectionPool Thread closePoolThread = new Thread(closeOldPool); closePoolThread.start(); } } else { lastPoolCheck = System.currentTimeMillis(); } } } private boolean timeToCheckPool() { return System.currentTimeMillis() - lastPoolCheck > POOL_CHECK_INTERVAL_MILLESCONDS; return (System.currentTimeMillis() - lastPoolCheck) > POOL_CHECK_INTERVAL_MILLESCONDS; } LDAPReadWriteConnectionPool createPool(LdapConfig config) private LDAPReadWriteConnectionPool createPool(LdapConfig config) { LDAPConnectionPool ro = createPool(config.getReadOnlyPool(), config); LDAPConnectionPool rw = createPool(config.getReadOnlyPool(), config); Loading @@ -237,7 +275,7 @@ public class LdapConnectionPool return pool; } private LDAPConnectionPool createPool(LdapPool pool, LdapConfig config) private synchronized LDAPConnectionPool createPool(LdapPool pool, LdapConfig config) { try { Loading Loading
projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/ldap/LdapConnectionPool.java +93 −55 Original line number Diff line number Diff line Loading @@ -89,7 +89,8 @@ import com.unboundid.ldap.sdk.SimpleBindRequest; /** * This object is designed to be shared between the DAO classes * for access to LDAP. There should only be a single instance. * It contains a connection pool object from the UnboundID library. * It wraps a connection pool object from the UnboundID library. * This object is thread-safe. * * @author majorb */ Loading @@ -108,29 +109,38 @@ public class LdapConnectionPool private long lastPoolCheck = System.currentTimeMillis(); LdapConnectionPool() public LdapConnectionPool() { this(LdapConfig.getLdapConfig()); } LdapConnectionPool(LdapConfig config) public LdapConnectionPool(LdapConfig config) { if (config == null) throw new IllegalArgumentException("config required"); connectionOptions = new LDAPConnectionOptions(); connectionOptions.setUseSynchronousMode(true); connectionOptions.setAutoReconnect(true); this.currentConfig = config; currentConfig = config; synchronized (poolMonitor) { pool = createPool(currentConfig); profiler.checkpoint("Create pool"); } } protected LDAPConnection getReadOnlyConnection() throws TransientException { synchronized (poolMonitor) public LDAPConnection getReadOnlyConnection() throws TransientException { poolCheck(); try { LDAPConnection conn = pool.getReadConnection(); LDAPConnection conn = null; synchronized (poolMonitor) { conn = pool.getReadConnection(); } logger.debug("Read pool statistics after borrow:\n" + pool.getReadPoolStatistics()); profiler.checkpoint("get read only connection"); conn.setConnectionOptions(connectionOptions); Loading @@ -142,18 +152,22 @@ public class LdapConnectionPool throw new TransientException("Failed to get read only connection", e); } } } protected LDAPConnection getReadWriteConnection() throws TransientException { synchronized (poolMonitor) public LDAPConnection getReadWriteConnection() throws TransientException { poolCheck(); try { LDAPConnection conn = pool.getWriteConnection(); LDAPConnection conn = null; synchronized (poolMonitor) { conn = pool.getWriteConnection(); } logger.debug("write pool statistics after borrow:\n" + pool.getWritePoolStatistics()); profiler.checkpoint("get read write connection"); conn.setConnectionOptions(connectionOptions); return conn; } Loading @@ -162,32 +176,39 @@ public class LdapConnectionPool throw new TransientException("Failed to get read write connection", e); } } } protected void releaseReadOnlyConnection(LDAPConnection conn) public void releaseReadOnlyConnection(LDAPConnection conn) { pool.releaseReadConnection(conn); logger.debug("Read pool statistics after release:\n" + pool.getReadPoolStatistics()); } protected void releaseReadWriteConnection(LDAPConnection conn) public void releaseReadWriteConnection(LDAPConnection conn) { pool.releaseWriteConnection(conn); logger.debug("write pool statistics after release:\n" + pool.getWritePoolStatistics()); } protected LdapConfig getCurrentConfig() public LdapConfig getCurrentConfig() { return currentConfig; } protected void shutdown() public void shutdown() { logger.debug("Shutting down pool"); pool.close(); profiler.checkpoint("Shutdown pool"); } @Override public void finalize() { // just in case the client doesn't call shutdown() if (!pool.isClosed()) pool.close(); } private void poolCheck() { if (timeToCheckPool()) Loading @@ -198,13 +219,25 @@ public class LdapConnectionPool if (!newConfig.equals(currentConfig)) { logger.debug("Detected ldap configuration change, rebuilding pools"); this.currentConfig = newConfig; boolean poolRecreated = false; final LDAPReadWriteConnectionPool oldPool = pool; synchronized (poolMonitor) { // check to see if another thread has already // done the work if (timeToCheckPool()) { this.currentConfig = newConfig; pool = createPool(currentConfig); profiler.checkpoint("Rebuild pool"); lastPoolCheck = System.currentTimeMillis(); poolRecreated = true; } } if (poolRecreated) { // close the old pool in a separate thread Runnable closeOldPool = new Runnable() { Loading @@ -218,16 +251,21 @@ public class LdapConnectionPool Thread closePoolThread = new Thread(closeOldPool); closePoolThread.start(); } } else { lastPoolCheck = System.currentTimeMillis(); } } } private boolean timeToCheckPool() { return System.currentTimeMillis() - lastPoolCheck > POOL_CHECK_INTERVAL_MILLESCONDS; return (System.currentTimeMillis() - lastPoolCheck) > POOL_CHECK_INTERVAL_MILLESCONDS; } LDAPReadWriteConnectionPool createPool(LdapConfig config) private LDAPReadWriteConnectionPool createPool(LdapConfig config) { LDAPConnectionPool ro = createPool(config.getReadOnlyPool(), config); LDAPConnectionPool rw = createPool(config.getReadOnlyPool(), config); Loading @@ -237,7 +275,7 @@ public class LdapConnectionPool return pool; } private LDAPConnectionPool createPool(LdapPool pool, LdapConfig config) private synchronized LDAPConnectionPool createPool(LdapPool pool, LdapConfig config) { try { Loading