From 380baf2a6e326d3c09c5f17962c850274e3a533c Mon Sep 17 00:00:00 2001 From: mlbiam Date: Mon, 30 Apr 2018 23:40:50 -0400 Subject: [PATCH] for #41 adding more timeout management --- .../myvd/inserts/jdbc/JdbcInsert.java | 57 +++++++---- .../net/sourceforge/myvd/server/Server.java | 99 ++++++++++++++++++- 2 files changed, 137 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/sourceforge/myvd/inserts/jdbc/JdbcInsert.java b/src/main/java/net/sourceforge/myvd/inserts/jdbc/JdbcInsert.java index 87c7c7e..5966a6e 100644 --- a/src/main/java/net/sourceforge/myvd/inserts/jdbc/JdbcInsert.java +++ b/src/main/java/net/sourceforge/myvd/inserts/jdbc/JdbcInsert.java @@ -159,6 +159,8 @@ public class JdbcInsert implements Insert,JdbcPool { boolean addBaseToFilter; + static HashMap poolCache = new HashMap(); + HashMap ldap2db,db2ldap; private String name; private boolean hasWhere; @@ -192,25 +194,40 @@ public void configure(String name, Properties props, NameSpace nameSpace) //this.maxIdleCons = Integer.parseInt(props.getProperty("maxIdleCons","5")); //logger.info("maxIdleCons : " + this.maxIdleCons); - - ComboPooledDataSource cpds = new ComboPooledDataSource(); - try { - cpds.setDriverClass(driver); - } catch (PropertyVetoException e1) { - throw new LDAPException(LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), LDAPException.OPERATIONS_ERROR, "Could not load driver",e1); - } - cpds.setJdbcUrl(url); - cpds.setUser(user); - cpds.setPassword(pwd); - cpds.setMaxPoolSize(this.maxCons); - //cpds.setma(this.maxIdleCons); - cpds.setPreferredTestQuery(this.valQuery); - cpds.setTestConnectionOnCheckin(true); - cpds.setIdleConnectionTestPeriod(30); - + String poolKey = (url+user).toLowerCase(); - this.ds = cpds; + synchronized(poolCache) { + if (poolCache.get(poolKey) != null ) { + logger.info(this.name + " - using existing connection pool"); + this.ds = poolCache.get(poolKey); + } else { + logger.info(this.name + " - creating connection pool"); + ComboPooledDataSource cpds = new ComboPooledDataSource(); + try { + cpds.setDriverClass(driver); + } catch (PropertyVetoException e1) { + throw new LDAPException(LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), LDAPException.OPERATIONS_ERROR, "Could not load driver",e1); + } + cpds.setJdbcUrl(url); + cpds.setUser(user); + cpds.setPassword(pwd); + cpds.setMaxPoolSize(this.maxCons); + //cpds.setma(this.maxIdleCons); + cpds.setPreferredTestQuery(this.valQuery); + cpds.setTestConnectionOnCheckin(true); + cpds.setIdleConnectionTestPeriod(30); + //TODO need to make configurable + cpds.setUnreturnedConnectionTimeout(Integer.parseInt(props.getProperty("unreturnedConnectionTimeout","0"))); + cpds.setDebugUnreturnedConnectionStackTraces(true); + + cpds.setCheckoutTimeout(30000); + + + this.ds = cpds; + poolCache.put(poolKey, cpds); + } + } base = nameSpace.getBase().toString(); rdn = props.getProperty("rdn"); @@ -985,7 +1002,11 @@ public HashMap getLDAP2DBMap() { } public void shutdown() { - // TODO Auto-generated method stub + synchronized(poolCache) { + for (String poolkey : poolCache.keySet()) { + poolCache.get(poolkey).close(); + } + } } diff --git a/src/main/java/net/sourceforge/myvd/server/Server.java b/src/main/java/net/sourceforge/myvd/server/Server.java index 8e5ce1a..70a6317 100644 --- a/src/main/java/net/sourceforge/myvd/server/Server.java +++ b/src/main/java/net/sourceforge/myvd/server/Server.java @@ -15,10 +15,15 @@ */ package net.sourceforge.myvd.server; +import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStreamReader; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; import java.nio.charset.Charset; import java.security.KeyStore; import java.util.ArrayList; @@ -412,7 +417,99 @@ public void startServer() throws Exception { ldapServer.start(); ((ExtendedRequestHandler) ldapServer.getExtendedRequestHandler()).init(globalChain, router); - + final int shutdownPort = Integer.parseInt(props.getProperty("server.shutdown.port","-1")); + if (shutdownPort > 0) { + final String shutdownHost = props.getProperty("server.shutdown.host","127.0.0.1"); + final String shutdownCommand = props.getProperty("server.shutdown.command","shutdown"); + + final Server server = this; + + new Thread() { + public void run() { + logger.info("Starting shutdown socket listener"); + try { + ServerSocket socket = new ServerSocket(shutdownPort,0,InetAddress.getByName(shutdownHost)); + while (true) { + logger.info("shutdown waiting for input"); + Socket clientSocket = null; + try { + clientSocket = socket.accept(); + } catch (Throwable t) { + logger.warn("Could not accept connection",t); + continue; + } + logger.info("request received"); + //PrintWriter out = + // new PrintWriter(clientSocket.getOutputStream(), true); + BufferedReader in = new BufferedReader( + new InputStreamReader(clientSocket.getInputStream())); + logger.info("reading data"); + String command = in.readLine(); + logger.info("'" + command + "'"); + if (command != null) { + command.trim(); + } + logger.info("'" + command + "'"); + if (shutdownCommand.equalsIgnoreCase(command)) { + logger.info("Stopping threads"); + + try { + server.stopServer(); + } catch (Exception e1) { + logger.warn("Could not gracefully shutdown server",e1); + } + + logger.info("Closing input stream"); + + try { + in.close(); + } catch (Throwable t) {} + + /*try { + out.close(); + } catch (Throwable t) {}*/ + + logger.info("Closing client socket"); + try { + clientSocket.close(); + } catch (Throwable t) {} + + logger.info("Closing server socket"); + try { + socket.close(); + } catch (Throwable t) {} + + logger.info("Sleeping for 10 seconds"); + try { + Thread.sleep(10000); + logger.info("Exiting"); + System.exit(0); + return; + } catch (Exception e) {} + + } else { + command = null; + logger.info("invalid command"); + try { + in.close(); + } catch (Throwable t) {} + + /*try { + out.close(); + } catch (Throwable t) {} +*/ + try { + clientSocket.close(); + } catch (Throwable t) {} + + } + } + } catch (IOException e) { + logger.error("Could not start shutdown listener",e); + } + } + }.start(); + } }