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(
33 getJdbcUrl(), getUsername(), getPassword());
34 // The following line must be kept in although it does not appear to be
35 // used, the constructor regsiters the
36 // constructed object at the connection pool.
37 PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
38 connectionFactory, connectionPool, null, null, false, true);
39 itsDataSource = new PoolingDataSource(connectionPool);
42 // / BELOW THIS LINE IS NOT OF INTEREST TO SUBCLASSES.
44 public final DataSource start() {
46 throw new RuntimeException("Database already started");
50 return getDatasource();
53 public final void stop() {
55 return; // nothing to do.
61 private final DataSource getDatasource() {
63 throw new RuntimeException("Database is not started!");
68 protected String getProperty(String aName) {
69 String value = System.getProperty(aName);
73 value = System.getenv(aName);
77 throw new RuntimeException("This class expects the '" + aName +
78 "' property to be set");