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;
31 public abstract class AbstractDatabase implements Database {
34 * Set this system property to a non-null value to ignore connection leaks
35 * when {@link #stop()} is called.
37 private static final String IGNORE_CONNECTION_LEAK_PROPERTY = "org.wamblee.database.ignoreconnectionleaks";
39 private static final Logger LOGGER = Logger
40 .getLogger(AbstractDatabase.class.getName());
42 private static final int CONNECTION_POOL_SIZE = 16;
44 private DataSource itsDataSource;
46 private GenericObjectPool connectionPool;
48 private boolean started;
50 protected AbstractDatabase() {
54 protected abstract void doStart();
56 protected abstract void doStop();
59 * This method must be called from the start method.
61 protected final void createDataSource() {
62 connectionPool = new GenericObjectPool(null);
63 connectionPool.setMaxActive(CONNECTION_POOL_SIZE);
64 ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
65 getJdbcUrl(), getUsername(), getPassword());
66 // The following line must be kept in although it does not appear to be
67 // used, the constructor regsiters the
68 // constructed object at the connection pool.
69 PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
70 connectionFactory, connectionPool, null, null, false, true);
71 ingoredVariable(poolableConnectionFactory);
72 itsDataSource = new PoolingDataSource(connectionPool);
76 public int getActiveConnections() {
77 return connectionPool.getNumActive();
80 private static void ingoredVariable(PoolableConnectionFactory aFactory) {
84 // / BELOW THIS LINE IS NOT OF INTEREST TO SUBCLASSES.
86 public final DataSource start() {
88 throw new RuntimeException("Database already started");
92 return getDatasource();
95 public final void stop() {
97 return; // nothing to do.
101 if (connectionPool.getNumActive() > 0) {
102 String msg = "JDBC connection pool still has " +
103 connectionPool.getNumActive() +
104 " active connection(s), this is a potential resource leak in the code\n";
105 // backdoor to ignore connection leaks. Use this system property only if you
106 // can safely ignore the connection leaks.
107 if (System.getProperty(IGNORE_CONNECTION_LEAK_PROPERTY) == null) {
111 connectionPool.close();
112 connectionPool.close();
113 } catch (Exception e) {
114 LOGGER.log(Level.WARNING, "Could not close pool", e);
119 private final DataSource getDatasource() {
121 throw new RuntimeException("Database is not started!");
123 return itsDataSource;
126 protected String getProperty(String aName) {
127 String value = System.getProperty(aName);
131 value = System.getenv(aName);
135 throw new RuntimeException("This class expects the '" + aName +
136 "' property to be set");