2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.support.persistence;
18 import javax.sql.DataSource;
20 import org.apache.commons.dbcp.ConnectionFactory;
21 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
22 import org.apache.commons.dbcp.PoolableConnectionFactory;
23 import org.apache.commons.dbcp.PoolingDataSource;
24 import org.apache.commons.pool.impl.GenericObjectPool;
26 public abstract class AbstractDatabase implements Database {
27 private static final int CONNECTION_POOL_SIZE = 16;
29 private DataSource itsDataSource;
31 private boolean started;
33 protected AbstractDatabase() {
37 protected abstract void doStart();
39 protected abstract void doStop();
42 * This method must be called from the start method.
44 protected final void createDataSource() {
45 GenericObjectPool connectionPool = new GenericObjectPool(null);
46 connectionPool.setMaxActive(CONNECTION_POOL_SIZE);
47 ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
48 getJdbcUrl(), getUsername(), getPassword());
49 // The following line must be kept in although it does not appear to be
50 // used, the constructor regsiters the
51 // constructed object at the connection pool.
52 PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
53 connectionFactory, connectionPool, null, null, false, true);
54 ingoredVariable(poolableConnectionFactory);
55 itsDataSource = new PoolingDataSource(connectionPool);
58 private static void ingoredVariable(PoolableConnectionFactory aFactory) {
62 // / BELOW THIS LINE IS NOT OF INTEREST TO SUBCLASSES.
64 public final DataSource start() {
66 throw new RuntimeException("Database already started");
70 return getDatasource();
73 public final void stop() {
75 return; // nothing to do.
81 private final DataSource getDatasource() {
83 throw new RuntimeException("Database is not started!");
88 protected String getProperty(String aName) {
89 String value = System.getProperty(aName);
93 value = System.getenv(aName);
97 throw new RuntimeException("This class expects the '" + aName +
98 "' property to be set");