1 23 24 package org.continuent.sequoia.controller.connection; 25 26 import java.sql.Connection ; 27 import java.util.NoSuchElementException ; 28 29 import org.continuent.sequoia.common.exceptions.UnreachableBackendException; 30 import org.continuent.sequoia.common.i18n.Translate; 31 import org.continuent.sequoia.common.xml.DatabasesXmlTags; 32 33 41 public class FailFastPoolConnectionManager 42 extends AbstractPoolConnectionManager 43 { 44 45 59 public FailFastPoolConnectionManager(String backendUrl, String backendName, 60 String login, String password, String driverPath, String driverClassName, 61 int poolSize) 62 { 63 super(backendUrl, backendName, login, password, driverPath, 64 driverClassName, poolSize); 65 } 66 67 76 public synchronized PooledConnection getConnection() 77 throws UnreachableBackendException 78 { 79 if (!initialized) 80 { 81 logger.error(Translate.get("connection.request.not.initialized")); 82 return null; 83 } 84 if (isShutdown) 85 { 86 return null; 87 } 88 try 89 { PooledConnection c = (PooledConnection) freeConnections.removeLast(); 91 activeConnections.add(c); 92 return c; 93 } 94 catch (NoSuchElementException e) 95 { int missing = poolSize 97 - (activeConnections.size() + freeConnections.size()); 98 if (missing > 0) 99 { logger.info(Translate.get("connection.reallocate.missing", missing)); 101 PooledConnection connectionToBeReturned = null; 102 while (missing > 0) 103 { 104 Connection c = getConnectionFromDriver(); 105 if (c == null) 106 { 107 if (missing == poolSize) 108 { 109 logger.error(Translate.get("connection.backend.unreachable", 110 backendName)); 111 throw new UnreachableBackendException(); 112 } 113 logger.warn(Translate.get("connection.reallocate.failed", missing)); 114 break; 115 } 116 else 117 { 118 if (connectionToBeReturned == null) 119 connectionToBeReturned = new PooledConnection(c); 120 else 121 freeConnections.addLast(new PooledConnection(c)); 122 } 123 missing--; 124 } 125 return connectionToBeReturned; 126 } 127 if (logger.isWarnEnabled()) 128 logger.warn(Translate.get("connection.backend.out.of.connections", 129 new String []{backendName, String.valueOf(poolSize)})); 130 return null; 131 } 132 } 133 134 137 public synchronized void releaseConnection(PooledConnection c) 138 { 139 if (!initialized) 140 { 141 closeConnection(c); 142 return; } 144 145 if (activeConnections.remove(c)) 146 freeConnections.addLast(c); 147 else 148 logger.error(Translate.get("connection.release.failed", c.toString())); 149 } 150 151 154 public synchronized void deleteConnection(PooledConnection c) 155 { 156 closeConnection(c); 157 if (!initialized) 158 return; 160 if (activeConnections.remove(c)) 161 { 162 Connection newConnection = getConnectionFromDriver(); 163 if (newConnection == null) 164 { 165 if (logger.isDebugEnabled()) 166 logger.error(Translate 167 .get("connection.replaced.failed", c.toString())); 168 } 169 else 170 { 171 freeConnections.addLast(newConnection); 172 if (logger.isDebugEnabled()) 173 logger.debug(Translate.get("connection.replaced.success", c 174 .toString())); 175 } 176 } 177 else 178 logger.error(Translate.get("connection.replaced.failed.exception", c 179 .toString())); 180 } 181 182 185 public String getXmlImpl() 186 { 187 StringBuffer info = new StringBuffer (); 188 info.append("<" + DatabasesXmlTags.ELT_FailFastPoolConnectionManager + " " 189 + DatabasesXmlTags.ATT_poolSize + "=\"" + poolSize / 1000 + "\"/>"); 190 return info.toString(); 191 } 192 193 196 protected Object clone() throws CloneNotSupportedException 197 { 198 return new FailFastPoolConnectionManager(backendUrl, backendName, rLogin, 199 rPassword, driverPath, driverClassName, poolSize); 200 } 201 202 206 public AbstractConnectionManager clone(String rLogin, String rPassword) 207 { 208 return new FailFastPoolConnectionManager(backendUrl, backendName, rLogin, 209 rPassword, driverPath, driverClassName, poolSize); 210 } 211 212 } 213 | Popular Tags |