1 23 24 package org.continuent.sequoia.controller.connection; 25 26 import java.sql.Connection ; 27 import java.sql.SQLException ; 28 import java.util.ArrayList ; 29 import java.util.ConcurrentModificationException ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.Map ; 33 34 import org.continuent.sequoia.common.i18n.Translate; 35 36 49 public abstract class AbstractPoolConnectionManager 50 extends AbstractConnectionManager 51 { 52 61 62 protected transient LinkedList freeConnections; 63 64 68 protected transient ArrayList activeConnections; 69 70 71 protected int poolSize; 72 73 76 77 92 public AbstractPoolConnectionManager(String backendUrl, String backendName, 93 String login, String password, String driverPath, String driverClassName, 94 int poolSize) 95 { 96 super(backendUrl, backendName, login, password, driverPath, driverClassName); 97 98 if (poolSize < 1) 99 throw new IllegalArgumentException ( 100 "Illegal value for size of the pool connection manager: " + poolSize); 101 102 this.poolSize = poolSize; 103 this.freeConnections = new LinkedList (); 104 this.activeConnections = new ArrayList (poolSize); 105 this.initialized = false; 106 107 if (logger.isDebugEnabled()) 108 logger.debug(Translate.get("connection.backend.pool.created", 109 new String []{backendName, String.valueOf(poolSize)})); 110 } 111 112 115 116 119 protected synchronized void doConnectionInitialization() throws SQLException 120 { 121 doConnectionInitialization(poolSize); 122 if (idlePersistentConnectionPingInterval > 0) 123 { 124 persistentConnectionPingerThread = new IdlePersistentConnectionsPingerThread( 125 backendName, this); 126 persistentConnectionPingerThread.start(); 127 idlePersistentConnectionPingRunning = true; 128 } 129 } 130 131 137 protected synchronized void doConnectionInitialization(int initPoolSize) 138 throws SQLException 139 { 140 if (initialized) 141 throw new SQLException ("Connection pool for backend '" + backendUrl 142 + "' already initialized"); 143 144 if (initPoolSize > poolSize) 145 { 146 logger.warn(Translate.get("connection.max.poolsize.reached", 147 new String []{String.valueOf(initPoolSize), String.valueOf(poolSize), 148 String.valueOf(poolSize)})); 149 initPoolSize = poolSize; 150 } 151 152 Connection c = null; 153 154 boolean connectionsAvailable = true; 155 int i = 0; 156 while ((i < initPoolSize) && connectionsAvailable) 157 { 158 c = getConnectionFromDriver(); 159 160 if (c == null) 161 connectionsAvailable = false; 162 163 if (!connectionsAvailable) 164 { 165 if (i > 0) 166 { 167 logger.warn(Translate.get("connection.limit.poolsize", i)); 168 } 169 else 170 { 171 logger.warn(Translate.get("connection.initialize.pool.failed")); 172 poolSize = 0; 173 } 174 } 175 else 176 { 177 PooledConnection pc = new PooledConnection(c); 178 freeConnections.addLast(pc); 179 i++; 180 } 181 } 182 183 poolSize = i; 184 initialized = true; 185 186 if (poolSize == 0) logger.error(Translate.get("connection.empty.pool")); 188 if (logger.isDebugEnabled()) 189 logger.debug(Translate.get("connection.pool.initialized", new String []{ 190 String.valueOf(initPoolSize), backendUrl})); 191 192 } 193 194 197 protected synchronized void doConnectionFinalization() throws SQLException 198 { 199 if (!initialized) 200 { 201 String msg = Translate.get("connection.pool.not.initialized"); 202 logger.error(msg); 203 throw new SQLException (msg); 204 } 205 206 PooledConnection c; 207 boolean error = false; 208 209 initialized = false; 211 int freed = 0; 212 while (!freeConnections.isEmpty()) 213 { 214 c = (PooledConnection) freeConnections.removeLast(); 215 try 216 { 217 c.getConnection().close(); 218 } 219 catch (SQLException e) 220 { 221 error = true; 222 } 223 freed++; 224 } 225 if (logger.isInfoEnabled()) 226 logger.info(Translate.get("connection.freed.connection", new String []{ 227 String.valueOf(freed), backendUrl})); 228 229 int size = activeConnections.size(); 231 if (size > 0) 232 { 233 logger.warn(Translate.get("connection.connections.still.active", size)); 234 for (int i = 0; i < size; i++) 235 { 236 c = (PooledConnection) activeConnections.get(i); 237 238 boolean retry; 243 do 244 { 245 retry = false; 246 try 247 { 248 for (Iterator iter = persistentConnections.entrySet().iterator(); iter 249 .hasNext();) 250 { 251 Map.Entry element = (Map.Entry ) iter.next(); 252 if (element.getValue() == c) 253 { 254 iter.remove(); 255 } 256 } 257 } 258 catch (ConcurrentModificationException e) 259 { 260 retry = true; 261 } 262 } 263 while (retry); 264 269 270 } 279 } 280 281 freeConnections.clear(); 284 activeConnections.clear(); 285 this.notifyAll(); 286 System.gc(); 287 288 if (error) 289 { 290 String msg = Translate.get("connection.free.connections.failed"); 291 logger.error(msg); 292 throw new SQLException (msg); 293 } 294 } 295 296 299 public synchronized void flagAllConnectionsForRenewal() 300 { 301 if (!initialized) 302 { 303 String msg = Translate.get("connection.pool.not.initialized"); 304 logger.error(msg); 305 throw new RuntimeException (msg); 306 } 307 308 for (Iterator iter = freeConnections.iterator(); iter.hasNext();) 309 { 310 PooledConnection c = (PooledConnection) iter.next(); 311 c.setMustBeRenewed(true); 312 } 313 314 for (Iterator iter = activeConnections.iterator(); iter.hasNext();) 315 { 316 PooledConnection c = (PooledConnection) iter.next(); 317 c.setMustBeRenewed(true); 318 } 319 } 320 321 324 public int getCurrentNumberOfConnections() 325 { 326 return poolSize; 327 } 328 } | Popular Tags |