1 24 25 package org.objectweb.cjdbc.controller.connection; 26 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 30 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException; 31 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 32 33 41 public class SimpleConnectionManager extends AbstractConnectionManager 42 { 43 private int nbOfConnections = 0; 44 45 59 public SimpleConnectionManager(String backendUrl, String backendName, 60 String login, String password, String driverPath, String driverClassName) 61 { 62 super(backendUrl, backendName, login, password, driverPath, driverClassName); 63 } 64 65 68 protected Object clone() throws CloneNotSupportedException 69 { 70 return new SimpleConnectionManager(backendUrl, backendName, rLogin, 71 rPassword, driverPath, driverClassName); 72 } 73 74 79 public void initializeConnections() throws SQLException 80 { 81 initialized = true; 82 } 83 84 89 public void finalizeConnections() throws SQLException 90 { 91 initialized = false; 92 } 93 94 99 public Connection getConnection() throws UnreachableBackendException 100 { 101 if (!initialized) 102 { 103 logger 104 .error("Requesting a connection from a non-initialized connection manager"); 105 return null; 106 } 107 108 addConnection(); 109 Connection c = getConnectionFromDriver(); 110 if (c == null) 111 { 112 removeConnection(); 113 logger.error("Unable to get connection from " + backendUrl); 114 if (nbOfConnections == 0) 115 { 116 logger.error("Backend '" + backendUrl + "' is considered unreachable. " 117 + "(No active connection and none can be opened)"); 118 throw new UnreachableBackendException(); 119 } 120 } 121 return c; 122 } 123 124 129 public void releaseConnection(Connection connection) 130 { 131 removeConnection(); 132 try 133 { 134 connection.close(); 135 } 136 catch (SQLException e) 137 { 138 logger.error("Failed to close connection for '" + backendUrl + "'", e); 139 } 140 } 141 142 145 public void deleteConnection(Connection c) 146 { 147 } 148 149 152 public int getCurrentNumberOfConnections() 153 { 154 return nbOfConnections; 155 } 156 157 private synchronized void addConnection() 158 { 159 nbOfConnections++; 160 } 161 162 private synchronized void removeConnection() 163 { 164 nbOfConnections--; 165 } 166 167 170 public String getXmlImpl() 171 { 172 return "<" + DatabasesXmlTags.ELT_SimpleConnectionManager + "/>"; 173 } 174 175 } | Popular Tags |