genericconnection.java

来自「这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用s」· Java 代码 · 共 734 行 · 第 1/2 页

JAVA
734
字号
/*
 * $Header: /sfroot/cvs/esimple/src/core/org/apache/struts/legacy/GenericConnection.java,v 1.1.1.1 2004/09/08 06:38:34 lava Exp $
 * $Revision: 1.1.1.1 $
 * $Date: 2004/09/08 06:38:34 $
 *
 * ====================================================================
 *
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Struts", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */


package org.apache.struts.legacy;


import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.sql.Savepoint;
import java.util.Map;
import javax.sql.DataSource;


/**
 * Generic wrapper implementation of a <strong>Connection</strong> that
 * works with <code>GenericDataSource</code> to wrap connections for any
 * JDBC driver.
 * <p>
 * This version of the source MUST be compiled under Java 1.4 or later.
 * </p>
 * <p>
 * This class was originally maintained in the core Struts util package.
 * </p>
 *
 * @author Craig R. McClanahan
 * @author Ted Husted
 * @version $Revision: 1.1.1.1 $ $Date: 2004/09/08 06:38:34 $
 */

public class GenericConnection implements Connection {

    // ---------------------------------------------- since 1.4 - not supported
    // To compile under 1.3, this block of signatures can be commented out.

    public void setHoldability(int holdability) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public int getHoldability() throws SQLException{
        throw new UnsupportedOperationException();
    }

    public Savepoint setSavepoint() throws SQLException {
        throw new UnsupportedOperationException();
    }

    public Savepoint setSavepoint(String name) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public void rollback(Savepoint savepoint) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public void releaseSavepoint(Savepoint savepoint) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public Statement createStatement(int resultSetType, int resultSetConcurrency,
			      int resultSetHoldability) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public PreparedStatement prepareStatement(String sql, int resultSetType,
				       int resultSetConcurrency, int resultSetHoldability)
	throws SQLException {
        throw new UnsupportedOperationException();
    }

    public CallableStatement prepareCall(String sql, int resultSetType,
				  int resultSetConcurrency,
				  int resultSetHoldability) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
	throws SQLException {
        throw new UnsupportedOperationException();
    }

    public PreparedStatement prepareStatement(String sql, int columnIndexes[])
	throws SQLException {
        throw new UnsupportedOperationException();
    }

    public PreparedStatement prepareStatement(String sql, String columnNames[])
	throws SQLException {
        throw new UnsupportedOperationException();
    }


    // ----------------------------------------------------------- Constructors


    /**
     * Construct a new GenericConnection wrapping the specified connection.
     *
     * @param source The data source that owns this connection
     * @param conn The connection to wrap
     * @param autoCommit Desired auto-commit state for this connection
     * @param readOnly Desired read-only state for this connection
     *
     * @exception SQLException if an SQL processing error occurs
     */
    public GenericConnection(GenericDataSource source, Connection conn,
                             boolean autoCommit, boolean readOnly)
        throws SQLException {

        super();
        this.source = source;
        this.conn = conn;

        this.autoCommit = autoCommit;
        this.catalog = conn.getCatalog();
        this.level = conn.getTransactionIsolation();
        try {
            this.map = conn.getTypeMap();
        } catch (SQLException e) {
            ;   // PostgreSQL throws a "not yet implemented" exception
        } catch (UnsupportedOperationException e) {
            ;   // JDBC-ODBC bridge throws this
        } catch (AbstractMethodError e) {
            ;   // mm.mysql throws this
        }
	this.readOnly = readOnly;
        this.conn.setAutoCommit(this.autoCommit);
	try {
	    this.conn.setReadOnly(this.readOnly);
	} catch (SQLException e) {
	    ;  // Informix throws a "not supported" exception
	}


    }


    // ----------------------------------------------------- Instance Constants


    private final static String SQLEXCEPTION_CLOSED = "Connection was closed.";


    // ----------------------------------------------------- Instance Variables


    /**
     * The initial auto-commit state to which we should return after release.
     */
    protected boolean autoCommit = false;


    /**
     * The initial catalog to which we should return after release.
     */
    protected String catalog = null;


    /**
     * The closed flag for this wrapped connection.
     */
    private boolean closed = false;


    /**
     * The Connection that is being wrapped.
     */
    protected Connection conn = null;


    /**
     * The initial transaction isolation level to which we should return
     * after release.
     */
    protected int level = 0;


    /**
     * The initial type map to which we should return after release.
     */
    protected Map map = null;


    /**
     * The initial read-only state to which we should return after release.
     */
    protected boolean readOnly = false;


    /**
     * The GenericDataSource that owns this connection.
     */
    protected GenericDataSource source = null;


    // --------------------------------------------------------- Public Methods


    /**
     * Clear all warnings reported for this Connection.
     *
     * @exception SQLException if a database access error occurs
     */
    public void clearWarnings() throws SQLException {

        if (closed) throw new SQLException(SQLEXCEPTION_CLOSED);

        conn.clearWarnings();

    }


    /**
     * Return this wrapped Connection to our data source connection pool.
     *
     * @exception SQLException if a database access error occurs
     */
    public void close() throws SQLException {

        if (closed) throw new SQLException(SQLEXCEPTION_CLOSED);

        // Clean up any outstanding transaction as best we can
        try {
            conn.rollback();
        } catch (SQLException e) {
            ;
        }
        try {
            conn.setAutoCommit(this.autoCommit);
        } catch (SQLException e) {
            ;
        }
        try {
            conn.setCatalog(this.catalog);
        } catch (SQLException e) {
            ;
        }
        try {
            conn.setTransactionIsolation(this.level);
        } catch (SQLException e) {
            ;
        }
        try {
            conn.setTypeMap(this.map);
        } catch (SQLException e) {
            ;   // PostgreSQL throws a "not yet implemented" exception
        } catch (UnsupportedOperationException e) {
            ;   // JDBC-ODBC driver throws this
        } catch (AbstractMethodError e) {
            ;   // mm.mysql throws this
        }
        try {
            conn.setReadOnly(this.readOnly);
        } catch (SQLException e) {
            ;   // Informix throws a "not supported" exception
        }
        try {
            conn.clearWarnings();
        } catch (SQLException e) {
            ;
        }

        // Flag that this connection is closed
        // All methods accessing conn will now throw SQLEXCEPTION_CLOSED
        closed = true;

        // Return this connection to the available connection pool
        source.returnConnection(this);

    }


    /**
     * Make all changes made since the previous commit or rollback
     * permanent, and releases any database locks currently held.
     *
     * @exception SQLException if a database access error occurs
     */
    public void commit() throws SQLException {

        if (closed) throw new SQLException(SQLEXCEPTION_CLOSED);

        conn.commit();

    }


    /**
     * Create a <code>Statement</code> for sending SQL statements to the
     * database.
     *
     * @exception SQLException if a database access error occurs
     */
    public Statement createStatement() throws SQLException {

        if (closed) throw new SQLException(SQLEXCEPTION_CLOSED);

        return (conn.createStatement());

    }



    /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?