715N/A/*
715N/A * CDDL HEADER START
715N/A *
715N/A * The contents of this file are subject to the terms of the
715N/A * Common Development and Distribution License (the "License").
715N/A * You may not use this file except in compliance with the License.
715N/A *
715N/A * See LICENSE.txt included in this distribution for the specific
715N/A * language governing permissions and limitations under the License.
715N/A *
715N/A * When distributing Covered Code, include this CDDL HEADER in each
715N/A * file and include the License file at LICENSE.txt.
715N/A * If applicable, add the following below this CDDL HEADER, with the
715N/A * fields enclosed by brackets "[]" replaced with your own identifying
715N/A * information: Portions Copyright [yyyy] [name of copyright owner]
715N/A *
715N/A * CDDL HEADER END
715N/A */
715N/A
715N/A/*
715N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
715N/A * Use is subject to license terms.
715N/A */
715N/A
715N/Apackage org.opensolaris.opengrok.jdbc;
715N/A
715N/Aimport java.sql.Connection;
715N/Aimport java.sql.DriverManager;
715N/Aimport java.sql.SQLException;
726N/Aimport java.util.concurrent.ConcurrentLinkedQueue;
715N/A
726N/A/**
726N/A * Class that manages the pool of database connections.
726N/A */
715N/Apublic class ConnectionManager {
715N/A
726N/A /** The JDBC URL to use when creating new connections. */
715N/A private final String url;
715N/A
726N/A /** A list of connections not currently in use. */
726N/A private final ConcurrentLinkedQueue<ConnectionResource> connections =
726N/A new ConcurrentLinkedQueue<ConnectionResource>();
726N/A
726N/A /**
726N/A * Create a new {@code ConnectionManager} instance.
760N/A *
760N/A * @param driverClass the name of the JDBC driver class
760N/A * @param url the JDBC connection URL to the database
1185N/A * @throws ClassNotFoundException if the JDBC driver class cannot be found
726N/A */
760N/A public ConnectionManager(String driverClass, String url)
760N/A throws ClassNotFoundException {
760N/A Class.forName(driverClass);
760N/A this.url = url;
715N/A }
715N/A
726N/A /**
726N/A * Open a new connection to the database.
726N/A *
726N/A * @return a {@code Connection} object
726N/A * @throws SQLException if a database error occurs
726N/A */
726N/A Connection openConnection() throws SQLException {
715N/A return DriverManager.getConnection(url);
715N/A }
715N/A
726N/A /**
726N/A * Get a {@code ConnectionResource} object from the pool, or create a
726N/A * new one if the pool is empty. Callers should make sure that the object
726N/A * is returned to the pool by calling
726N/A * {@link #releaseConnection(ConnectionResource)} after they are finished
726N/A * with it.
726N/A *
726N/A * @return a {@code ConnectionResource} object
726N/A * @throws SQLException if a database error occurs
726N/A */
726N/A public ConnectionResource getConnectionResource() throws SQLException {
726N/A ConnectionResource cr = connections.poll();
862N/A if (cr == null || !cr.isValid()) {
726N/A cr = new ConnectionResource(this);
726N/A }
726N/A return cr;
726N/A }
726N/A
726N/A /**
726N/A * Return a {@code ConnectionResource} back to the pool.
726N/A *
726N/A * @param cr
726N/A * @throws SQLException
726N/A */
726N/A public void releaseConnection(ConnectionResource cr) throws SQLException {
726N/A cr.rollback();
726N/A connections.offer(cr);
715N/A }
715N/A}