1 package org.apache.ojb.broker.accesslayer; 2 3 17 18 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; 19 import org.apache.ojb.broker.platforms.PlatformException; 20 import org.apache.ojb.broker.platforms.PlatformFactory; 21 import org.apache.ojb.broker.util.ClassHelper; 22 import org.apache.ojb.broker.util.logging.Logger; 23 import org.apache.ojb.broker.util.logging.LoggerFactory; 24 25 import javax.naming.InitialContext ; 26 import javax.naming.NamingException ; 27 import javax.sql.DataSource ; 28 import java.sql.Connection ; 29 import java.sql.DriverManager ; 30 import java.sql.SQLException ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 35 41 public abstract class ConnectionFactoryAbstractImpl implements ConnectionFactory 42 { 43 private Logger log = LoggerFactory.getLogger(ConnectionFactoryAbstractImpl.class); 44 45 49 private Map dataSourceCache = new HashMap (); 50 51 72 public abstract Connection checkOutJdbcConnection(JdbcConnectionDescriptor jcd) 73 throws LookupException; 74 75 90 public abstract void releaseJdbcConnection(JdbcConnectionDescriptor jcd, Connection con) 91 throws LookupException; 92 93 public void releaseConnection(JdbcConnectionDescriptor jcd, Connection con) 94 { 95 if (con == null) return; 96 if (jcd.isDataSource()) 97 { 98 try 99 { 100 con.close(); 101 } 102 catch (SQLException e) 103 { 104 log.error("Closing connection failed", e); 105 } 106 } 107 else 108 { 109 try 110 { 111 releaseJdbcConnection(jcd, con); 112 } 113 catch (LookupException e) 114 { 115 log.error("Unexpected exception when return connection " + con + 116 " to pool using " + jcd, e); 117 } 118 } 119 } 120 121 public Connection lookupConnection(JdbcConnectionDescriptor jcd) throws LookupException 122 { 123 Connection conn; 124 128 if (jcd.isDataSource()) 129 { 130 if (log.isDebugEnabled()) 131 { 132 log.debug("do datasource lookup, name: " + jcd.getDatasourceName() + 133 ", user: " + jcd.getUserName()); 134 } 135 conn = newConnectionFromDataSource(jcd); 136 } 137 else 138 { 139 conn = checkOutJdbcConnection(jcd); 140 } 142 return conn; 143 } 144 145 153 protected void initializeJdbcConnection(Connection con, JdbcConnectionDescriptor jcd) 154 throws LookupException 155 { 156 try 157 { 158 PlatformFactory.getPlatformFor(jcd).initializeJdbcConnection(jcd, con); 159 } 160 catch (PlatformException e) 161 { 162 throw new LookupException("Platform dependent initialization of connection failed", e); 163 } 164 } 165 166 171 public synchronized void releaseAllResources() 172 { 173 this.dataSourceCache.clear(); 174 } 175 176 186 protected Connection newConnectionFromDataSource(JdbcConnectionDescriptor jcd) 187 throws LookupException 188 { 189 Connection retval = null; 190 DataSource ds = jcd.getDataSource(); 192 193 if (ds == null) 194 { 195 ds = (DataSource ) dataSourceCache.get(jcd.getDatasourceName()); 199 } 200 try 201 { 202 if (ds == null) 203 { 204 208 synchronized (dataSourceCache) 209 { 210 InitialContext ic = new InitialContext (); 211 ds = (DataSource ) ic.lookup(jcd.getDatasourceName()); 212 215 dataSourceCache.put(jcd.getDatasourceName(), ds); 216 } 217 } 218 if (jcd.getUserName() == null) 219 { 220 retval = ds.getConnection(); 221 } 222 else 223 { 224 retval = ds.getConnection(jcd.getUserName(), jcd.getPassWord()); 225 } 226 } 227 catch (SQLException sqlEx) 228 { 229 log.error("SQLException thrown while trying to get Connection from Datasource (" + 230 jcd.getDatasourceName() + ")", sqlEx); 231 throw new LookupException("SQLException thrown while trying to get Connection from Datasource (" + 232 jcd.getDatasourceName() + ")", sqlEx); 233 } 234 catch (NamingException namingEx) 235 { 236 log.error("Naming Exception while looking up DataSource (" + jcd.getDatasourceName() + ")", namingEx); 237 throw new LookupException("Naming Exception while looking up DataSource (" + jcd.getDatasourceName() + 238 ")", namingEx); 239 } 240 initializeJdbcConnection(retval, jcd); 242 if(log.isDebugEnabled()) log.debug("Create new connection using DataSource: "+retval); 243 return retval; 244 } 245 246 252 protected Connection newConnectionFromDriverManager(JdbcConnectionDescriptor jcd) 253 throws LookupException 254 { 255 Connection retval = null; 256 final String driver = jcd.getDriver(); 258 final String url = getDbURL(jcd); 259 try 260 { 261 ClassHelper.getClass(driver, true); 263 final String user = jcd.getUserName(); 264 final String password = jcd.getPassWord(); 265 final Properties properties = getJdbcProperties(jcd, user, password); 266 if (properties.isEmpty()) 267 { 268 if (user == null) 269 { 270 retval = DriverManager.getConnection(url); 271 } 272 else 273 { 274 retval = DriverManager.getConnection(url, user, password); 275 } 276 } 277 else 278 { 279 retval = DriverManager.getConnection(url, properties); 280 } 281 } 282 catch (SQLException sqlEx) 283 { 284 log.error("Error getting Connection from DriverManager with url (" + url + ") and driver (" + driver + ")", sqlEx); 285 throw new LookupException("Error getting Connection from DriverManager with url (" + url + ") and driver (" + driver + ")", sqlEx); 286 } 287 catch (ClassNotFoundException cnfEx) 288 { 289 log.error(cnfEx); 290 throw new LookupException("A class was not found", cnfEx); 291 } 292 catch (Exception e) 293 { 294 log.error("Instantiation of jdbc driver failed", e); 295 throw new LookupException("Instantiation of jdbc driver failed", e); 296 } 297 initializeJdbcConnection(retval, jcd); 299 if(log.isDebugEnabled()) log.debug("Create new connection using DriverManager: "+retval); 300 return retval; 301 } 302 303 312 protected Properties getJdbcProperties(JdbcConnectionDescriptor jcd, 313 String user, String password) 314 { 315 final Properties jdbcProperties; 316 jdbcProperties = jcd.getConnectionPoolDescriptor().getJdbcProperties(); 317 if (user != null) 318 { 319 jdbcProperties.put("user", user); 320 jdbcProperties.put("password", password); 321 } 322 return jdbcProperties; 323 } 324 325 protected Properties getJdbcProperties(JdbcConnectionDescriptor jcd) 326 { 327 final String user = jcd.getUserName(); 328 final String password = jcd.getPassWord(); 329 return getJdbcProperties(jcd, user, password); 330 } 331 332 protected String getDbURL(JdbcConnectionDescriptor jcd) 333 { 334 return jcd.isDataSource() ? jcd.getDatasourceName() : 335 jcd.getProtocol() + ":" + jcd.getSubProtocol() + ":" + jcd.getDbAlias(); 336 } 337 338 protected String getJcdDescription(JdbcConnectionDescriptor jcd) 339 { 340 return "Connection for JdbcConnectionDescriptor (" + 341 (jcd.getDatasourceName() != null ? "datasource: " + jcd.getDatasourceName() : 342 "db-url: " + getDbURL(jcd) + ", user: " + jcd.getUserName()) + 343 ")"; 344 } 345 346 } 347 | Popular Tags |