1 22 23 package org.xquark.jdbc.datasource; 24 25 import java.io.PrintWriter ; 26 import java.sql.*; 27 import java.util.Properties ; 28 29 import javax.sql.DataSource ; 30 31 import org.apache.commons.dbcp.*; 32 import org.apache.commons.pool.impl.GenericKeyedObjectPool; 33 import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory; 34 import org.apache.commons.pool.impl.GenericObjectPool; 35 36 37 public class JDBCDataSource implements DataSource { 38 39 42 private static final int DEFAULT_MAX_OPEN_STATEMENTS = 100; 43 44 47 protected Driver driver = null; 48 49 53 protected String url = null; 54 55 59 protected Properties connectionProperties = null; 60 61 64 protected GenericObjectPool connectionPool = null; 65 66 69 protected PoolableConnectionFactory connectionFactory = null; 70 71 74 protected PoolingDataSource dataSource = null; 75 76 JDBCDataSource(Driver driver, String url, Properties connectionProperties) throws SQLException { 77 this(driver, url, connectionProperties, DEFAULT_MAX_OPEN_STATEMENTS); 78 } 79 80 JDBCDataSource(Driver driver, String url, Properties connectionProperties, int maxOpenPreparedStatements) 81 throws SQLException 82 { 83 this.driver = driver; 84 this.url = url; 85 this.connectionProperties = connectionProperties; 86 87 connectionPool = new GenericObjectPool(); 88 connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW); 89 GenericKeyedObjectPoolFactory statementPoolFactory = null; 90 if (maxOpenPreparedStatements >= 0) { 91 statementPoolFactory = new GenericKeyedObjectPoolFactory( 92 null, 93 -1, GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL, 95 0, 1, maxOpenPreparedStatements); 98 } 99 100 DriverConnectionFactory driverConnectionFactory = 101 new DriverConnectionFactory(driver, url, connectionProperties); 102 103 connectionFactory = 104 new PoolableConnectionFactory(driverConnectionFactory, 105 connectionPool, 106 statementPoolFactory, 107 null, 108 false, 109 true); 110 if (connectionFactory == null) { 111 throw new SQLException("Cannot create PoolableConnectionFactory"); 112 } 113 dataSource = new PoolingDataSource(connectionPool); 114 } 115 116 122 public void close() throws SQLException { 123 GenericObjectPool oldpool = connectionPool; 124 connectionPool = null; 125 dataSource = null; 126 try { 127 if (oldpool != null) { 128 oldpool.close(); 129 } 130 } catch(SQLException e) { 131 throw e; 132 } catch(RuntimeException e) { 133 throw e; 134 } catch(Exception e) { 135 throw new SQLNestedException("Cannot close connection pool", e); 136 } 137 } 138 139 144 public Connection getConnection() throws SQLException { 145 return dataSource.getConnection(); 146 } 147 148 157 public Connection getConnection(String username, String password) throws SQLException { 158 return dataSource.getConnection(username, password); 159 } 160 161 166 public int getLoginTimeout() throws SQLException { 167 return dataSource.getLoginTimeout(); 168 } 169 170 175 public PrintWriter getLogWriter() throws SQLException { 176 return dataSource.getLogWriter(); 177 } 178 179 186 public void setLoginTimeout(int loginTimeout) throws SQLException { 187 dataSource.setLoginTimeout(loginTimeout); 188 } 189 190 197 public void setLogWriter(PrintWriter logWriter) throws SQLException { 198 dataSource.setLogWriter(logWriter); 199 } 200 201 public Driver getDriver() { 202 return this.driver; 203 } 204 205 public String getUrl() { 206 return this.url; 207 } 208 209 public String getUsername() { 210 return this.connectionProperties.getProperty("user"); 211 } 212 213 public String getPassword() { 214 return this.connectionProperties.getProperty("password"); 215 } 216 217 public void setDefaultAutoCommit(boolean defaultAutoCommit) { 218 connectionFactory.setDefaultAutoCommit(defaultAutoCommit); 219 } 220 221 public void setDefaultReadOnly(boolean defaultReadOnly) { 222 connectionFactory.setDefaultReadOnly(defaultReadOnly); 223 } 224 225 public void setDefaultTransactionIsolation(int defaultTransactionIsolation) { 226 connectionFactory.setDefaultTransactionIsolation(defaultTransactionIsolation); 227 } 228 229 public void setDefaultCatalog(String defaultCatalog) { 230 connectionFactory.setDefaultCatalog(defaultCatalog); 231 } 232 233 public void setValidationQuery(String validationQuery) { 234 if ((validationQuery != null) && (validationQuery.trim().length() > 0)) { 235 connectionFactory.setValidationQuery(validationQuery); 236 } else { 237 connectionFactory.setValidationQuery(null); 238 } 239 } 240 241 public boolean isAccessToUnderlyingConnectionAllowed() { 242 return dataSource.isAccessToUnderlyingConnectionAllowed(); 243 } 244 245 public void setAccessToUnderlyingConnectionAllowed(boolean allow) { 246 dataSource.setAccessToUnderlyingConnectionAllowed(allow); 247 } 248 249 251 255 public int getNumActive() { 256 if (connectionPool != null) { 257 return connectionPool.getNumActive(); 258 } else { 259 return 0; 260 } 261 } 262 263 267 public int getNumIdle() { 268 if (connectionPool != null) { 269 return connectionPool.getNumIdle(); 270 } else { 271 return 0; 272 } 273 } 274 275 public int getMaxActive() { 276 return connectionPool.getMaxActive(); 277 } 278 279 public void setMaxActive(int maxActive) { 280 connectionPool.setMaxActive(maxActive); 281 } 282 283 public int getMaxIdle() { 284 return connectionPool.getMaxIdle(); 285 } 286 287 public void setMaxIdle(int maxIdle) { 288 connectionPool.setMaxIdle(maxIdle); 289 } 290 291 public int getMinIdle() { 292 return connectionPool.getMinIdle(); 293 } 294 295 public void setMinIdle(int minIdle) { 296 connectionPool.setMinIdle(minIdle); 297 } 298 299 public long getMaxWait() { 300 return connectionPool.getMaxWait(); 301 } 302 303 public void setMaxWait(long maxWait) { 304 connectionPool.setMaxWait(maxWait); 305 } 306 307 public byte getWhenExhaustedAction() { 308 return connectionPool.getWhenExhaustedAction(); 309 } 310 311 public void setWhenExhaustedAction(byte whenExhaustedAction) { 312 connectionPool.setWhenExhaustedAction(whenExhaustedAction); 313 } 314 315 public boolean getTestOnBorrow() { 316 return connectionPool.getTestOnBorrow(); 317 } 318 319 public void setTestOnBorrow(boolean testOnBorrow) { 320 connectionPool.setTestOnBorrow(testOnBorrow); 321 } 322 323 public boolean getTestOnReturn() { 324 return connectionPool.getTestOnReturn(); 325 } 326 327 public void setTestOnReturn(boolean testOnReturn) { 328 connectionPool.setTestOnReturn(testOnReturn); 329 } 330 331 public long getTimeBetweenEvictionRunsMillis() { 332 return connectionPool.getTimeBetweenEvictionRunsMillis(); 333 } 334 335 public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) { 336 connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); 337 } 338 339 public int getNumTestsPerEvictionRun() { 340 return connectionPool.getNumTestsPerEvictionRun(); 341 } 342 343 public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) { 344 connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun); 345 } 346 347 public long getMinEvictableIdleTimeMillis() { 348 return connectionPool.getMinEvictableIdleTimeMillis(); 349 } 350 351 public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) { 352 connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); 353 } 354 355 public boolean getTestWhileIdle() { 356 return connectionPool.getTestWhileIdle(); 357 } 358 359 public void setTestWhileIdle(boolean testWhileIdle) { 360 connectionPool.setTestWhileIdle(testWhileIdle); 361 } 362 363 } 364 | Popular Tags |