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 junit.framework.Assert;
25 import org.apache.commons.dbcp.ConnectionFactory;
26 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
27 import org.apache.commons.dbcp.PoolableConnectionFactory;
28 import org.apache.commons.dbcp.PoolingDataSource;
29 import org.apache.commons.pool.impl.GenericObjectPool;
32 * Abstract database class providing the creation of the datasource,
33 * preventing duplicate starts of the same database, and checking
34 * for connection leaks when the database is stopped.
36 * @author Erik Brakkee
39 public abstract class AbstractDatabase implements Database {
42 * Set this system property to a non-null value to ignore connection leaks
43 * when {@link #stop()} is called.
45 private static final String IGNORE_CONNECTION_LEAK_PROPERTY = "org.wamblee.database.ignoreconnectionleaks";
47 private static final Logger LOGGER = Logger
48 .getLogger(AbstractDatabase.class.getName());
50 private static final int CONNECTION_POOL_SIZE = 16;
52 private DataSource itsDataSource;
54 private GenericObjectPool connectionPool;
56 private boolean started;
59 * Constructs the database.
61 protected AbstractDatabase() {
66 * To be implemented by subclasses to start the database.
68 protected abstract void doStart();
71 * To be implemented by subclasses to stop the database.
73 protected abstract void doStop();
76 * This method must be called from the start method.
78 protected final void createDataSource() {
79 connectionPool = new GenericObjectPool(null);
80 connectionPool.setMaxActive(CONNECTION_POOL_SIZE);
81 ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
82 getJdbcUrl(), getUsername(), getPassword());
83 // The following line must be kept in although it does not appear to be
84 // used, the constructor regsiters the
85 // constructed object at the connection pool.
86 PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
87 connectionFactory, connectionPool, null, null, false, true);
88 ingoredVariable(poolableConnectionFactory);
89 itsDataSource = new PoolingDataSource(connectionPool);
93 public int getActiveConnections() {
94 return connectionPool.getNumActive();
97 private static void ingoredVariable(PoolableConnectionFactory aFactory) {
101 // / BELOW THIS LINE IS NOT OF INTEREST TO SUBCLASSES.
104 * Starts the database.
106 public final DataSource start() {
108 throw new RuntimeException("Database already started");
112 return getDatasource();
116 * Stops the database and tests for connection leaks.
117 * In cast the system property with the name given by {@link #IGNORE_CONNECTION_LEAK_PROPERTY}
118 * is set then the connection leaks are not checked.
120 public final void stop() {
122 return; // nothing to do.
126 if (connectionPool.getNumActive() > 0) {
127 String msg = "JDBC connection pool still has " +
128 connectionPool.getNumActive() +
129 " active connection(s), this is a potential resource leak in the code\n";
130 // backdoor to ignore connection leaks. Use this system property only if you
131 // can safely ignore the connection leaks.
132 if (System.getProperty(IGNORE_CONNECTION_LEAK_PROPERTY) == null) {
136 connectionPool.close();
137 connectionPool.close();
138 } catch (Exception e) {
139 LOGGER.log(Level.WARNING, "Could not close pool", e);
144 private final DataSource getDatasource() {
146 throw new RuntimeException("Database is not started!");
148 return itsDataSource;
151 protected String getProperty(String aName) {
152 String value = System.getProperty(aName);
156 value = System.getenv(aName);
160 throw new RuntimeException("This class expects the '" + aName +
161 "' property to be set");