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/*
1374N/A * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
726N/A */
726N/A
726N/Apackage org.opensolaris.opengrok.jdbc;
726N/A
726N/Aimport java.sql.Connection;
747N/Aimport java.sql.DatabaseMetaData;
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 /**
862N/A * Check if the connection is still valid.
862N/A * @return {@code true} if the connection is valid, {@code false} if not
862N/A * @throws SQLException if a database error occurs
862N/A */
862N/A public boolean isValid() throws SQLException {
862N/A // Check whether the connection is valid. Assume that the connection
862N/A // is lost if there's no response after 10 seconds. (Will normally
862N/A // return immediately if the connection is lost, but use a timeout
862N/A // in case of odd network issues.)
862N/A return conn.isValid(10);
862N/A }
862N/A
862N/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);
1374N/A if (ps == null || ps.isClosed()) {
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 }
747N/A
747N/A /**
747N/A * Prepare a statement. This method should only be used to prepare
747N/A * statements that are used infrequently. If a statement is likely to
747N/A * be used frequently, a {@code StatementCreator} object should be
747N/A * created for it, and the method {@link #getStatement(StatementCreator)}
747N/A * should be used instead.
747N/A *
747N/A * @param sql the SQL text to compile
747N/A * @return a prepared statement
747N/A * @throws SQLException if a database error occurs
747N/A */
747N/A public PreparedStatement prepareStatement(String sql) throws SQLException {
747N/A return conn.prepareStatement(sql);
747N/A }
747N/A
747N/A /**
747N/A * Get a meta-data object for the underlying connection.
747N/A *
747N/A * @return a {@code DatabaseMetaData} object
747N/A * @throws SQLException if a database error occurs
747N/A */
747N/A public DatabaseMetaData getMetaData() throws SQLException {
747N/A return conn.getMetaData();
747N/A }
726N/A}