1 19 package org.apache.cayenne.conf; 20 21 import java.sql.Connection ; 22 23 import javax.sql.DataSource ; 24 25 import org.apache.cayenne.ConfigurationException; 26 import org.apache.commons.dbcp.ConnectionFactory; 27 import org.apache.commons.dbcp.DriverManagerConnectionFactory; 28 import org.apache.commons.dbcp.PoolableConnectionFactory; 29 import org.apache.commons.dbcp.PoolingDataSource; 30 import org.apache.commons.pool.KeyedObjectPoolFactory; 31 import org.apache.commons.pool.ObjectPool; 32 import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory; 33 import org.apache.commons.pool.impl.GenericObjectPool; 34 35 41 class DBCPDataSourceBuilder { 42 43 static final String DRIVER_CLASS_NAME = "driverClassName"; 45 static final String URL = "url"; 46 static final String USER_NAME = "username"; 47 static final String PASSWORD = "password"; 48 49 static final String MAX_ACTIVE = "maxActive"; 51 static final String MIN_IDLE = "minIdle"; 52 static final String MAX_IDLE = "maxIdle"; 53 static final String MAX_WAIT = "maxWait"; 54 static final String VALIDATION_QUERY = "validationQuery"; 55 static final String TEST_ON_BORROW = "testOnBorrow"; 56 static final String TEST_ON_RETURN = "testOnReturn"; 57 static final String TEST_IDLE = "testWhileIdle"; 58 static final String TIME_BETWEEN_EVICTIONS = "timeBetweenEvictionRunsMillis"; 59 static final String NUM_TEST_PER_EVICTION = "numTestsPerEvictionRun"; 60 static final String MIN_EVICTABLE_TIME = "minEvictableIdleTimeMillis"; 61 static final String EXHAUSTED_ACTION = "whenExhaustedAction"; 62 static final String AUTO_COMMIT = "defaultAutoCommit"; 63 static final String READ_ONLY = "defaultReadOnly"; 64 static final String TRANSACTION_ISOLATION = "defaultTransactionIsolation"; 65 static final String CONNECTION_NOWRAP = "accessToUnderlyingConnectionAllowed"; 66 static final String CATALOG = "defaultCatalog"; 67 68 70 static final String POOL_PS = "poolPreparedStatements"; 71 static final String PS_MAX_ACTIVE = "ps.maxActive"; 72 static final String PS_MAX_IDLE = "ps.maxIdle"; 73 static final String PS_MAX_TOTAL = "ps.maxTotal"; 74 static final String PS_MAX_WAIT = "ps.maxWait"; 75 static final String PS_MIN_EVICTABLE_TIME = "ps.minEvictableIdleTimeMillis"; 76 static final String PS_NUM_TEST_PER_EVICTION = "ps.numTestsPerEvictionRun"; 77 static final String PS_TEST_ON_BORROW = "ps.testOnBorrow"; 78 static final String PS_TEST_ON_RETURN = "ps.testOnReturn"; 79 static final String PS_TEST_IDLE = "ps.testWhileIdle"; 80 static final String PS_TIME_BETWEEN_EVICTIONS = "ps.timeBetweenEvictionRunsMillis"; 81 static final String PS_EXHAUSTED_ACTION = "ps.whenExhaustedAction"; 82 83 private DBCPDataSourceProperties config; 84 85 DBCPDataSourceBuilder(DBCPDataSourceProperties properties) { 86 this.config = properties; 87 } 88 89 DataSource createDataSource() { 90 boolean connectionNoWrap = config.getBoolean(CONNECTION_NOWRAP, false); 91 ObjectPool connectionPool = createConnectionPool(); 92 PoolingDataSource dataSource = new PoolingDataSource(connectionPool); 93 dataSource.setAccessToUnderlyingConnectionAllowed(connectionNoWrap); 94 95 return dataSource; 96 } 97 98 private void loadDriverClass() { 99 String driver = config.getString(DRIVER_CLASS_NAME, true); 100 try { 101 Class.forName(driver); 102 } 103 catch (ClassNotFoundException e) { 104 throw new ConfigurationException("Error loading driver " + driver, e); 105 } 106 } 107 108 private ObjectPool createConnectionPool() { 109 110 ConnectionFactory factory = createConnectionFactory(); 111 GenericObjectPool.Config poolConfig = createConnectionPoolConfig(); 112 KeyedObjectPoolFactory statementPool = createPreparedStatementPool(); 113 114 String validationQuery = config.getString(VALIDATION_QUERY); 115 boolean defaultReadOnly = config.getBoolean(READ_ONLY, false); 116 boolean defaultAutoCommit = config.getBoolean(AUTO_COMMIT, false); 117 int defaultTransactionIsolation = config.getTransactionIsolation( 118 TRANSACTION_ISOLATION, 119 Connection.TRANSACTION_SERIALIZABLE); 120 String defaultCatalog = config.getString(CATALOG); 121 122 ObjectPool connectionPool = new GenericObjectPool(null, poolConfig); 123 124 new PoolableConnectionFactory( 129 factory, 130 connectionPool, 131 statementPool, 132 validationQuery, 133 defaultReadOnly ? Boolean.TRUE : Boolean.FALSE, 134 defaultAutoCommit, 135 defaultTransactionIsolation, 136 defaultCatalog, 137 null); 138 139 return connectionPool; 140 } 141 142 private ConnectionFactory createConnectionFactory() { 143 loadDriverClass(); 144 String url = config.getString(URL, true); 145 String userName = config.getString(USER_NAME); 146 String password = config.getString(PASSWORD); 147 return new DriverManagerConnectionFactory(url, userName, password); 148 } 149 150 private KeyedObjectPoolFactory createPreparedStatementPool() { 151 152 if (!config.getBoolean("poolPreparedStatements", false)) { 153 return null; 154 } 155 156 162 int maxActive = config 163 .getInt(PS_MAX_ACTIVE, GenericObjectPool.DEFAULT_MAX_ACTIVE); 164 byte whenExhaustedAction = config.getWhenExhaustedAction( 165 PS_EXHAUSTED_ACTION, 166 GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION); 167 long maxWait = config.getLong(PS_MAX_WAIT, GenericObjectPool.DEFAULT_MAX_WAIT); 168 int maxIdle = config.getInt(PS_MAX_IDLE, GenericObjectPool.DEFAULT_MAX_IDLE); 169 int maxTotal = config.getInt(PS_MAX_TOTAL, 1); 170 171 boolean testOnBorrow = config.getBoolean( 172 PS_TEST_ON_BORROW, 173 GenericObjectPool.DEFAULT_TEST_ON_BORROW); 174 boolean testOnReturn = config.getBoolean( 175 PS_TEST_ON_RETURN, 176 GenericObjectPool.DEFAULT_TEST_ON_RETURN); 177 178 long timeBetweenEvictionRunsMillis = config.getLong( 179 PS_TIME_BETWEEN_EVICTIONS, 180 GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS); 181 int numTestsPerEvictionRun = config.getInt( 182 PS_NUM_TEST_PER_EVICTION, 183 GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN); 184 185 long minEvictableIdleTimeMillis = config.getLong( 186 PS_MIN_EVICTABLE_TIME, 187 GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); 188 189 boolean testWhileIdle = config.getBoolean( 190 PS_TEST_IDLE, 191 GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); 192 193 return new GenericKeyedObjectPoolFactory( 194 null, 195 maxActive, 196 whenExhaustedAction, 197 maxWait, 198 maxIdle, 199 maxTotal, 200 testOnBorrow, 201 testOnReturn, 202 timeBetweenEvictionRunsMillis, 203 numTestsPerEvictionRun, 204 minEvictableIdleTimeMillis, 205 testWhileIdle); 206 } 207 208 private GenericObjectPool.Config createConnectionPoolConfig() { 209 GenericObjectPool.Config poolConfig = new GenericObjectPool.Config(); 210 211 poolConfig.maxIdle = config.getInt(MAX_IDLE, GenericObjectPool.DEFAULT_MAX_IDLE); 212 poolConfig.minIdle = config.getInt(MIN_IDLE, GenericObjectPool.DEFAULT_MIN_IDLE); 213 poolConfig.maxActive = config.getInt( 214 MAX_ACTIVE, 215 GenericObjectPool.DEFAULT_MAX_ACTIVE); 216 poolConfig.maxWait = config.getLong(MAX_WAIT, GenericObjectPool.DEFAULT_MAX_WAIT); 217 218 poolConfig.testOnBorrow = config.getBoolean( 219 TEST_ON_BORROW, 220 GenericObjectPool.DEFAULT_TEST_ON_BORROW); 221 poolConfig.testOnReturn = config.getBoolean( 222 TEST_ON_RETURN, 223 GenericObjectPool.DEFAULT_TEST_ON_RETURN); 224 poolConfig.testWhileIdle = config.getBoolean( 225 TEST_IDLE, 226 GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); 227 228 poolConfig.timeBetweenEvictionRunsMillis = config.getLong( 229 TIME_BETWEEN_EVICTIONS, 230 GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS); 231 poolConfig.numTestsPerEvictionRun = config.getInt( 232 NUM_TEST_PER_EVICTION, 233 GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN); 234 poolConfig.minEvictableIdleTimeMillis = config.getLong( 235 MIN_EVICTABLE_TIME, 236 GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); 237 238 poolConfig.whenExhaustedAction = config.getWhenExhaustedAction( 239 EXHAUSTED_ACTION, 240 GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION); 241 242 return poolConfig; 243 } 244 } 245 | Popular Tags |