1 24 25 package org.objectweb.cjdbc.controller.connection; 26 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import java.util.ArrayList ; 30 import java.util.LinkedList ; 31 32 import org.objectweb.cjdbc.common.i18n.Translate; 33 34 47 public abstract class AbstractPoolConnectionManager 48 extends AbstractConnectionManager 49 { 50 59 60 protected transient LinkedList freeConnections; 61 62 66 protected transient ArrayList activeConnections; 67 68 69 protected int poolSize; 70 71 74 75 90 public AbstractPoolConnectionManager(String backendUrl, String backendName, 91 String login, String password, String driverPath, String driverClassName, 92 int poolSize) 93 { 94 super(backendUrl, backendName, login, password, driverPath, driverClassName); 95 96 if (poolSize < 1) 97 throw new IllegalArgumentException ( 98 "Illegal value for size of the pool connection manager: " + poolSize); 99 100 this.poolSize = poolSize; 101 this.freeConnections = new LinkedList (); 102 this.activeConnections = new ArrayList (poolSize); 103 this.initialized = false; 104 105 if (logger.isDebugEnabled()) 106 logger.debug(Translate.get("connection.backend.pool.created", 107 new String []{backendName, String.valueOf(poolSize)})); 108 } 109 110 113 114 117 public synchronized void initializeConnections() throws SQLException 118 { 119 initializeConnections(poolSize); 120 } 121 122 128 public synchronized void initializeConnections(int initPoolSize) 129 throws SQLException 130 { 131 if (initialized) 132 throw new SQLException ("Connection pool for backend '" + backendUrl 133 + "' already initialized"); 134 135 if (initPoolSize > poolSize) 136 { 137 logger.warn(Translate.get("connection.max.poolsize.reached", 138 new String []{String.valueOf(initPoolSize), String.valueOf(poolSize), 139 String.valueOf(poolSize)})); 140 initPoolSize = poolSize; 141 } 142 143 Connection c = null; 144 145 boolean connectionsAvailable = true; 146 int i = 0; 147 while ((i < initPoolSize) && connectionsAvailable) 148 { 149 c = getConnectionFromDriver(); 150 151 if (c == null) 152 connectionsAvailable = false; 153 154 if (!connectionsAvailable) 155 { 156 if (i > 0) 157 { 158 logger.warn(Translate.get("connection.limit.poolsize", i)); 159 } 160 else 161 { 162 logger.warn(Translate.get("connection.initialize.pool.failed")); 163 poolSize = 0; 164 } 165 } 166 else 167 { 168 freeConnections.addLast(c); 169 i++; 170 } 171 } 172 173 poolSize = i; 174 initialized = true; 175 176 if (poolSize == 0) logger.error(Translate.get("connection.empty.pool")); 178 if (logger.isDebugEnabled()) 179 logger.debug(Translate.get("connection.pool.initialized", new String []{ 180 String.valueOf(initPoolSize), backendUrl})); 181 182 } 183 184 187 public synchronized void finalizeConnections() throws SQLException 188 { 189 if (!initialized) 190 { 191 String msg = Translate.get("connection.pool.not.initialized"); 192 logger.error(msg); 193 throw new SQLException (msg); 194 } 195 196 Connection c; 197 boolean error = false; 198 199 initialized = false; 201 int freed = 0; 202 while (!freeConnections.isEmpty()) 203 { 204 c = (Connection ) freeConnections.removeLast(); 205 try 206 { 207 c.close(); 208 } 209 catch (SQLException e) 210 { 211 error = true; 212 } 213 freed++; 214 } 215 if (logger.isInfoEnabled()) 216 logger.info(Translate.get("connection.freed.connection", new String []{ 217 String.valueOf(freed), backendUrl})); 218 219 int size = activeConnections.size(); 221 if (size > 0) 222 { 223 logger.warn(Translate.get("connection.connections.still.active", size)); 224 for (int i = 0; i < size; i++) 225 { 226 c = (Connection ) activeConnections.get(i); 227 try 228 { 229 c.close(); 230 } 231 catch (SQLException e) 232 { 233 error = true; 234 } 235 } 236 } 237 238 freeConnections.clear(); 241 activeConnections.clear(); 242 243 if (error) 244 { 245 String msg = Translate.get("connection.free.connections.failed"); 246 logger.error(msg); 247 throw new SQLException (msg); 248 } 249 } 250 251 254 public int getCurrentNumberOfConnections() 255 { 256 return poolSize; 257 } 258 259 } | Popular Tags |