ConnectionResource.java revision 726
726N/A/*
726N/A * CDDL HEADER START
726N/A *
726N/A * The contents of this file are subject to the terms of the
726N/A * Common Development and Distribution License (the "License").
726N/A * You may not use this file except in compliance with the License.
726N/A *
726N/A * See LICENSE.txt included in this distribution for the specific
726N/A * language governing permissions and limitations under the License.
726N/A *
726N/A * When distributing Covered Code, include this CDDL HEADER in each
726N/A * file and include the License file at LICENSE.txt.
726N/A * If applicable, add the following below this CDDL HEADER, with the
726N/A * fields enclosed by brackets "[]" replaced with your own identifying
726N/A * information: Portions Copyright [yyyy] [name of copyright owner]
726N/A *
726N/A * CDDL HEADER END
726N/A */
726N/A
726N/A/*
726N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
726N/A * Use is subject to license terms.
726N/A */
726N/A
726N/Apackage org.opensolaris.opengrok.jdbc;
726N/A
726N/Aimport java.sql.Connection;
726N/Aimport java.sql.PreparedStatement;
726N/Aimport java.sql.SQLException;
726N/Aimport java.sql.Statement;
726N/Aimport java.util.HashMap;
726N/Aimport java.util.Map;
726N/A
726N/A/**
726N/A * Class that manages the resources associated with a database connection.
726N/A * This includes a cache of {@code PreparedStatement}s.
726N/A */
726N/Apublic class ConnectionResource {
726N/A /** The connection to the database. */
726N/A private final Connection conn;
726N/A
726N/A /** Statement cache. */
726N/A private final Map<StatementCreator, PreparedStatement> statements =
726N/A new HashMap<StatementCreator, PreparedStatement>();
726N/A
726N/A /**
726N/A * Create a new {@code ConnectionResource} instance.
726N/A * @param manager the {@code ConnectionManager} that created this object
726N/A * @throws SQLException if an error occurs when connecting to the database
726N/A */
726N/A ConnectionResource(ConnectionManager manager) throws SQLException {
726N/A conn = manager.openConnection();
726N/A conn.setAutoCommit(false);
726N/A }
726N/A
726N/A /**
726N/A * Commit the transaction.
726N/A * @throws SQLException if a database error occurs
726N/A */
726N/A public void commit() throws SQLException {
726N/A conn.commit();
726N/A }
726N/A
726N/A /**
726N/A * Abort the transaction.
726N/A * @throws SQLException if a database error occurs
726N/A */
726N/A public void rollback() throws SQLException {
726N/A conn.rollback();
726N/A }
726N/A
726N/A /**
726N/A * Get a {@code PreparedStatement} as defined by the specified
726N/A * {@code StatementCreator}. If it is the first time the statement
726N/A * creator is used on this {@code ConnectionResource}, the creator's
726N/A * {@link StatementCreator#create(Connection)} method is called to
726N/A * create a new {@code PreparedStatement}. This statement is cached,
726N/A * so that on subsequent calls with the same statement creator, the same
726N/A * {@code PreparedStatement} will be returned.
726N/A *
726N/A * @param creator object that specifies how to create the statement if
726N/A * necessary
726N/A * @return a {@code PreparedStatement} object
726N/A * @throws SQLException if a database error occurs
726N/A */
726N/A public PreparedStatement getStatement(StatementCreator creator)
726N/A throws SQLException {
726N/A PreparedStatement ps = statements.get(creator);
726N/A if (ps == null || ps.isClosed()) {
726N/A // TODO: Log if isClosed() since callers should normally not
726N/A // close the statements themselves.
726N/A ps = creator.create(conn);
726N/A statements.put(creator, ps);
726N/A }
726N/A return ps;
726N/A }
726N/A
726N/A /**
726N/A * Create a new {@code Statement} object.
726N/A * @return a {@code Statement} object
726N/A * @throws java.sql.SQLException
726N/A */
726N/A public Statement createStatement() throws SQLException {
726N/A return conn.createStatement();
726N/A }
726N/A}