1 43 package org.exolab.jms.persistence; 44 45 import java.sql.Connection ; 46 import java.sql.SQLException ; 47 import javax.sql.DataSource ; 48 49 import org.apache.commons.dbcp.BasicDataSource; 50 51 52 58 public class DBCPConnectionManager extends AbstractConnectionManager { 59 60 63 private DataSource _dataSource; 64 65 68 public DBCPConnectionManager() { 69 } 70 71 76 public void init() throws PersistenceException { 77 long evictionIntervalMS = getEvictionInterval() * 1000; 78 long minIdleTimeMS = getMinIdleTime() * 1000; 79 80 String testQuery = getTestQuery(); 81 boolean testWhileIdle = false; 82 boolean testOnBorrow = false; 83 84 if (testQuery != null) { 85 testWhileIdle = true; 86 testOnBorrow = getTestBeforeUse(); 87 } 88 89 BasicDataSource dataSource = new BasicDataSource(); 90 dataSource.setUsername(getUser()); 91 dataSource.setPassword(getPassword()); 92 dataSource.setDriverClassName(getDriver()); 93 dataSource.setUrl(getURL()); 94 dataSource.setDefaultAutoCommit(false); 95 dataSource.setPoolPreparedStatements(true); 96 97 dataSource.setMaxActive(getMaxActive()); 99 dataSource.setMaxIdle(getMaxIdle()); 100 dataSource.setValidationQuery(testQuery); 101 dataSource.setTestOnBorrow(getTestBeforeUse()); 102 dataSource.setTimeBetweenEvictionRunsMillis(evictionIntervalMS); 103 dataSource.setMinEvictableIdleTimeMillis(minIdleTimeMS); 104 dataSource.setTestWhileIdle(testWhileIdle); 105 106 _dataSource = dataSource; 107 } 108 109 116 public Connection getConnection() throws PersistenceException { 117 Connection connection; 118 try { 119 connection = _dataSource.getConnection(); 120 } catch (SQLException exception) { 121 throw new PersistenceException("Failed to get pooled connection", 122 exception); 123 } 124 125 return connection; 126 } 127 128 } | Popular Tags |