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