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 java.util.logging.Level;
19 import java.util.logging.Logger;
21 import javax.sql.DataSource;
23 import org.apache.commons.dbcp.ConnectionFactory;
24 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
25 import org.apache.commons.dbcp.PoolableConnectionFactory;
26 import org.apache.commons.dbcp.PoolingDataSource;
27 import org.apache.commons.pool.impl.GenericObjectPool;
29 public abstract class AbstractDatabase implements Database {
31 private static final Logger LOGGER = Logger
32 .getLogger(AbstractDatabase.class.getName());
34 private static final int CONNECTION_POOL_SIZE = 16;
36 private DataSource itsDataSource;
38 private GenericObjectPool connectionPool;
40 private boolean started;
42 protected AbstractDatabase() {
46 protected abstract void doStart();
48 protected abstract void doStop();
51 * This method must be called from the start method.
53 protected final void createDataSource() {
54 connectionPool = new GenericObjectPool(null);
55 connectionPool.setMaxActive(CONNECTION_POOL_SIZE);
56 ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
57 getJdbcUrl(), getUsername(), getPassword());
58 // The following line must be kept in although it does not appear to be
59 // used, the constructor regsiters the
60 // constructed object at the connection pool.
61 PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
62 connectionFactory, connectionPool, null, null, false, true);
63 ingoredVariable(poolableConnectionFactory);
64 itsDataSource = new PoolingDataSource(connectionPool);
67 private static void ingoredVariable(PoolableConnectionFactory aFactory) {
71 // / BELOW THIS LINE IS NOT OF INTEREST TO SUBCLASSES.
73 public final DataSource start() {
75 throw new RuntimeException("Database already started");
79 return getDatasource();
82 public final void stop() {
84 return; // nothing to do.
88 connectionPool.close();
89 connectionPool.close();
90 } catch (Exception e) {
91 LOGGER.log(Level.WARNING, "Could not close pool", e);
96 private final DataSource getDatasource() {
98 throw new RuntimeException("Database is not started!");
100 return itsDataSource;
103 protected String getProperty(String aName) {
104 String value = System.getProperty(aName);
108 value = System.getenv(aName);
112 throw new RuntimeException("This class expects the '" + aName +
113 "' property to be set");