1 23 24 package org.continuent.sequoia.controller.connection; 25 26 import java.sql.Connection ; 27 import java.sql.SQLException ; 28 29 import org.continuent.sequoia.common.exceptions.UnreachableBackendException; 30 import org.continuent.sequoia.common.xml.DatabasesXmlTags; 31 32 40 public class SimpleConnectionManager extends AbstractConnectionManager 41 { 42 private int nbOfConnections = 0; 43 44 58 public SimpleConnectionManager(String backendUrl, String backendName, 59 String login, String password, String driverPath, String driverClassName) 60 { 61 super(backendUrl, backendName, login, password, driverPath, driverClassName); 62 } 63 64 67 protected Object clone() throws CloneNotSupportedException 68 { 69 return new SimpleConnectionManager(backendUrl, backendName, rLogin, 70 rPassword, driverPath, driverClassName); 71 } 72 73 77 public AbstractConnectionManager clone(String rLogin, String rPassword) 78 { 79 return new SimpleConnectionManager(backendUrl, backendName, rLogin, 80 rPassword, driverPath, driverClassName); 81 } 82 83 88 protected void doConnectionInitialization() throws SQLException 89 { 90 initialized = true; 91 if (idlePersistentConnectionPingInterval > 0) 92 { 93 persistentConnectionPingerThread = new IdlePersistentConnectionsPingerThread( 94 backendName, this); 95 persistentConnectionPingerThread.start(); 96 idlePersistentConnectionPingRunning = true; 97 } 98 } 99 100 105 protected void doConnectionFinalization() throws SQLException 106 { 107 initialized = false; 108 } 109 110 115 public PooledConnection getConnection() throws UnreachableBackendException 116 { 117 if (!initialized) 118 { 119 logger 120 .error("Requesting a connection from a non-initialized connection manager"); 121 return null; 122 } 123 if (isShutdown) 124 { 125 return null; 126 } 127 addConnection(); 128 Connection c = getConnectionFromDriver(); 129 if (c == null) 130 { 131 removeConnection(); 132 logger.error("Unable to get connection from " + backendUrl); 133 if (nbOfConnections == 0) 134 { 135 logger.error("Backend '" + backendUrl + "' is considered unreachable. " 136 + "(No active connection and none can be opened)"); 137 throw new UnreachableBackendException(); 138 } 139 } 140 return new PooledConnection(c); 141 } 142 143 148 public void releaseConnection(PooledConnection connection) 149 { 150 removeConnection(); 151 try 152 { 153 connection.getConnection().close(); 154 } 155 catch (SQLException e) 156 { 157 logger.error("Failed to close connection for '" + backendUrl + "'", e); 158 } 159 } 160 161 164 public void deleteConnection(PooledConnection c) 165 { 166 } 167 168 171 public void flagAllConnectionsForRenewal() 172 { 173 } 175 176 179 public int getCurrentNumberOfConnections() 180 { 181 return nbOfConnections; 182 } 183 184 private synchronized void addConnection() 185 { 186 nbOfConnections++; 187 } 188 189 private synchronized void removeConnection() 190 { 191 nbOfConnections--; 192 } 193 194 197 public String getXmlImpl() 198 { 199 return "<" + DatabasesXmlTags.ELT_SimpleConnectionManager + "/>"; 200 } 201 202 } | Popular Tags |