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