1 package org.wamblee.support.persistence;
3 import javax.sql.DataSource;
5 import org.apache.commons.dbcp.ConnectionFactory;
6 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
7 import org.apache.commons.dbcp.PoolableConnectionFactory;
8 import org.apache.commons.dbcp.PoolingDataSource;
9 import org.apache.commons.pool.impl.GenericObjectPool;
11 public abstract class AbstractDatabase implements Database {
12 private static final int CONNECTION_POOL_SIZE = 16;
14 private DataSource itsDataSource;
16 private boolean started;
18 protected AbstractDatabase() {
22 protected abstract void doStart();
24 protected abstract void doStop();
27 * This method must be called from the start method.
29 protected final void createDataSource() {
30 GenericObjectPool connectionPool = new GenericObjectPool(null);
31 connectionPool.setMaxActive(CONNECTION_POOL_SIZE);
32 ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(getJdbcUrl(), getUsername(), getPassword());
33 // The following line must be kept in although it does not appear to be used, the constructor regsiters the
34 // constructed object at the connection pool.
35 PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
36 itsDataSource = new PoolingDataSource(connectionPool);
40 /// BELOW THIS LINE IS NOT OF INTEREST TO SUBCLASSES.
42 public final DataSource start() {
44 throw new RuntimeException("Database already started");
48 return getDatasource();
51 public final void stop() {
53 return; // nothing to do.
59 private final DataSource getDatasource() {
61 throw new RuntimeException("Database is not started!");
66 protected String getProperty(String aName) {
67 String value = System.getProperty(aName);
68 if ( value != null ) {
71 value = System.getenv(aName);
72 if ( value != null ) {
75 throw new RuntimeException("This class expects the '" + aName + "' property to be set");